preface

I’ve got a lot on my plate, so take a break from the previous series. Today’s topic is file uploading

  • Retrofit2+RXjava encapsulation based on MVP Mode
  • Retrofit2+RXjava package file download based on MVP mode
  • Retrofit2+RXjava package file upload based on MVP mode
  • Common Problems with Retrofit2+RXjava encapsulation based on MVP Mode (iv)
  • MVP mode Retrofit2+RXjava encapsulation breakpoint Download (5)
  • Retrofit2+RXjava Encapsulation based on MVP Mode data preprocessing (6)
  • 【Android architecture 】 Retrofit2+RXjava encapsulation based on MVP mode

1. Upload a single image

First of all, ApiServer, using the Multipart annotation

// Upload images (private interface)
  @POST("index.php/PrivateApi/Goods/uploadPic")
  @Multipart
  Observable<BaseListModel<String>> upLoadImg(@Part MultipartBody.Part parts);

Copy the code

And then the Presenter

 public void upLoadImg(String path) {
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("headimg", file.getName(), requestFile);
        addDisposable(apiServer.upLoadImg(filePart), new BaseObserver<BaseListModel<String>>(baseView, true) {
            @Override


            public void onSuccess(BaseListModel<String> o) {
                baseView.onUpLoadSucc(o.getData());
            }

            @Override
            public void onError(String msg) { baseView.showError(msg); }}); }Copy the code

Just make a note of your success

2. Upload multiple images

ApiServer

 @POST("index.php/PrivateApi/Goods/uploadPic")
    @Multipart
    Observable<BaseListModel<String>> upLoadImg(@Part MultipartBody.Part[] parts);
Copy the code

Presenter

 public void upLoadImg(ArrayList<String> media) {
        if (media == null) {
            return;
        }

        MultipartBody.Part[] parts = new MultipartBody.Part[media.size()];
        int cnt = 0;
        for (String m : media) {
            File file = new File(m);
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part filePart = MultipartBody.Part.createFormData("headimg[]", file.getName(), requestFile);
            parts[cnt] = filePart;
            cnt++;
        }

     
        addDisposable(apiServer.upLoadImg(parts), new BaseObserver<BaseListModel<String>>(baseView, true) {
            @Override


            public void onSuccess(BaseListModel<String> o) {
                baseView.onUpLoadSucc(o.getData());
            }

            @Override
            public void onError(String msg) { baseView.showError(msg); }}); }Copy the code

3. Mix files with common parameters

ApiServer

  // Upload images (private interface)
    @POST("index.php/PrivateApi/Goods/uploadPic")
    @Multipart
    Observable<BaseListModel<String>> upLoadImg(@Part MultipartBody.Part[] parts, @Part("APP_KEY") RequestBody APP_KEY, @Part("APP_TOKEN") RequestBody APP_TOKEN);
Copy the code

Presenter

 public void upLoadImg(ArrayList<String> media) {
        if (media == null) {
            return;
        }

        MultipartBody.Part[] parts = new MultipartBody.Part[media.size()];
        int cnt = 0;
        for (String m : media) {
            File file = new File(m);
            RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            MultipartBody.Part filePart = MultipartBody.Part.createFormData("headimg[]", file.getName(), requestFile);
            parts[cnt] = filePart;
            cnt++;
        }

        RequestBody appkey = RequestBody.create(MediaType.parse("multipart/form-data"), AppConstant.APP_KEY);
        RequestBody apptoken = RequestBody.create(MediaType.parse("multipart/form-data"), UserImpl.getAppToken());
//
        addDisposable(apiServer.upLoadImg(parts, appkey, apptoken), new BaseObserver<BaseListModel<String>>(baseView, true) {
            @Override


            public void onSuccess(BaseListModel<String> o) {
                baseView.onUpLoadSucc(o.getData());
            }

            @Override
            public void onError(String msg) { baseView.showError(msg); }}); }Copy the code

At this point, using Retrofit file uploads is over for now.

Your recognition is the motivation for me to keep updating my blog. If it is useful, please give me a thumbs-up. Thank you