0
votes

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?

1

1 Answers

0
votes

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