Android Asynchronous Http Client

Posted By : Akash Tomer | 28-Feb-2018

Android Asynchronous Http Client, which is also called Loopz is a networking library for Android. Although there a lot of networking library like Volley and Retrofit. But, I used Loopz because of it easy implementation and small in size. It can handle the request on another thread not on UI thread. It can handle both requests get and post. It can upload the multipart file with no additional library, smart retries, store cookies, easily integrate with another third party library like Gson and saving response directly in file.

First of all, we have to add the dependency in the gradle file 

dependencies {
  compile 'com.loopj.android:android-async-http:1.4.9'
}

Now create a hhtp client object and make request as below
 

AsyncHttpClient client = new AsyncHttpClient();
client.get("url", 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 this, the onStart() method will be called before the invocation of the get or post request. The onSuccess() method is called when you request is successfully received by the server and server will response according to your request. The onFailure() method will be called when the server did not receive your request and onRetry() method will be called when it retry a request to the server. In the request, we just have to pass the URL and the response handler object.

If you want to request with some parameter you can do like this

RequestParams params = new RequestParams();
params.put("key1", "value");
params.put("key2", "data");

Right now this library is used by Instagram,PinIntrest,Pose etc.

Hope this will help you.

 

 

About Author

Author Image
Akash Tomer

Akash is an Android Developer at Oodles Technology.

Request for Proposal

Name is required

Comment is required

Sending message..