async-http-client库是一个基于回调函数的Http异步通信客户端Android组件,是在Apache的HttpClient库的基础上开发构建而成的。
Eclipse使用:导入android-async-http-1.4.4.jar 包, 点击下载
AndroidStudio: gradle中引入 compile 'com.loopj.android:android-async-http:1.4.8'
功能特色
- 利用版4.3.6上游HttpClient代替Android提供defaulthttpclient
- 兼容AndroidAPI 23高
- 做异步HTTP请求处理的响应匿名回调
- HTTP请求发生UI线程之外
- 请求使用线程池限制并发资源使用情况
- get /后参数生成器( RequestParams )
- 多文件上传没有额外的第三方库
- JSON上传流没有额外的图书馆
- 处理循环和相对重定向
- 小的开销给你的应用程序只90kb一切
- 自动智能请求重试次数质量不一的移动连接优化
- 自动gzip响应解码速度超快的请求支持
- 二进制协议通信
binaryhttpresponsehandler
- 内置的响应分析JSON与
jsonhttpresponsehandler
- 节能反应直接进入文件
fileasynchttpresponsehandler
- 大的持久性Cookie,保存cookie到你的应用程序的SharedPreferences
- 杰克逊JSON集成,gson或其他JSON序列化库(德)
basejsonhttpresponsehandler
- 与SAX解析器支持
saxasynchttpresponsehandler
- 语言和内容编码的支持,不仅仅是UTF-8
效果图:
1 public class MainActivity extends Activity implements OnClickListener { 2 3 public static AsyncHttpClient mHttpc = new AsyncHttpClient(); 4 5 private TextView mTextView; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_asynchttpclict); 11 initView(); 12 } 13 14 private void initView() { 15 findViewById(R.id.btn1).setOnClickListener(this); 16 findViewById(R.id.btn2).setOnClickListener(this); 17 findViewById(R.id.btn3).setOnClickListener(this); 18 findViewById(R.id.btn4).setOnClickListener(this); 19 mTextView=(TextView) findViewById(R.id.Text); 20 } 21 22 @Override 23 public void onClick(View v) { 24 switch (v.getId()) { 25 case R.id.btn1: 26 showHttpGet1(); 27 break; 28 case R.id.btn2: 29 showHttpGet2("https://www.baidu.com"); 30 break; 31 case R.id.btn3: 32 showHttpGet3(); 33 break; 34 case R.id.btn4: 35 showHttpPost(); 36 break; 37 case R.id.btn5: 38 39 case R.id.btn6: 40 41 default: 42 break; 43 } 44 45 } 46 47 private void showHttpGet1() { 48 AsyncHttpClient client = new AsyncHttpClient(); 49 client.get("https://www.baidu.com", new AsyncHttpResponseHandler() { 50 @Override 51 public void onStart() { // 请求启动 请求前 52 } 53 54 @Override 55 public void onSuccess(int statusCode, Header[] headers, 56 byte[] responseBody) { // 请求成功 57 StringBuffer result = new StringBuffer(""); 58 int length = responseBody.length; 59 for (int i = 0; i < length; i++) { 60 result.append((char) (responseBody[i] & 0xff)); 61 } 62 Toast.makeText(MainActivity.this, "结果:" + result.toString(), 2) 63 .show(); 64 mTextView.setText("结果:" +result.toString()); 65 } 66 67 @Override 68 public void onFailure(int statusCode, Header[] headers, 69 byte[] responseBody, Throwable error) // 请求失败 70 { 71 72 } 73 74 public void onRetry() { // 重试 75 76 } 77 78 @Override 79 public void onProgress(int bytesWritten, int totalSize) { // 请求进度 80 81 } 82 83 @Override 84 public void onFinish() { // 请求完成 85 86 } 87 }); 88 } 89 90 public void showHttpGet2(String uri) { 91 mHttpc.get(uri, null, new AsyncHttpResponseHandler() { // 请求失败 92 @Override 93 public void onFailure(int arg0, Header[] arg1, byte[] arg2, 94 Throwable arg3) { 95 } 96 97 @Override 98 public void onSuccess(int arg0, Header[] arg1, 99 byte[] responseBody) { 100 StringBuffer result = new StringBuffer(""); 101 int length = responseBody.length; 102 for (int i = 0; i < length; i++) { 103 result.append((char) (responseBody[i] & 0xff)); 104 } 105 Toast.makeText(MainActivity.this, 106 "结果:" + result.toString(), 2).show(); 107 mTextView.setText("结果:" +result.toString()); 108 } 109 }); 110 } 111 112 private void showHttpGet3() { 113 AsyncHttpClient client = new AsyncHttpClient(); 114 RequestParams params = new RequestParams(); 115 params.put("q", "test"); 116 params.put("showapi_appid", "11548"); 117 // 当前时间 118 params.put("showapi_timestamp", "20160511151954"); 119 params.put("showapi_sign", "bb1d15ab7ce646ec87cc89d684ca4bcb"); 120 client.get("https://route.showapi.com/32-9", params, 121 new TextHttpResponseHandler() { 122 @Override 123 public void onSuccess(int statusCode, Header[] headers, 124 String response) { 125 Toast.makeText(MainActivity.this, 126 "结果:" + response.toString(), 2).show(); 127 mTextView.setText("结果:" +response.toString()); 128 } 129 130 @Override 131 public void onFailure(int statusCode, Header[] headers, 132 String responseBody, Throwable error) { 133 Log.i("ERROR", error.toString()); 134 } 135 }); 136 } 137 138 /* 139 * 获得字符串 140 */ 141 public void showHttpPost() { 142 AsyncHttpClient client = new AsyncHttpClient(); 143 RequestParams params = new RequestParams(); 144 params.put("q", "test"); 145 params.put("showapi_appid", "11548"); 146 // 当前时间 147 params.put("showapi_timestamp", "20160511151954"); 148 params.put("showapi_sign", "bb1d15ab7ce646ec87cc89d684ca4bcb"); 149 client.post("https://route.showapi.com/32-9", params, 150 new AsyncHttpResponseHandler() { 151 152 @Override 153 public void onFailure(int arg0, Header[] arg1, 154 byte[] responseBody, Throwable arg3) { 155 156 } 157 158 @Override 159 public void onSuccess(int arg0, Header[] arg1, 160 byte[] responseBody) { 161 StringBuffer result = new StringBuffer(""); 162 int length = responseBody.length; 163 for (int i = 0; i < length; i++) { 164 result.append((char) (responseBody[i] & 0xff)); 165 } 166 Toast.makeText(MainActivity.this, 167 "结果:" + result.toString(), 2).show(); 168 mTextView.setText("结果:" +result.toString()); 169 } 170 }); 171 172 } 173 174 175 176 /** 177 * 上传单个文件 178 */ 179 private void showFile() { 180 File myFile = new File("filePath");// filePath--->文件路径 181 RequestParams params = new RequestParams(); 182 try { 183 params.put("time", "20160511151954"); 184 params.put("sign", "bb1d15ab7ce646ec87cc89d684ca4bcb"); 185 params.put("filename", myFile); 186 AsyncHttpClient client = new AsyncHttpClient(); 187 client.post("url", params, new AsyncHttpResponseHandler() { 188 189 @Override 190 public void onFailure(int arg0, Header[] arg1, byte[] arg2, 191 Throwable arg3) { 192 193 } 194 195 @Override 196 public void onSuccess(int arg0, Header[] arg1, byte[] arg2) { 197 198 } 199 }); 200 } catch (FileNotFoundException e) { 201 e.printStackTrace(); 202 } 203 } 204 /*** 205 * 多个文件上传 206 * 207 * @param sendFilesPath 208 */ 209 public void uploadFile(ArrayList<String> sendFilesPath) { 210 if (sendFilesPath.size() == 0) 211 return; 212 213 String strUploadFile = ""; 214 AsyncHttpClient client = new AsyncHttpClient(); 215 client.setURLEncodingEnabled(false); 216 217 RequestParams params = new RequestParams(); 218 params.put("time", "20160511151954"); 219 params.put("sign", "bb1d15ab7ce646ec87cc89d684ca4bcb"); 220 // 批量上传 221 for (int i = 0; i < sendFilesPath.size(); i++) { 222 File myFile = new File(sendFilesPath.get(i)); 223 try { 224 params.put(myFile.getName(), myFile); 225 } catch (FileNotFoundException e1) { 226 continue; 227 } 228 } 229 230 client.setTimeout(10000); 231 client.post(strUploadFile, params, new AsyncHttpResponseHandler() { 232 233 @Override 234 public void onFailure(int statusCode, Header[] headers, 235 byte[] responseBody, Throwable arg3) { 236 Log.i("Show", "上传失败"); 237 } 238 239 @Override 240 public void onSuccess(int statusCode, Header[] headers, 241 byte[] responseBody) { 242 Log.i("Show", "上传成功"); 243 } 244 245 @Override 246 public void onProgress(int bytesWritten, int totalSize) { 247 super.onProgress(bytesWritten, totalSize); 248 int count = (int) ((bytesWritten * 1.0 / totalSize) * 100); 249 // 上传进度显示 250 Log.i("Show", "上传进度显示:" + count); 251 252 } 253 254 @Override 255 public void onRetry(int retryNo) { 256 super.onRetry(retryNo); 257 // 返回重试次数 258 } 259 }); 260 } 261 262 }
记得加网络权限
<uses-permission android:name="android.permission.INTERNET"/>