RxJava+Retrofit+MVP 带有进度条的文件上传

github地址:https://github.com/JiangAndroidwork/RetrofitOfRxJava

   传送门:   

RxJava+Retrofit+MVP 封装带有加载框的Http请求 

RxJava+Retrofit实现文件下载

上传文件的方法和基本的Http请求方法相同,只不过针对了上传文件的参数进行了封装。

service接口:

 public interface RetrofitMethodsInterface  {
    /*上传文件*/
    @Multipart
    @POST("classalbumUpload")
    Flowable uploadImage(@Part("albumId") RequestBody albumId, @Part("accessToken") RequestBody accessToken, @Part MultipartBody.Part file);
}

封装上传的文件,并返回Part,用来作为uploadImage的参数:

final PushFileManage pushFileManage = new PushFileManage(this,new File("/storage/emulated/0/DCIM/Camera/IMG_20170202_094844.jpg"),"file","image/jpg");
  final MultipartBody.Part part = pushFileManage.pushFileBackPart();

请求方法:

 final RequestBody uid= RequestBody.create(MediaType.parse("text/plain"), "72");
        final RequestBody key = RequestBody.create(MediaType.parse("text/plain"), "45ab2fbbdd5ac8aec951f219f33fb5cc");
        ProgressBarOfRetrofit pBR = ProgressBarOfRetrofit.getInstance(this,
                "http://sss/cloudapi/teacher/", new RetrofitOfRxJavaCallBack() {
                    @Override
                    public void callBack(Retrofit retrofit) {
                        retrofit.create(RetrofitMethodsInterface.class)
                                .uploadImage(uid,key,part)
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new ApiSubscriber() {
                                    @Override
                                    protected void onError(String msg, int code) {
                                        Log.i("失败的信息==",msg);
                                    }

                                    @Override
                                    protected void onSuceess(PushFileBean pushFileBean) {
                                Log.i("输出信息==",pushFileBean.toString());
                                    }
                                });
                    }
                });
        pBR.setProgressState(false);
        pBR.setStart(false);
pBR.setProgressState(false)是设置基础http请求的加载弹窗是否显示。这里我让其关闭,因为PushFileManage封装了ProgressBar。

你可能感兴趣的:(Retrofit,RxJava)