I want to factor out the AsyncTask to a Manager. I thought to make it a singleton, but then I cannot use generics as the object to serialize to JSON and send (e.g. PostManager<T>).How can this be refactored?public class RestPostActivity extends BaseActivity implements OnClickListener {...    public static String POST(String url, Person person) {        InputStream inputStream = null;        String result = ;        try {            // 1. create HttpClient            HttpClient httpclient = new DefaultHttpClient();            // 2. make POST request to the given URL            HttpPost httpPost = new HttpPost(url);            String json = ;            // 3. build jsonObject            JSONObject jsonObject = new JSONObject();            jsonObject.accumulate(name, person.getName());            jsonObject.accumulate(country, person.getCountry());            jsonObject.accumulate(twitter, person.getTwitter());            // 4. convert JSONObject to JSON to String            json = jsonObject.toString();            // ** Alternative way to convert Person object to JSON string usin            // Jackson Lib            // ObjectMapper mapper = new ObjectMapper();            // json = mapper.writeValueAsString(person);            // 5. set json to StringEntity            StringEntity se = new StringEntity(json);            // 6. set httpPost Entity            httpPost.setEntity(se);            // 7. Set some headers to inform server about the type of the            // content            httpPost.setHeader(Accept, application/json);            httpPost.setHeader(Content-type, application/json);            // 8. Execute POST request to the given URL            HttpResponse httpResponse = httpclient.execute(httpPost);            // 9. receive response as inputStream            inputStream = httpResponse.getEntity().getContent();            // 10. convert inputstream to string            if (inputStream != null)                result = convertInputStreamToString(inputStream);            else                result = Did not work!;        } catch (Exception e) {            Log.d(InputStream, e.getLocalizedMessage());        }        // 11. return result        return result;    }    public boolean isConnected() {        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();        if (networkInfo != null && networkInfo.isConnected())            return true;        else            return false;    }    @Override    public void onClick(View view) {        switch (view.getId()) {        case R.id.btnPost:            if (!validate())                Toast.makeText(getBaseContext(), Enter some data!,                        Toast.LENGTH_LONG).show();            // call AsynTask to perform network operation on separate thread            new HttpAsyncTask()                    .execute(http://hmkcode.appspot.com/jsonservlet);            break;        }    }    private class HttpAsyncTask extends AsyncTask<String, Void, String> {        @Override        protected String doInBackground(String... urls) {            person = new Person();            person.setName(etName.getText().toString());            person.setCountry(etCountry.getText().toString());            person.setTwitter(etTwitter.getText().toString());            return POST(urls[0], person);        }        // onPostExecute displays the results of the AsyncTask.        @Override        protected void onPostExecute(String result) {            Toast.makeText(getBaseContext(), Data Sent!, Toast.LENGTH_LONG)                    .show();        }    }    private boolean validate() {        if (etName.getText().toString().trim().equals())            return false;        else if (etCountry.getText().toString().trim().equals())            return false;        else if (etTwitter.getText().toString().trim().equals())            return false;        else            return true;    }    private static String convertInputStreamToString(InputStream inputStream)            throws IOException {        BufferedReader bufferedReader = new BufferedReader(                new InputStreamReader(inputStream));        String line = ;        String result = ;        while ((line = bufferedReader.readLine()) != null)            result += line;        inputStream.close();        return result;    }}
Factoring out REST-POST activity to a manager
java;android;database
First thing I would do is separate the two concerns you have in the POST method.The POST method should have a different name (since POST does not match Java conventions), and the signature should be something like:public static String httpPost(String url, JSONObject data) {    ....}And that method should handle the server communication.You should have a second method, perhaps Person.toJSON() that converts the Person to a JSONObject.I know I have used a different capitalization for httpPost() vs. toJSON(). For some reason, it makes sense to me since JSONObject is capitalized that way. Strict conformance to code-style guidelines suggests the method should be called toJson()Once you have separated the concerns, the httpPost(...) method should be completely reusable.If you want to be smart, create an interface JSONConvertible like:public interface JSONConvertible {    public JSONObject toJSON();}Then, you can have the stand-alone class (with the embedded httpPost method):public class HttpPostAsyncTask extends AsyncTask<String, Void, String> {    private static final String httpPost(String url, JSONObject value) {        .....    }    private final JSONObject toPost;    public HttpAsyncTask (JSONConvertible convertible) {        this(convertible.toJSON());    }    public HttpPostAsyncTask(JSONObject json) {        this.toPost = json;    }    @Override    protected String doInBackground(String... urls) {        return POST(urls[0], topost);    }    // onPostExecute displays the results of the AsyncTask.    @Override    protected void onPostExecute(String result) {        Toast.makeText(getBaseContext(), Data Sent!, Toast.LENGTH_LONG)                .show();    }}Then, make Person implement JSONConvertible ....and you can simply do:HttpPostAsyncTask atask = new HttpPostAsync(person);atask.execute(url)