我想做一个实用程序httpClient类AsyncTask只能第一次工作
我的代码很好的执行第一次。在调试时,第二个执行不会进入或执行,所以有些东西被搞乱了。
的活动/监听
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 {
}
}
}
像冠军一样工作。谢谢Ravi和Haphazard。我会让你们两个都满意,但我还没有足够的积分。 – kireol 2011-06-09 03:18:30
@ user763472你应该接受一个很好的答案,不仅可以让它满意。我不认为你需要任何意见来接受你的问题的答案。接受答案会鼓励人们帮助你。 – 2011-06-09 04:38:21
嗨拉维,我已经做了类似的事情,但它不是working-'公共无效的onClick(视图v){ \t \t \t \t \t INT指数= groupPos; \t \t \t \t \t //现在读取相应的mChildStates字段并处理数据。 \t \t \t \t \t boolean chBoxesState [] = mChildCheckStates.get(index); \t \t \t \t \t //发送此数据到后端 \t \t \t \t \t Log.d( “点击摄影指导Vinit”, “我在这里”); \t \t \t \t \t RequestCloser reqCloser = null; \t \t \t \t \t reqCloser = new RequestCloser(); \t \t \t \t \t reqCloser.execute(chBoxesState); \t \t \t \t}'你能帮我解决吗 – codeomnitrix 2014-11-07 11:01:02