Android-async-http Uploading Files

Source: Internet
Author: User

1. Asynchttpclient, Requestparams, Asynchttpresponsehandler three ways to use a class

(1) asynchttpclient
public class Asynchttpclient extends Java.lang.Object
  This class is typically used in Android applications to create asynchronous get, POST, put, and delete HTTP requests, request parameters created through Requestparams instances, and respond by overriding anonymous inner classes Responsehandlerinterface method of processing.
example:

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 

example:

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> ("Age",  " user1.put");  User1.put ("Gender",  "male");  map<string, string> user2 = new hashmap <String, String> ("Age",  " user2.put"),  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
is used to intercept and process requests created by Asynchttpclient. The override onsuccess (int, org.apache.http.header[], byte[]) method in anonymous class Asynchttpresponsehandler is used to process requests that respond successfully. In addition, you can also rewrite onfailure (int, org.apache.http.header[], byte[], throwable), OnStart (), OnFinish (), Onretry (), and OnProgress ( int, int) method
example:

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. Uploading files using Requestparams
Class Requestparams supports multipart file upload

(1) Add File object for uploading

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

(2) Add byte array for uploading (byte conversion see above)

byte[] Mybytearray = bytes; Requestparams params = new Requestparams ();p arams.put ("File", new Bytearrayinputstream (Mybytearray), "Text.txt");


Android-async-http Uploading Files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.