-1

I want to use this awesome library for my server request in my application to do some work on map. My need is I want to make server request for every 2 minutes without block user interaction on the screen while server requesting process happening and in the onSuccess call back I'll do some work on the map. How can I use this library for my need? and the library coupled with Android activity and fragment life cycle?

Please refer this site for the library information. http://loopj.com/android-async-http/

M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

1 Answers1

2

Actually that's is from the page you linked http://loopj.com/android-async-http/ . Theses callbacks are executed in the UI thread but the actual request is being processed in another thread.

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

    @Override
    public void onStart() {
        // called before request is started
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        // called when response HTTP status is "200 OK"
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
        // called when response HTTP status is "4XX" (eg. 401, 403, 404)
    }

    @Override
    public void onRetry(int retryNo) {
        // called when request is retried
    }
});

In your Activity's onStop() you should call AsyncHttpClient's cancelAllRequests(Context context, boolean mayInterruptIfRunning) method to cancel all the active and pending requests.

client.cancelAllRequests(this, true);

This should ensure that the callbacks of your requests are never called and then save yourself from non trivial network usage and save yourself from IllegalStateExceptions.

Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64