当并行运行2个http AsyncTasks时,有时会交换结果

时间:2013-01-03 08:14:11

标签: android android-asynctask

我的申请中有一个奇怪的问题。

我有一个使用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数据 反之亦然。

0 个答案:

没有答案