First, preface
Google has been widely used since the release of the volley framework in the 2013 IO Conference, indeed, using several network request libraries, feeling volley is still very useful, especially handy. But encountered uploading files is more troublesome, especially sometimes want a parameter name corresponding to multiple files, like my pit daddy backstage to my interface, is the parameter key is called images, then value is multi-figure. Multi-image ... Figure....
There are many network Request library post requests, with parameters using HashMap for encapsulation, but HashMap is in the form of key-value pairs, there is a problem is that a key corresponding to a value, when you put in a row two key is the same time, the latter will replace the former. Finally, there is only one parameter left. This is not up to the demand. And there is a solution is to open more sub-threads, each pass a picture in the past, in fact, this is also OK, specific or look at the demand, I would like to discuss a one-time transmission of the map.
Second, the settlement
If you use volley, because it is easy to request data, but it is not good to have to upload files, it is not recommended to use multiple network request class libraries. So here is a solution, but also with a jar package, here is Httpmime (click to download), the main use is the multipartentity class, you can encapsulate the request parameters.
< Span style= "padding:0px; margin:0px; font-size:14px; " >
< Span style= "padding:0px; margin:0px; font-size:14px; " > multipartentity class encapsulates file parameters, This implements a parameter name corresponding to a file,
a parameter name corresponds to multiple files, if you want multiple parameter names to correspond to multiple files, you can try to implement it yourself. :
< Span style= "padding:0px; margin:0px; font-size:14px; " >
package com.android.volley.toolbox;import java.io.bytearrayoutputstream;import java.io.file; import java.io.ioexception;import java.io.unsupportedencodingexception;import java.nio.charset.charset;import java.util.arraylist;import java.util.collections;import java.util.hashmap;import java.util.list;import java.util.map;import Org.apache.http.entity.mime.multipartentity;import org.apache.http.entity.mime.content.filebody;import org.apache.http.entity.mime.content.stringbody;import com.android.volley.authfailureerror;import com.android.volley.NetworkResponse;import com.android.volley.Request;import Com.android.volley.response;import com.android.volley.volleylog;import com.common.utils.clog;import com.common.utils.fileutil;public class multipartrequest extends request<string > {private multipartentity entity = new multipartentity ();private final response.listener<string> mlistener;private list<file> mfileparts;private string mfilepartname;private map<string, string> mparams;/** * Single file * @param url * @param errorListener * @param listener * @param filePartName * @param file * @param params */public multipartrequest (String url, response.errorlistener errorlistener, response.listener<string> listener, string filepartname, file file,map< String, string> params) {super (method.post, url, errorlistener); mFileParts = new ArrayList<File> ();if (file != null) {mfileparts.add (File);} mfilepartname = filepartname;mlistener = listener;mparams = params; Buildmultipartentity ();} /** * multiple files, corresponding to a key * , @param url * @param errorListener * @param listener * @param filePartName * @param files * @param params */public Multipartrequest (string url, response.errorlistener errorlistener,response.listener<string > listener, string filepartname,list<file> files, map<string, string > params) {super (Method.post, url, errorlistener);mfilepartname = filepartname;mlistener = listener;mfileparts = files;mparams = params; Buildmultipartentity ();} Private void buildmultipartentity () {if (mfileparts != null && mfileparts.size () > 0) {for (file file : mfileparts) { Entity.addpart (mfilepartname, new filebody (file));} Long l = entity.getcontentlength (); CLog.log (mfileparts.size () + "one, longDegree: "+l);} try {if (Mparams != null && mparams.size () > 0) {for (Map.entry<string, string> entry : mparams.entryset ()) {entity.addPart ( Entry.getkey (), New stringbody (Entry.getvalue (), charset.forname ("UTF-8"));}}} catch (unsupportedencodingexception e) {volleylog.e ("Unsupportedencodingexception");}} @Overridepublic string getbodycontenttype () {return entity.getcontenttype (). GetValue ();} @Overridepublic byte[] getbody () throws AuthFailureError {ByteArrayOutputStream Bos = new bytearrayoutputstream (); Try {entity.writeto (BOS);} catch (ioexception e) {volleylog.e ("ioexception writing to Bytearrayoutputstream ");} Return bos.tobytearray ();} @Overrideprotected response<string> parsenetworkresponse (networkresponse response) { CLog.log ("ParsenetworkResponse ");if (Volleylog.debug) {if (response.headers != null) {for ( Map.entry<string, string> entry : response.headers.entryset ()) &NBSP;{VOLLEYLOG.D ( Entry.getkey () + "=" + entry.getvalue ());}} String parsed;try {parsed = new string (Response.data,httpheaderparser.parsecharset ( response.headers));} catch (unsupportedencodingexception e) {parsed = new string (response.data) ;} Return response.success (Parsed,httpheaderparser.parsecacheheaders (Response));} /* * (non-javadoc) * * @see com.android.volley.request#getheaders () * /@Overridepublic map<string, string> getheaders () throws AuthFailureError {VOLLEYLOG.D ("getheaders"); Map<string, string> headers = super.getheaders ();if (headers == null | | headers.equals (Collections.emptyMap ())) {headers = new HashMap<String, String> ();} Return headers;} @Overrideprotected void deliverresponse (string response) {mlistener.onresponse (response);}}
Through this request can upload a lot of files, also do not long explanation, code is relatively simple. I also tested this method, but because it is to deal with the server, there is really nothing to show it, but I still like convincing description, so-called there is a picture of the truth Haha, on the two diagram in the explanation
Use volley to upload pictures, a parameter more than one diagram, multiple pictures of multiple images, pro-test effective OH