Asynchronous HTTP Connection for Android - Library + Tutorial

Updated: 28 April 2013

Tested in:
HTC desire S (android 2.3.5), Samsung Galaxy Ace (android 2.3.4), Samsung Galaxy Tab P3100 (android 4.0.4), Samsung Galaxy Y - S5360 (android 2.3.6), Samsung Galaxy S3 (android 4.0.2)

Base on:
Eclipse Indigo build in 20120216-1857, Java 1.6, Android 4.1.2

Download Source code:
HttpConnection20130428.rar

Description:
This turorial includes HTTP Connection Library with many features:
- Best performance
- Errors handle (timeout, no network connection, ...)
- Easy to use
- Include POST and GET methods

Screen shot:
n/a

Tutorial:
1. GET method

AsyncHttpClient clientGet = new AsyncHttpClient();
clientGet.get(this, "http://developer.android.com/index.html", new AsyncHttpResponseHandler() {
 @Override
 public void onSuccess(int statusCode, String content) {
  Log.e("HttpConnection - onSuccess", content);
  super.onSuccess(statusCode, content);
 }
   
 @Override
 public void onFailure(Throwable error, String content) {
  Log.e("HttpConnection - onFailure", content);
  super.onFailure(error, content);
  }
 });

2. POST method
RequestParams requestParams = new RequestParams();
requestParams.put("username", "monopoly");
requestParams.put("password", "123456");
  
AsyncHttpClient clientPost = new AsyncHttpClient();
 clientPost.post(
  "http://yourdomain.com", 
  requestParams, 
  new AsyncHttpResponseHandler(){
  @Override
  public void onSuccess(int statusCode, String content) {
   super.onSuccess(statusCode, content);
  }
     
  @Override
  public void onFailure(Throwable error, String content) {
   super.onFailure(error, content);
  }
 });

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

Leave a Reply

Powered by Blogger.