2

I'm using this library to request from my web services. It didn't response at all in onSuccess and onFailure sometimes (these are the only two methods I overrides). I tested under 1.4.4 and 1.4.5 (android-async-http-1.4.5-20131110.125018-1, this one is better, but still encounter the problem sometimes). I'm sure it's not the network problem, because my ios app never encounter this problem from the same web services. And I can get response when I refresh immediately after this problem occurs.

Here is my code:

In requester.java

public class Requester 
{

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {

    AsyncHttpClient client = newClient();
    client.get(getAbsoluteUrl(url), params, responseHandler);
}

public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {

    AsyncHttpClient client = newClient();
    client.post(getAbsoluteUrl(url), params, responseHandler);
}

private static String getAbsoluteUrl(String relativeUrl) {
    return Settings.BASE_URL + relativeUrl;
}

private static AsyncHttpClient newClient()
{
    AsyncHttpClient client = new AsyncHttpClient();
    client.setMaxRetriesAndTimeout(Settings.HTTP_TIMEOUT,Settings.HTTP_RETRIES);
    return client;
}
}

In my activity who's making http request:

    Requester.get(urlRequest, null, new JsonHttpResponseHandler()
    {
        @Override
        public void onSuccess(int statusCode, org.apache.http.Header[] headers, org.json.JSONArray objects)
        {
            Logger.logDebug(TAG, "request success for " + " " + objects.length() + " objects");
        }

        @Override
        public void onFailure(int statusCode, org.apache.http.Header[] headers, java.lang.Throwable throwable, org.json.JSONArray errorResponse)
        {
            Logger.logError(TAG,"Failed to request");
        }

    });

I'm using the similar source in a few projects. But all have the same problem. I don't know it's the problem of my code or the android-async-http library. Can anybody help? Thanks.

By the way, I'm normally making 3 requests at the same time by using the same method as the above mentioned source code but with different url.

secretformula
  • 6,414
  • 3
  • 33
  • 56
Bagusflyer
  • 12,675
  • 21
  • 96
  • 179

2 Answers2

2

I confirmed that the problem is related to the multiple requests. My solution is to replace with another library: Volley library. Problem solved!

Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
1

I encounter this problem as well and upon further investigation I realized that if the json response received is not in proper json format or if the response from the server contains any extra characters you will not get a callback method being called, although the post or get would have been processed the callbacks will not be triggered. Check your response from the server to make sure.

kabuto178
  • 3,129
  • 3
  • 40
  • 61
  • Thanks for your reply. But it may not be my problem. Because some of the json data I got from server always the same. But the problem is intermittent. And I can almost 100% get data back by resend the same request again if I encountered the no response problem. – Bagusflyer Mar 26 '14 at 09:37
  • By the way, where is the place I can get the raw data before the callback? So that I can monitor it. – Bagusflyer Mar 26 '14 at 09:39
  • Thats the thing, once your using the JsonResponseHandler if anything is not "perfect" json data that is returned you will get no callback, however if you use the AsyncHttpResponse call back I think there is an option for success that gives you a string output where you could see what was returned. – kabuto178 Mar 26 '14 at 10:52
  • Ok, I'll have a try on AsyncHttpResponse. But I think might be related to multiple requests at the same time. Because it was fine when I only have one request during the test just now. (I request more than 100 times and there is no problem at all.) What is the best way to have multiple requests? – Bagusflyer Mar 26 '14 at 11:49
  • Hmm, multiple requests meaning what exactly? – kabuto178 Mar 26 '14 at 12:16
  • Means a few request will be made almost at the sometime. Take my code as an example, a few Requester.get() was called at the sometime. – Bagusflyer Mar 27 '14 at 10:27