Search code examples
androidhttploopj

Associating responses with specific requests using Loopj Android Asynchronous Http Client


I'm using The Loopj Android Asynchronous HTTP Client to send multiple HTTP requests asynchronously.

I'm using a static AsyncHttpClient as suggested and sending multiple HTTP posts and receiving responses on an anonymous class. The problem is that when a request comes back I don't know how to tie it back to the original request.

For instance, in a caching situation, when I send a post and receive a 200 OK I need to be able to know which request that response is for so I can mark it as successfully sent.


Solution

  • Try this:

    public class MyAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
    
        private String requestId;
    
        public AsyncHttpResponseHandler(String requestId) {
            this.requestId = requestId;
        }
    
        @Override
        public void onSuccess(String arg0)
        {
            super.onSuccess(arg0);
            // Use requestId here
        }
    }
    

    Sending request:

    client.get(url, new MyAsyncHttpResponseHandler(requestId))