前言
最近手頭事比較多,抽個空把之前系列也補充一下。今天要說的是檔案上傳
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝(一)
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之檔案下載(二)
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之檔案上傳(三)
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之常見問題(四)
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之斷點下載(五)
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之資料預處理(六)
- 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之多Url(七)
1.單圖上傳
首先ApiServer
,要使用Multipart
註解
//上傳圖片(私有介面)
@POST("index.php/PrivateApi/Goods/uploadPic")
@Multipart
Observable<BaseListModel<String>> 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<BaseListModel<String>>(baseView, true) {
@Override
public void onSuccess(BaseListModel<String> o) {
baseView.onUpLoadSucc(o.getData());
}
@Override
public void onError(String msg) {
baseView.showError(msg);
}
});
}
複製程式碼
成功後做個提示就好
2.多圖上傳
ApiServer
@POST("index.php/PrivateApi/Goods/uploadPic")
@Multipart
Observable<BaseListModel<String>> upLoadImg(@Part MultipartBody.Part[] parts);
複製程式碼
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);
}
});
}
複製程式碼
3.檔案和普通引數混合
ApiServer
//上傳圖片(私有介面)
@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);
複製程式碼
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);
}
});
}
複製程式碼
至此,使用Retrofit
檔案上傳暫時告一段落。