浅论Android网络请求库——android-async-http
在iOS开发中有大名鼎鼎的ASIHttpRequest库,用来处理网络请求操作,今天要介绍的是一个在Android上同样强大的网络请求库android-async-http,目前非常火的应用Instagram和Pinterest的Android版就是用的这个网络请求库。这个网络请求库是基于Apache HttpClient库之上的一个异步网络请求处理库,网络处理均基于Android的非UI线程,通过回调方法处理请求结果。
其主要特征如下:处理异步Http请求,并通过匿名内部类处理回调结果,Http异步请求均位于非UI线程,不会阻塞UI操作,通过线程池处理并发请求处理文件上传、下载,响应结果自动打包JSON格式. 自动处理连接断开时请求重连.使用android-async-http也非常简单,到官网http://loopj.com/android-async-http/下载依赖jar包,导入工程中libs文件夹下并添加到工程路径即可。通过下面的代码来创建一个异步请求: 首先,我们分析一下相应核心的操作类。AsyncHttpResponseHandler ——这是一个请求返回处理 成功 失败 开始 完成 等自定义的消息的类
BinaryHttpResponseHandler extends AsyncHttpResponseHandler ——继承AsyncHttpResponseHandler的子类,这是一个字节流返回处理的类, 该类用于处理图片等类。
JsonHttpResponseHandler extends AsyncHttpResponseHandler ——继承AsyncHttpResponseHandler的子类,这是一个json请求返回处理服务器与客户端用json交流时使用的类.
AsyncHttpRequest implements Runnable ——基于线程的子类,用于 异步请求类, 通过AsyncHttpResponseHandler回调。
PersistentCookieStore implements CookieStore ——这是一个基于CookieStore的子类,使用HttpClient处理数据,并且使用cookie持久性存储接口。
RequestParams ——封装了参数处理 例如:
* RequestParams params = new RequestParams();* params.put("username", "james");* params.put("password", "123456");* params.put("email", "[email protected]");* params.put("profile_picture", new File("pic.jpg")); // Upload a File* params.put("profile_picture2", someInputStream); // Upload an InputStream* params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes** AsyncHttpClient client = new AsyncHttpClient();
接下来核心类。
RetryHandler implements HttpRequestRetryHandler——这是一个多个线程同步处理的类
SerializableCookie implements Serializable——这是操作cookie 放入/取出数据的类
SimpleMultipartEntity implements HttpEntity——用于处理多个请求实体封装
SyncHttpClient extends AsyncHttpClient——同步客户端请求的类
AsyncHttpClient——异步客户端请求的类
介绍了这些核心类之后,我们主要看看他的用法:
这是普通get方式来返回相应字符串的代码:
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"); } }
同时,请求方式还支持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(); }
除此之外,还能够支持相应文件图片上传的话。相应的源代码如下:
String path="http://sv1.livechano.com:8080/upload.action?&action=1.6&type=1&ext=png"; File myFile = new File("/sdcard/test.png"); RequestParams params = new RequestParams(); try { params.put("image", myFile,"application/octet-stream"); AsyncHttpClient client = new AsyncHttpClient(); client.post(path, params, new AsyncHttpResponseHandler(){ @Override public void onFailure(Throwable error, String content) { // TODO Auto-generated method stub super.onFailure(error, content); Toast.makeText(MainActivity.this, "上传失败!"+content, Toast.LENGTH_LONG).show(); } @Override public void onSuccess(int statusCode, String content) { // TODO Auto-generated method stub super.onSuccess(statusCode, content); System.out .println("content: "+content); Toast.makeText(MainActivity.this, "上传成功!"+content, Toast.LENGTH_LONG).show(); } }); } catch(FileNotFoundException e) { }
注意了,这种方法上传的参数一定要设置params.put("image", myFile,"application/octet-stream");否则就会失败。
当然,android-async-http还有很多用法,这里不做过多赘述了。希望android-async-http能够大家以后android的请求模块得到帮助。
更多相关文章
- [置顶] Android事件总线还能怎么玩?
- Android更新UI的两种方法(一)
- Android(安卓)Audio - 支持多个APK同时录音
- Android(安卓)从启动到程序运行发生的事情
- Android(安卓)网络编程 API笔记 - java.net 包相关 接口 api
- android中如何通过代码来开启和关闭移动网络
- 异步加载图片
- Android中 网络框架Volley的用法
- [转]Android异步处理系列文章索引