1. AsyncHttpClient, RequestParams ,AsyncHttpResponseHandler三個類使用方法java
(1)AsyncHttpClient
public class AsyncHttpClient extends java.lang.Object
該類一般用在android應用程序中建立異步GET, POST, PUT和DELETE HTTP請求,請求參數經過RequestParams實例建立,響應經過重寫匿名內部類 ResponseHandlerInterface的方法處理。
例子:android
AsyncHttpClient client = new AsyncHttpClient(); client.get("http://www.baidu.com", new ResponseHandlerInterface() { @Override public void onSuccess(String response) { System.out.println(response); } });
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);
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) } });
(1)添加文件對象用於上傳apache
File myFile = new File("/path/to/file.png"); RequestParams params = new RequestParams(); try { params.put("profile_picture", myFile); } catch(FileNotFoundException e) {}
byte[] myByteArray = bytes; RequestParams params = new RequestParams(); params.put("file", new ByteArrayInputStream(myByteArray), "text.txt");
轉載自:http://my.oschina.net/u/1471093/blog/341811
數組