b4sxqg
Last Updated: February 25, 2016
·
4.834K
· chunallen

Android Asynchronous Http Client

I just want to share this snippet for requesting GET request using Android Asynchronous Http Client a Callback-Based Http Client Library for Android

Download the Library for Android Asynchronous Http Client

Move the downloaded library to your android project libs/ folder.

In your code import:

import com.loopj.android.http.*;

Here's the complete callbacks for GET request

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://google.com", new AsyncHttpResponseHandler() {
    @Override
    public void onStart() {
       //Start progress indicator here
    }

    @Override
    public void onSuccess(String response) {
       //Hide progress indicator
       System.out.println(response);   
    }

    @Override
    public void onFailure(Throwable error, String content)
    {
         error.printStackTrace();
    }

    @Override
    public void onRetry() {
        // Request was retried
    }

    @Override
    public void onProgress(int bytesWritten, int totalSize) {
        // Progress notification
    }

    @Override
    public void onFinish() {
        // Completed the request (either success or failure)
    }



}