Two AsyncTasks running in parallel while requesting with HttpClient

1

I have two classes extending AsyncTask, in which they return a JSON from a server. I'm using the following code to run them simultaneously:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    AsyncTaskExemplo1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");
else
    contact.execute("");

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    AsyncTaskExemplo2.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "");
else
    contact.execute("");

I use the following code to create my connection:

public static HttpClient httpclient = new DefaultHttpClient();
public JSONObject GET(String url, JSONObject jsonObject) {
    InputStream inputStream = null;
    String result = "";
    JSONObject jArray = null;
    try {
        HttpGet httpGet = new HttpGet(url);
        if (jsonObject != null) {
            String json = "";
            json = jsonObject.toString();
            StringEntity se = new StringEntity(json);
            ((HttpResponse) httpGet).setEntity(se);
        }
        httpGet.setHeader("Accept", "application/json");
        httpGet.setHeader("Content-type", "application/json");
        HttpResponse httpResponse = httpclient.execute(httpGet);
        inputStream = httpResponse.getEntity().getContent();
        if (inputStream != null) {
            result = convertInputStreamToString(inputStream);
        } else {
            result = "Did not work!";
        }
    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    if (result != null) {
        try {
            jArray = new JSONObject(result);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return jArray;
}

When I run AsyncTask one after the other, they return the JSONs correctly, however, if I execute them simultaneously, ONE of them does not wait for the server to return and continues execution returning null in the 'GET' function of my code. >

I've read that Android does not support two simultaneous HTTP requests with the standard Apache HttpClient library.

Please, if someone has already faced a similar situation and can help me, I am very grateful.

    
asked by anonymous 20.02.2014 / 21:37

1 answer

2

According to the Android documentation

  

public end AsyncTask execute (Params ... params)

     

Note: this function schedules the task on a single thread background or pool of threads depending on the platform version. When first introduced, AsyncTasks were serially executed on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution. If you really want parallel execution, you can use the executeOnExecutor (Executor, Params ...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.

In this way, you can not run more than one AsyncTask in parallel. Asynctasks are used to facilitate the use of Threads, since they allow modifications in the UI. However, it is possible to achieve the same effect using conventional threads along with some mechanism of communication with the main thread. The android documentation presents a good tutorial on the subject in the link link

    
25.03.2014 / 13:47