【转】Android应用中使用AsyncHttpClient来异步网络数据
摘要: 首先下载AsyncHttpClient的库文件,可以自行搜索,可以到下面地址下载 http://download.csdn.net/detail/xujinyang1234/5767419 测试的Activity,用到了RequstClient,LoadCacheResponseLogin...
首先下载AsyncHttpClient的库文件,可以自行搜索,可以到下面地址下载
http://download.csdn.net/detail/xujinyang1234/5767419
测试的Activity,用到了RequstClient,LoadCacheResponseLoginouthandler,LoadDatahandler三个重写的类
- package com.example.asynchttpclienttest;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.widget.TextView;
- import com.loopj.android.http.RequestParams;
- public class MainActivity extends Activity {
- private TextView test;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
- private void init() {
- test = (TextView) findViewById(R.id.test);
- System.out.println("拉拉");
- RequestParams param = new RequestParams();
- param.put("id", 1 + "");
- RequstClient.get(
- "http://58.192.23.75:8080/xiaoxiao2/ColumnServletAndroid",
- param, new LoadCacheResponseLoginouthandler(MainActivity.this,
- new LoadDatahandler() {
- @Override
- public void onStart() {
- super.onStart();
- test.setText("开始拉去数据");
- }
- @Override
- public void onSuccess(String data) {
- super.onSuccess(data);
- test.setText(data);
- System.out.println("拉倒的数据" + data);
- }
- @Override
- public void onFailure(String error, String message) {
- super.onFailure(error, message);
- test.setText("错误的数据" + message);
- }
- @Override
- public void onFinish() {
- super.onFinish();
- }
- }));
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
RequstClient.java
- package com.example.asynchttpclienttest;
- import com.loopj.android.http.AsyncHttpClient;
- import com.loopj.android.http.AsyncHttpResponseHandler;
- import com.loopj.android.http.RequestParams;
- public class RequstClient {
- /**
- * 定义一个异步网络客户端 默认超时未20秒 当超过,默认重连次数为5次 默认最大连接数为10个
- */
- private static AsyncHttpClient mClient = new AsyncHttpClient();
- static {
- mClient.setTimeout(20000);
- }
- public static void post(String url, AsyncHttpResponseHandler handler) {
- post(url, null, handler);
- }
- /**
- * post 请求
- *
- * @param url
- * API 地址
- * @param params
- * 请求的参数
- * @param handler
- * 数据加载句柄对象
- */
- public static void post(String url, RequestParams params,
- AsyncHttpResponseHandler handler) {
- System.out.println("进入post");
- mClient.post(url, params, handler);
- }
- public static void get(String url, AsyncHttpResponseHandler handler) {
- }
- public static void get(String url, RequestParams params,
- AsyncHttpResponseHandler handler) {
- System.out.println("进入get");
- mClient.get(url, params, handler);
- }
- }
- package com.example.asynchttpclienttest;
- import org.apache.http.Header;
- import android.content.Context;
- import com.loopj.android.http.AsyncHttpResponseHandler;
- public class LoadCacheResponseLoginouthandler extends AsyncHttpResponseHandler {
- private Context context;
- private LoadDatahandler mHandler;
- public LoadCacheResponseLoginouthandler(Context context,
- LoadDatahandler mHandler) {
- this.context = context;
- this.mHandler = mHandler;
- }
- @Override
- public void onStart() {
- super.onStart();
- mHandler.onStart();
- }
- @Override
- public void onFailure(Throwable error, String content) {
- super.onFailure(error, content);
- mHandler.onFailure("", "网络连接超时");
- }
- @Override
- public void onFinish() {
- super.onFinish();
- mHandler.onFinish();
- }
- @Override
- public void onSuccess(int statusCode, Header[] headers, String content) {
- super.onSuccess(statusCode, headers, content);
- System.out.println("得到的返回码" + statusCode);
- try {
- switch (statusCode) {
- case 200:
- mHandler.onSuccess(content);
- System.out.println("返回的内容" + content);
- break;
- case 401:
- onFailure("401", "没有登录");
- break;
- case 403:
- onFailure("404", "没有权限");
- break;
- default:
- break;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 出错
- *
- * @param error
- * @param errorMessage
- */
- public void onFailure(String error, String errorMessage) {
- if (errorMessage != null) {
- mHandler.onFailure(error, errorMessage);
- }
- }
- }
接口:
- package com.example.asynchttpclienttest;
- public class LoadDatahandler {
- /**
- * 加载数据时调用
- */
- public void onStart() {};
- /**
- * 加载数据调用,得到缓存数据
- * @param data
- */
- public void onLoadCaches(String data) {};
- /**
- * 当调用服务器接口成功获取数据时,调用这个方法
- * @param data
- */
- public void onSuccess(String data) {};
- /**
- * 当调用服务器接口获取数据失败时,调用这个方法
- * @param error 出错原因
- * @param message 出错原因描述
- */
- public void onFailure(String error, String message) {};
- /**
- * 加载完成时调用
- */
- public void onFinish() {};
- }
扩展: