Before uploading images are directly to the image into IO stream to the server, no frame to pass the picture.
Recently did the project, intends to change the method to upload the picture.
Android Development to now, Okhttp appears more and more important, so, this time I choose to upload pictures with okhttp.
Okhttp has now been updated to the OKHTTP3 version, and there are some differences in usage than before. Find a lot of information on the Internet,
And the Java background colleagues repeatedly debugging, and finally successfully uploaded multiple pictures, while passing some key value pairs of parameters.
Here is my encapsulation of the process:
Private Static FinalMediaType media_type_png = Mediatype.parse ("Image/png");/*** Upload multiple photos and parameters *@paramrequrl URL Address *@paramparams parameter *@parampic_key keywords to upload images *@paramPaths Picture Path*/ PublicObservable<string> Sendmultipart (String requrl,map<string, string> params,string Pic_key, List<File >files) { returnObservable.create (NewObservable.onsubscribe<string>() {@Override Public voidCall (SUBSCRIBER<?SuperString>subscriber) {Multipartbody.builder Multipartbodybuilder=NewMultipartbody.builder (); Multipartbodybuilder.settype (Multipartbody.form); //traverse all parameters in map to builder if(Params! =NULL){ for(String key:params.keySet ()) {Multipartbodybuilder.addformdatapart (key, Params.get (key)); } } //traverse the absolute path of all images in paths to builder, and contract key such as "upload" as the background to accept multiple images of key if(Files! =NULL){ for(File file:files) {Multipartbodybuilder.addformdatapart (Pic_key, File.getname (), Requestbod Y.create (media_type_png, file)); } } //Building the request bodyRequestbody Requestbody =Multipartbodybuilder.build (); Request.builder Requestbuilder=NewRequest.builder (); Requestbuilder.url (Requrl);//Add a URL addressRequestbuilder.post (requestbody); Request Request=Requestbuilder.build (); Mokhttpclient.newcall (Request). Enqueue (NewCallback () {@Override Public voidonfailure (call call, IOException e) {subscriber.onerror (e); Subscriber.oncompleted (); Call.cancel (); } @Override Public voidOnresponse (call call, Response Response)throwsIOException {String str=response.body (). String (); Subscriber.onnext (str); Subscriber.oncompleted (); Call.cancel (); } }); } }); }
View Code
Calls in the UI interface:
Okhttp3utils.getinstance (). Sendmultipart (Constants.URL.URL_ADD_NOTICE, MMap, "Appendix", Mimagelist). Observeon (Androidschedulers.mainthread ()). Subscribeon (Schedulers.newthre AD ()). Subscribe (NewSubscriber<string>() {@Override Public voidoncompleted () {} @Override Public voidonError (Throwable throwable) {logutil.i (TAG,"Throwable:" +throwable.tostring ()); } @Override Public voidOnNext (String s) {logutil.i (TAG,"s:" +s); } });
View Code
During the commissioning process, one of the
Multipartbodybuilder.addformdatapart (Pic_key, File.getname (), Requestbody.create (media_type_png, file));
Was written
Multipartbodybuilder.addformdatapart (Pic_key, NULL, Requestbody.create (media_type_png, file));
It is important to note that the background is unable to get the picture in the normal way (although you can see the picture data when debugging the breakpoint).
Finally: Thank you very much for your colleague's help! The gift rose, the hand is fragrant ~
OKHTTP3 upload multiple images and pass parameters at the same time