android-async-http上傳檔案

來源:互聯網
上載者:User

標籤:android   style   http   io   color   ar   os   使用   java   

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", "[email protected]"); 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");


android-async-http上傳檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.