Install an Android Asynchronous Http Client

Install an Android Asynchronous Http Client



Android Asynchronous Http Client its a Library that allow us get data from a web server, this library it should be install into Android App.


For this example I'm using a new project App.

For install follow this steps.

1.- Select the Android option into your project




2.- Open this file into your Grandle.



3.- Add this line into de dependencies secction. 



This will install this library, is necesary Sync.

5.- You will see some like this in your log. 





6.-  Import class. Now you can import the class into your MainActivity or another.


import com.loopj.android.http.*;

7.- Add Permisson to Add permission for Access to internet.

Open the file "AndroidManifiest.xml" and add this line


<uses-permission android:name="android.permission.INTERNET" />

Like this image




8.- Test a simple request


In the main activity just page this. 

You will need import this clases:





            // ------ Simple call for test
            AsyncHttpClient client = new AsyncHttpClient();
            client.get("https://www.google.com", new AsyncHttpResponseHandler() {

                @Override
                public void onStart() {
                    Log.w("##TAG_URL_REQUEST##", "On start" );
                    // called before request is started
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error){
                    Log.w("##TAG_URL_REQUEST##", "On fail" );
                };

                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody){
                    Log.w("##TAG_URL_REQUEST##", "On onSuccess" );
                };

                @Override
                public void onRetry(int retryNo) {
                    Log.w("##TAG_URL_REQUEST##", "On retry" );
                    // called when request is retried
                }
            });





The results  should be:



           
W/##TAG_URL_REQUEST##: On start
W/##TAG_URL_REQUEST##: On onSuccess


This mean that firts call to start and when end the request call to onSucess method.




Reference:

 https://loopj.com/android-async-http/









Comments

Popular posts from this blog

Retriving Android API version by code

Add image from URL