AsyncTask only works first time
I'm trying to make a utility httpClient class
My code works great the first execute. When debugging, the second execute won't step in, or execute, so something is messed up.
The Activity/Listener
protected String getPage(String url, List<NameValuePair> namevaluePairs, String postOrGet, Activity whichActivity, String dialogText) {
try {
httpHelper.setListValues(namevaluePairs);
httpHelper.setPostOrGet(postOrGet);
httpHelper.setParentActivity(whichActivity);
httpHelper.setDialogText(dialogText);
httpHelper.execute(url);
} catch (Exception e) {
e.printStackTrace();
}
return resultHTML;
}
The Utility class:
public class HTTPHelper extends AsyncTask<String, Void, Void> {
private String resultString;
private HttpClient httpclient;
private List<NameValuePair> nameValuePairs;
private String postOrGet;
private Activity parentActivity;
private String Error;
private String dialogText;
private ProgressDialog Dialog;
WebServiceListener listener;
public HTTPHelper(WebServiceListener listener) {
this.listener = listener;
Error = null;
httpclient = new DefaultHttpClient();
postOrGet = "get";
nameValuePairs = null;
dialogText = "Logging in";
}
@Override
public void onPreExecute() {
super.onPreExecute();
Error = null;
Dialog.setMessage(dialogText);
Dialog.show();
}
@Override
public void onPostExecute(Void unused) {
Dialog.dismiss();
if (Error != null) {
Toast.makeText(parentActivity, Error, Toast.LENGTH_LONG).show();
} else {
ArrayList<String> myList = new ArrayList<String>();
myList.add(resultString);
listener.onHTTPGetComplete(myList);
}
}
public void setDialogText(String txt) {
dialogText = txt;
}
public void setListValues(List<NameValuePair> incNameValuePairs) {
nameValuePairs = incNameValuePairs;
}
public void setPostOrGet(String pOrG) {
postOrGet = pOrG;
}
public void setParentActivity(Activity myAct) {
parentActivity = myAct;
Dialog = new ProgressDialog(parentActivity);
}
@Override
protected Void doInBackground(String... urls) {
BufferedReader in = null;
try {
HttpGet httpget = new HttpGet(urls[0]);
HttpPost httppost = new HttpPost(urls[0]);
HttpResponse response = null;
if (postOrGet.toLowerCase().contains("post")) {
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
response = httpclient.execute(httppost);
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
response = httpclient.execute(httpget);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
} catch (IOException e) {
e.printStackTrace();
}
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
try {
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
resultString = sb.toString();
return 开发者_运维知识库null;
} finally {
}
}
}
An AsyncTask can only be run once as stated in the documentation : http://developer.android.com/reference/android/os/AsyncTask.html
So for every request you need to create a new helper object.
Edit:
Check this out : ThreadSafeClientConnManager
Create a new instance of the AsyncTask
(HTTPHelper
in your case) each time you execute it.
精彩评论