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.