其主要特征如下:
处理异步Http请求,并通过匿名内部类处理回调结果
Http请求均位于非UI线程,不会阻塞UI操作
通过线程池处理并发请求
处理文件上传、下载
响应结果自动打包JSON格式
自动处理连接断开时请求重连
使用android-async-http也非常简单,文章下面是下载jar地址,导入工程中libs文件夹下并添加到工程路径即可。通过下面的代码来创建一个异步请求:
AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.baidu.com", new AsyncHttpResponseHandler() { @Override public void onSuccess(String response) { System.out.println(response); textView.setText(response); } @Override public void onStart() { super.onStart(); System.out.println("onStart"); } @Override public void onFinish() { super.onFinish(); System.out.println("onFinish"); } }通过Get请求指定的URL并通过回调函数处理请求结果,同时,请求方式还支持POST和PUT,请求的同时还支持参数传递,下面看看如何通过JSON字符串作为参数访问服务器:
try { JSONObject jsonObject = new JSONObject(); jsonObject.put("username", "ryantang"); StringEntity stringEntity = new StringEntity(jsonObject.toString()); client.post(MainActivity.this, "http://api.com/login", stringEntity, "application/json", new JsonHttpResponseHandler(){ @Override public void onSuccess(JSONObject jsonObject) { super.onSuccess(jsonObject); } }); } catch (JSONException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }
官方推荐的使用方法,使用一个静态的请求对象。
public class HttpUtil { private static AsyncHttpClient client = new AsyncHttpClient(); // 实例话对象 static { client.setTimeout(10000); // 设置链接超时,如果不设置,默认为10s } // 用一个完整url获取一个string对象 public static void get(String urlString, AsyncHttpResponseHandler res) { client.get(urlString, res); } // url里面带参数 public static void get(String urlString, RequestParams params, AsyncHttpResponseHandler res){ client.get(urlString, params, res); } // 不带参数,获取json对象或者数组 public static void get(String urlString, JsonHttpResponseHandler res) { client.get(urlString, res); } // 带参数,获取json对象或者数组 public static void get(String urlString, RequestParams params, JsonHttpResponseHandler res) { client.get(urlString, params, res); } // 下载数据使用,会返回byte数据 public static void get(String uString, BinaryHttpResponseHandler bHandler) { client.get(uString, bHandler); } public static AsyncHttpClient getClient(){ return client; } }
下载:
public void downloadClick(View view) { String url = "http://f.hiphotos.baidu.com/album/w%3D2048/sign=38c43ff7902397ddd6799f046dbab3b7/9c16fdfaaf51f3dee973bf7495eef01f3b2979d8.jpg"; HttpUtil.get(url, new BinaryHttpResponseHandler() { @Override public void onSuccess(byte[] arg0) { super.onSuccess(arg0); File file = Environment.getExternalStorageDirectory(); File file2 = new File(file, "cat"); file2.mkdir(); file2 = new File(file2, "cat.jpg"); try { FileOutputStream oStream = new FileOutputStream(file2); oStream.write(arg0); oStream.flush(); oStream.close(); textView.setText("可爱的猫咪已经保存在sdcard里面"); } catch (Exception e) { e.printStackTrace(); Log.i("hck", e.toString()); } } }); }上传:
public void uploadClick(View view){ String path="http://10.0.2.2:8080/jsontest/servlet/UploadServlet"; File myFile = new File("/sdcard/cat/cat.jpg"); RequestParams params = new RequestParams(); try { params.put("filename", myFile); AsyncHttpClient client = new AsyncHttpClient(); client.post(path, params, new AsyncHttpResponseHandler(){ @Override public void onSuccess(int statusCode, String content) { // TODO Auto-generated method stub super.onSuccess(statusCode, content); Toast.makeText(MainActivity.this, "上传成功!", Toast.LENGTH_LONG).show(); } }); } catch(FileNotFoundException e) { } }
由于涉及网络请求,最后别忘了添加权限:
<uses-permission android:name="android.permission.INTERNET" />