Okhttp3 uploads multiple images and transmits parameters at the same time,

Source: Internet
Author: User

Okhttp3 uploads multiple images and transmits parameters at the same time,

Previously, the uploaded images are directly converted to I/O and sent to the server without frame.

Recently, I am planning to upload images in another way.

Since the development of Android, Okhttp has become more and more important. So I chose to upload images using Okhttp this time.

Okhttp has been updated to Okhttp3, and its usage is different from that before. I found a lot of information online,

After debugging with the java background colleagues, I finally successfully uploaded multiple images and passed some key-Value Pair parameters.

The following is my encapsulation of this process:

Private static final MediaType MEDIA_TYPE_PNG = MediaType. parse ("image/png "); /*** upload multiple images and parameters * @ param reqUrl URL * @ param params parameter * @ param pic_key: the keyword for uploading images * @ param paths image path */public Observable <String> sendMultipart (String reqUrl, map <String, String> params, String pic_key, List <File> files) {return Observable. create (new Observable. onSubscribe <String> () {@ Override public void call (Subscriber <? Super String> subscriber) {MultipartBody. Builder multipartBodyBuilder = new MultipartBody. 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 paths of all images in paths to the builder, and specify the key, such as "upload", as the key if (files! = Null) {for (File file: files) {multipartBodyBuilder. addFormDataPart (pic_key, file. getName (), RequestBody. create (MEDIA_TYPE_PNG, file) ;}// construct the Request body RequestBody requestBody = multipartBodyBuilder. build (); Request. builder RequestBuilder = new Request. builder (); RequestBuilder. url (reqUrl); // Add the URL address RequestBuilder. post (requestBody); Request request = RequestBuilder. build (); mOkHttpClient. newCall (request ). enqueue (new Callback () {@ Override public void onFailure (Call call, IOException e) {subscriber. onError (e); subscriber. onCompleted (); call. cancel () ;}@ Override public void onResponse (Call call, Response response) throws IOException {String str = response. body (). string (); subscriber. onNext (str); subscriber. onCompleted (); call. cancel ();}});}});}
View Code

Call on the UI:

OkHttp3Utils.getInstance().sendMultipart(Constants.URL.URL_ADD_NOTICE, mMap, "appendix", mImageList)                .observeOn(AndroidSchedulers.mainThread())                .subscribeOn(Schedulers.newThread())                .subscribe(new Subscriber<String>() {                    @Override                    public void onCompleted() {                    }                    @Override                    public void onError(Throwable throwable) {                        LogUtil.i(TAG, "throwable:" + throwable.toString());                    }                    @Override                    public void onNext(String s) {                                               LogUtil.i(TAG, "s:" + s);                    }                });
View Code

During the debugging

MultipartBodyBuilder. addFormDataPart (pic_key, file. getName (), RequestBody. create (MEDIA_TYPE_PNG, file ));
Written
MultipartBodyBuilder. addFormDataPart (pic_key, null, RequestBody. create (MEDIA_TYPE_PNG, file ));
As a result, the background cannot obtain images in the conventional way (although the image data can be seen during breakpoint debugging.


Finally, I am very grateful to my colleagues for their help! Roses for gifts ~

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.