【Android架构】基于MVP模式的Retrofit2+RXjava封装之文件上传(三)

1.单图上传

首先ApiServer,要使用Multipart 注解

//上传图片(私有接口)
  @POST("index.php/PrivateApi/Goods/uploadPic")
  @Multipart
  Observable> upLoadImg(@Part MultipartBody.Part parts);

然后是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>(baseView, true) {
            @Override


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

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

成功后做个提示就好

2.多图上传

ApiServer

@POST("index.php/PrivateApi/Goods/uploadPic")
    @Multipart
    Observable> upLoadImg(@Part MultipartBody.Part[] parts);

Presenter

 public void upLoadImg(ArrayList 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>(baseView, true) {
            @Override


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

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

3.文件和普通参数混合

ApiServer

  //上传图片(私有接口)
    @POST("index.php/PrivateApi/Goods/uploadPic")
    @Multipart
    Observable> upLoadImg(@Part MultipartBody.Part[] parts, @Part("APP_KEY") RequestBody APP_KEY, @Part("APP_TOKEN") RequestBody APP_TOKEN);

Presenter

 public void upLoadImg(ArrayList 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>(baseView, true) {
            @Override


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

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

至此,使用Retrofit文件上传暂时告一段落。

喜欢的话请帮忙转发一下能让更多有需要的人看到吧,有些技术上的问题大家可以多探讨一下。

【Android架构】基于MVP模式的Retrofit2+RXjava封装之文件上传(三)_第1张图片
【Android架构】基于MVP模式的Retrofit2+RXjava封装之文件上传(三)_第2张图片

以上Android资料以及更多Android相关资料及面试经验可在QQ群里获取:936903570。有加群的朋友请记得备注上CSDN,谢谢。

你可能感兴趣的:(【Android架构】基于MVP模式的Retrofit2+RXjava封装之文件上传(三))