0

I am using Loopj to get a request and response. But it seems that callback method is not called properly if internet connection with no data. When I click a listview its execute below code:

AsyncHttpClient client = new AsyncHttpClient();
            client.get("http://www.google.com", new AsyncHttpResponseHandler() {

                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    System.out.println("onSuccess");
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                    System.out.println("onFailure");
                }

            });

Problem is that if internet connected but with no data then onFailure() is not calling even after timeout settings.

I have tried with below timeout settings in client:

client.setTimeout(5000);

or

client.setConnectTimeout(5000);

or

client.setResponseTimeout(5000);

or

client.setMaxRetriesAndTimeout(5,1000);

None of these forcing to call onFailure(). My question is there any way to call onFailure() if internet connection with no data or any other callback available in Loopj which fired after timeout?

0xAliHn
  • 18,390
  • 23
  • 91
  • 111

1 Answers1

0

There are other callback for AsyncHttpClient, the most important are

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://www.google.com", new AsyncHttpResponseHandler() {

        @Override
        public void onStart() {
            super.onStart();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            System.out.println("onSuccess");
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            System.out.println("onFailure");
        }

        @Override
        public void onFinish() {
            super.onFinish();
            System.out.println("onFinish");
        }
    });

In your case you can use the onFinish() callback which will give the end of the wed request even if its success or failure..

Hope this helps

Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49
  • I have tried but onFInish() is not called when internet connection with no data. Its keeps requesting until internet data availability. – 0xAliHn Sep 03 '16 at 07:09
  • 1
    Keeps requesting? Didn't get you..Can you please explain more. Or post screen shot or something? @0xAliHn – Sanoop Surendran Sep 03 '16 at 07:10
  • I mean onFinish() called after either onSuccess() or OnFailure(). In my case internet connection with no data both onSuccess() and onFailure() not getting fired. So, onFinish() also not getting called. – 0xAliHn Sep 03 '16 at 07:14
  • Ok.. I think if you are using `client.setMaxRetriesAndTimeout(5,1000);` then you can override `onRetry()` callback and do you task if retry count == 4 or your appropriate condition and cancel the request by calling ` client.cancelAllRequests(true);` @0xAliHn – Sanoop Surendran Sep 03 '16 at 07:19
  • if you need help with this suggestion, let me know, i will edit my answer @0xAliHn – Sanoop Surendran Sep 03 '16 at 07:22
  • Strange..onRetry() method also not calling after setMaxRetriesAndTimeout(5,1000). – 0xAliHn Sep 03 '16 at 07:31