AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三个类使用方法

来源:互联网 发布:淘宝卡 哪个好 编辑:程序博客网 时间:2024/06/04 18:06

1. AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三个类使用方法

(1)AsyncHttpClient
public class AsyncHttpClient extends java.lang.Object
 该类通常用在android应用程序中创建异步GET, POST, PUT和DELETE HTTP请求,请求参数通过RequestParams实例创建,响应通过重写匿名内部类 ResponseHandlerInterface的方法处理。
例子:

AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.baidu.com", new ResponseHandlerInterface() {     @Override     public void onSuccess(String response) {         System.out.println(response);     } });


(2)RequestParams
public class RequestParams extends java.lang.Object 
用于创建AsyncHttpClient实例中的请求参数(包括字符串或者文件)的集合
例子:

RequestParams params = new RequestParams(); params.put("username", "tom"); params.put("password", "111111"); params.put("email", "my@email.com"); params.put("file", new File("test.txt")); // Upload a File  Map<String, String> map = new HashMap<String, String>(); map.put("first_name", "James"); map.put("last_name", "Smith"); params.put("user", map); // url params: "user[first_name]=James&user[last_name]=Smith"  Set<String> set = new HashSet<String>(); // unordered collection set.add("music"); set.add("art"); params.put("like", set); // url params: "like=music&like=art"  List<String> list = new ArrayList<String>(); // Ordered collection list.add("Java"); list.add("C"); params.put("languages", list); // url params: "languages[]=Java&languages[]=C"  String[] colors = { "blue", "yellow" }; // Ordered collection params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"  List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>(); Map<String, String> user1 = new HashMap<String, String>(); user1.put("age", "30"); user1.put("gender", "male"); Map<String, String> user2 = new HashMap<String, String>(); user2.put("age", "25"); user2.put("gender", "female"); listOfMaps.add(user1); listOfMaps.add(user2); params.put("users", listOfMaps); // url params: "users[][age]=30&users[][gender]=male&users[][age]=25&users[][gender]=female"  AsyncHttpClient client = new AsyncHttpClient(); client.post("http://myendpoint.com", params, responseHandler);


(3)public class AsyncHttpResponseHandler extends java.lang.Object implements ResponseHandlerInterface
用于拦截和处理由AsyncHttpClient创建的请求。在匿名类AsyncHttpResponseHandler中的重写 onSuccess(int, org.apache.http.Header[], byte[])方法用于处理响应成功的请求。此外,你也可以重写 onFailure(int, org.apache.http.Header[], byte[], Throwable), onStart(), onFinish(), onRetry() 和onProgress(int, int)方法
例子:

AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.google.com", new AsyncHttpResponseHandler() {     @Override     public void onStart() {         // Initiated the request     }      @Override     public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {         // Successfully got a response     }      @Override     public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {         // Response failed :(     }      @Override     public void onRetry() {         // Request was retried     }      @Override     public void onProgress(int bytesWritten, int totalSize) {         // Progress notification     }      @Override     public void onFinish() {         // Completed the request (either success or failure)     } });


2.利用RequestParams上传文件
类RequestParams支持multipart file 文件上传


(1)添加文件对象用于上传

File myFile = new File("/path/to/file.png");RequestParams params = new RequestParams();try {    params.put("profile_picture", myFile);} catch(FileNotFoundException e) {}


(2)添加字节数组用于上传(字节转化见上一片)
byte[] myByteArray = bytes;RequestParams params = new RequestParams();params.put("file", new ByteArrayInputStream(myByteArray), "text.txt");

转载自:http://my.oschina.net/u/1471093/blog/341811

0 0
原创粉丝点击