AsyncTask 仅在第一次有效
我正在尝试创建一个实用程序 httpClient 类
我的代码在第一次执行时效果很好。调试时,第二个执行不会介入或执行,因此有些事情变得混乱。
活动/监听器
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;
}
实用程序类:
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 {
}
}
}
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 {
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AsyncTask 只能运行一次,如文档中所述:http://developer. android.com/reference/android/os/AsyncTask.html
因此,对于每个请求,您需要创建一个新的辅助对象。
编辑:
看看这个: ThreadSafeClientConnManager
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
每次执行时都会创建一个新的 AsyncTask 实例(在您的情况下为 HTTPHelper)。
Create a new instance of the
AsyncTask
(HTTPHelper
in your case) each time you execute it.