RxJava+Retrofit+MVP 封裝帶有加載框的Http請求
上傳文件的方法和基本的Http請求方法相同,只不過針對了上傳文件的參數進行了封裝。
service接口:
public interface RetrofitMethodsInterface {
/*上傳文件*/
@Multipart
@POST("classalbumUpload")
Flowable<PushFileBean> 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<PushFileBean>() {
@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。