1, in addition to the use of volley, but also used a package httpmime (: http://download.csdn.net/detail/chequer_lkp/8102751)
2, need a tool class, this class is inherited volley request
Packagenewairtek.com.utils;ImportCom.android.volley.AuthFailureError;ImportCom.android.volley.NetworkResponse;Importcom.android.volley.Request;ImportCom.android.volley.Response;ImportCom.android.volley.VolleyLog;ImportCom.android.volley.toolbox.HttpHeaderParser;Importorg.apache.http.entity.mime.MultipartEntity;ImportOrg.apache.http.entity.mime.content.FileBody;ImportOrg.apache.http.entity.mime.content.StringBody;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;Importjava.io.IOException;Importjava.io.UnsupportedEncodingException;ImportJava.nio.charset.Charset;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;/*** Created by Administrator on 2016/1/12. *---------Volley image upload-------------*/ Public classMultipartrequestextendsRequest<string> { Privatemultipartentity entity =Newmultipartentity (); Private FinalResponse.listener<string>Mlistener; PrivateList<file>Mfileparts; PrivateString Mfilepartname; PrivateMap<string, string>Mparams; /*** Single File *@paramURL *@paramErrorlistener *@paramListener *@paramFilepartname *@paramfile *@paramparams*/ Publicmultipartrequest (String URL, response.errorlistener errorlistener, Response.listener<String>Listener, String filepartname, file file, Map<string, string>params) { Super(method.post, URL, errorlistener); Mfileparts=NewArraylist<file>(); if(File! =NULL) {mfileparts.add (file); } mfilepartname=Filepartname; Mlistener=Listener; Mparams=params; Buildmultipartentity (); } /*** Multiple files, corresponding to a key *@paramURL *@paramErrorlistener *@paramListener *@paramFilepartname *@paramFiles *@paramparams*/ Publicmultipartrequest (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 voidbuildmultipartentity () {if(Mfileparts! =NULL&& mfileparts.size () > 0) { for(File file:mfileparts) {Entity.addpart (Mfilepartname,Newfilebody (file)); } LongL =entity.getcontentlength ();//CLog.log (mfileparts.size () + "One, Length:" +l); } Try { if(Mparams! =NULL&& mparams.size () > 0) { for(Map.entry<string, string>Entry:mParams.entrySet ()) {Entity.addpart (Entry.getkey (),NewStringbody (Entry.getvalue (), Charset. forname ("UTF-8"))); } } } Catch(unsupportedencodingexception e) {VOLLEYLOG.E ("Unsupportedencodingexception"); }} @Override PublicString Getbodycontenttype () {returnEntity.getcontenttype (). GetValue (); } @Override Public byte[] GetBody ()throwsauthfailureerror {bytearrayoutputstream BOS=NewBytearrayoutputstream (); Try{Entity.writeto (BOS); } Catch(IOException e) {VOLLEYLOG.E ("IOException Writing to Bytearrayoutputstream"); } returnBos.tobytearray (); } @OverrideprotectedResponse<string>Parsenetworkresponse (networkresponse response) {//CLog.log ("Parsenetworkresponse"); if(volleylog.debug) {if(Response.headers! =NULL) { for(Map.entry<string, string>entry:response.headers. EntrySet ()) {VOLLEYLOG.D (Entry.getkey ()+ "=" +Entry.getvalue ()); }}} String parsed; Try{parsed=NewString (Response.data, Httpheaderparser.parsecharset (response.headers)); } Catch(Unsupportedencodingexception e) {parsed=NewString (Response.data); } returnresponse.success (parsed, httpheaderparser.parsecacheheaders (Response)); } /** (Non-javadoc) * * @see com.android.volley.request#getheaders ()*/@Override PublicMap<string, string> getheaders ()throwsauthfailureerror {volleylog.d ("Getheaders"); Map<string, string> headers =Super. Getheaders (); if(Headers = =NULL||headers.equals (Collections.emptymap ())) {Headers=NewHashmap<string, string>(); } returnheaders; } @Overrideprotected voidDeliverresponse (String response) {mlistener.onresponse (response); }}
3, usage and volley other request methods are basically the same, but the parameters are not the same.
android--using the valley frame to upload images