我正在使用Android AsyncTask
每秒向服务器发送一些简单数据。服务器没有返回任何内容。但是,在此活动的短时间内,我开始看到ThreadPool错误。我很快意识到,由于某种原因,线程被创建但从未被破坏。然后我在每次POST时从服务器发回一个简单的响应,现在它正在完美地工作。为什么会这样?
public class doPost extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
double lat = (Double) objects[0];
double longit = (Double) objects[1];
HttpClient httpclient = new DefaultHttpClient();
// HttpPost httppost = new HttpPost("http://192.168.1.110:3000/coord");
HttpPost httppost = new HttpPost(POSTURL);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("lat", Double.toString(lat)));
nameValuePairs.add(new BasicNameValuePair("long", Double.toString(longit)));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
}
答案 0 :(得分:1)
您正在进行从池后台线程到http客户端的同步调用:
HttpResponse response = httpclient.execute(httppost);
在服务器响应之前,此调用不会返回。在服务器响应之前,doInBackground
方法甚至不会到达您的return语句。如果在return
行添加断点,则可以很容易地看到在服务器响应之前不会返回返回值。