I use the following pattern for similar cases:
import java.util.List;
import org.apache.http.NameValuePair;
import android.os.AsyncTask;
public class AsyncHttpPostTask extends AsyncTask<Void, Void, Boolean> {
private List<NameValuePair> httpPostParams;
private String postHref;
private String someOtherValue;
public AsyncHttpPostTask(final String postHref, final List<NameValuePair> httpPostParams, final String someOtherValue) {
super();
this.postHref = postHref;
this.httpPostParams = httpPostParams;
this.someOtherValue = someOtherValue;
}
@Override
protected Boolean doInBackground(final Void... params) {
// Use httpPostParams (or any other values you supplied to a constructor) in your actual Http post here
// ...
return true;
}
}
To use the AsyncTask, create an instance suppling required parameters to the constructor and the call execute():
new AsyncHttpPostTask("http://example.com/post", httpPostParams, otherValue).execute();