Android应用中使用AsyncHttpClient来异步网络数据

首先下载AsyncHttpClient的库文件,可以自行搜索,可以到下面地址下载

http://download.csdn.net/detail/xujinyang1234/5767419

测试的Activity,用到了RequstClient,LoadCacheResponseLoginouthandler,LoadDatahandler三个重写的类

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. package com.example.asynchttpclienttest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.widget.TextView;  
  7.   
  8. import com.loopj.android.http.RequestParams;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     private TextView test;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         init();  
  18.     }  
  19.   
  20.     private void init() {  
  21.         test = (TextView) findViewById(R.id.test);  
  22.         System.out.println("拉拉");  
  23.         RequestParams param = new RequestParams();  
  24.         param.put("id"1 + "");  
  25.         RequstClient.get(  
  26.                 "http://58.192.23.75:8080/xiaoxiao2/ColumnServletAndroid",  
  27.                 param, new LoadCacheResponseLoginouthandler(MainActivity.this,  
  28.                         new LoadDatahandler() {  
  29.                             @Override  
  30.                             public void onStart() {  
  31.                                 super.onStart();  
  32.                                 test.setText("开始拉去数据");  
  33.                             }  
  34.   
  35.                             @Override  
  36.                             public void onSuccess(String data) {  
  37.                                 super.onSuccess(data);  
  38.                                 test.setText(data);  
  39.                                 System.out.println("拉倒的数据" + data);  
  40.                             }  
  41.   
  42.                             @Override  
  43.                             public void onFailure(String error, String message) {  
  44.                                 super.onFailure(error, message);  
  45.                                 test.setText("错误的数据" + message);  
  46.                             }  
  47.   
  48.                             @Override  
  49.                             public void onFinish() {  
  50.                                 super.onFinish();  
  51.                             }  
  52.                         }));  
  53.     }  
  54.   
  55.     @Override  
  56.     public boolean onCreateOptionsMenu(Menu menu) {  
  57.         getMenuInflater().inflate(R.menu.main, menu);  
  58.         return true;  
  59.     }  
  60.   
  61. }  

RequstClient.java

 

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. package com.example.asynchttpclienttest;  
  2.   
  3. import com.loopj.android.http.AsyncHttpClient;  
  4. import com.loopj.android.http.AsyncHttpResponseHandler;  
  5. import com.loopj.android.http.RequestParams;  
  6.   
  7. public class RequstClient {  
  8.     /** 
  9.      * 定义一个异步网络客户端 默认超时未20秒 当超过,默认重连次数为5次 默认最大连接数为10个 
  10.      */  
  11.     private static AsyncHttpClient mClient = new AsyncHttpClient();  
  12.     static {  
  13.         mClient.setTimeout(20000);  
  14.     }  
  15.     public static void post(String url, AsyncHttpResponseHandler handler) {  
  16.         post(url, null, handler);  
  17.     }  
  18.     /** 
  19.      * post 请求 
  20.      *  
  21.      * @param url 
  22.      *            API 地址 
  23.      * @param params 
  24.      *            请求的参数 
  25.      * @param handler 
  26.      *            数据加载句柄对象 
  27.      */  
  28.     public static void post(String url, RequestParams params,  
  29.             AsyncHttpResponseHandler handler) {  
  30.         System.out.println("进入post");  
  31.         mClient.post(url, params, handler);  
  32.     }  
  33.     public static void get(String url, AsyncHttpResponseHandler handler) {  
  34.   
  35.     }  
  36.     public static void get(String url, RequestParams params,  
  37.             AsyncHttpResponseHandler handler) {  
  38.         System.out.println("进入get");  
  39.         mClient.get(url, params, handler);  
  40.     }  
  41. }  

 

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. package com.example.asynchttpclienttest;  
  2.   
  3. import org.apache.http.Header;  
  4.   
  5. import android.content.Context;  
  6.   
  7. import com.loopj.android.http.AsyncHttpResponseHandler;  
  8.   
  9. public class LoadCacheResponseLoginouthandler extends AsyncHttpResponseHandler {  
  10.     private Context context;  
  11.     private LoadDatahandler mHandler;  
  12.   
  13.     public LoadCacheResponseLoginouthandler(Context context,  
  14.             LoadDatahandler mHandler) {  
  15.         this.context = context;  
  16.         this.mHandler = mHandler;  
  17.     }  
  18.     @Override  
  19.     public void onStart() {  
  20.         super.onStart();  
  21.         mHandler.onStart();  
  22.     }  
  23.     @Override  
  24.     public void onFailure(Throwable error, String content) {  
  25.         super.onFailure(error, content);  
  26.         mHandler.onFailure("""网络连接超时");  
  27.     }  
  28.     @Override  
  29.     public void onFinish() {  
  30.         super.onFinish();  
  31.         mHandler.onFinish();  
  32.     }  
  33.   
  34.     @Override  
  35.     public void onSuccess(int statusCode, Header[] headers, String content) {  
  36.         super.onSuccess(statusCode, headers, content);  
  37.         System.out.println("得到的返回码" + statusCode);  
  38.         try {  
  39.             switch (statusCode) {  
  40.             case 200:  
  41.                 mHandler.onSuccess(content);  
  42.                 System.out.println("返回的内容" + content);  
  43.                 break;  
  44.             case 401:  
  45.                 onFailure("401""没有登录");  
  46.   
  47.                 break;  
  48.             case 403:  
  49.                 onFailure("404""没有权限");  
  50.                 break;  
  51.             default:  
  52.                 break;  
  53.             }  
  54.   
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.   
  60.     /** 
  61.      * 出错 
  62.      *  
  63.      * @param error 
  64.      * @param errorMessage 
  65.      */  
  66.     public void onFailure(String error, String errorMessage) {  
  67.         if (errorMessage != null) {  
  68.             mHandler.onFailure(error, errorMessage);  
  69.         }  
  70.     }  
  71. }  



 

 

接口:

 

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  1. package com.example.asynchttpclienttest;  
  2.   
  3. public class LoadDatahandler {  
  4.     /** 
  5.      * 加载数据时调用 
  6.      */  
  7.     public void onStart() {};  
  8.     /** 
  9.      * 加载数据调用,得到缓存数据 
  10.      * @param data 
  11.      */  
  12.     public void onLoadCaches(String data) {};  
  13.     /** 
  14.      * 当调用服务器接口成功获取数据时,调用这个方法 
  15.      * @param data 
  16.      */  
  17.     public void onSuccess(String data) {};  
  18.       
  19.     /** 
  20.      * 当调用服务器接口获取数据失败时,调用这个方法 
  21.      * @param error     出错原因 
  22.      * @param message   出错原因描述 
  23.      */  
  24.     public void onFailure(String error, String message) {};  
  25.     /** 
  26.      * 加载完成时调用 
  27.      */  
  28.     public void onFinish() {};  
  29.   
  30. }  

扩展:

Android在AsyncHttpClient框架的基础上定制能直接返回对象数组的框架

 

转自:http://blog.csdn.net/mobilexu/article/details/9350467

猜你喜欢

转载自chriszeng87.iteye.com/blog/2009137