定义请求方法
@Multipart
@POST(RequestApi.GET_DASH_NAME)
Observable<Message> getXXX( @Part MultipartBody.Part body);
获取将要上传的文件并创建MultipartBody.Part对象
//获取文件路径
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator+"2.jpg" ;
Log.d(TAG, path);
//获取路径对应的文件
File file = new File(path);
//得到请求体
RequestBody fileRQ = RequestBody.create(MediaType.parse("image/jpeg"), file);
//创建MultipartBody.Part对象
//注意:这个file是后台定义的参数名
MultipartBody.Part part =MultipartBody.Part.createFormData("file", file.getName(), fileRQ);
请求方法
RetrofitClient.getApiService().getXXX(part)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Message>() {
@Override
public void onSubscribe(Disposable d) {
//订阅
subscribe(d);
}
@Override
public void onNext(Message message) {
mView.showLoading();
mView.setData(message);
}
@Override
public void onError(Throwable e) {
Log.e(TAG,e.getMessage());
mView.dismissLoading();
}
@Override
public void onComplete() {
mView.dismissLoading();
}
});