【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
文件上传暂时告一段落。
你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢
-
932
-
equationl
Android
Android Jetpack
-
999
-
Android
Android Jetpack
-
528
-
TechMerger
Android
掘金·金石计划
-
776
-
张风捷特烈
Flutter
Android
-
1030
-
equationl
Android
Android Jetpack
-
759
-
恋猫de小郭
Flutter
Android
-
642
-
二流小码农
Android
Kotlin
Android Studio
-
427
-
Bowen_Jin
Flutter
Android
-
978
-
半岛铁盒里的猫
OpenGL
音视频开发
Android