我的应用程序中有一个奇怪的问题。
我有一个使用 AsyncTasks 并行获取 2 或 3 个东西的活动
当我简单地执行以下操作时
new getMessages().execute("someID");
new getNotifications().execute("someID");
并且两个 AsyncTasks 的代码如下:
(除了每个方法请求的 URL 不同之外,它们都是相同的)。
注意:我稍微修改了这段代码,只是为了删除任何不需要的额外内容,比如在 http 请求中发送的额外参数
// in the other Async task "notifications" is changed with "messages"
public class getNotifications extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
Integer verified = 0;
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... args) {
// getRequestsCount just perfomrs http request and grabs JSON data
String result = getRequestsCount(args[0] , "notifications");
return result;
}
protected void onPostExecute(String result) {
// This is just a method that handles the result
// When I log result I found that results are exchanged.
displayResults(result);
}
}
public String getRequestsCount(String id, String type){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost(GlobalSettings.apiURL + "/getcount/" + type );
Log.i("will contact",GlobalSettings.apiURL + "/getcount/" + type);
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));
String responseMessage = reader.readLine().toString().trim();
// Response is always 1 line of JSON data
Log.i("Response for "+type,responseMessage);
return responseMessage;
}
现在我的问题是有时会交换结果。
即 getMessages 接收从 getNotifications 请求的 JSON 数据,反之亦然。