【Android架構】基於MVP模式的Retrofit2+RXjava封裝之文件上傳(三)

前言

最近手頭事比較多,抽個空把以前系列也補充一下。今天要說的是文件上傳php

1.單圖上傳

首先ApiServer,要使用Multipart 註解java

//上傳圖片(私有接口)
  @POST("index.php/PrivateApi/Goods/uploadPic")
  @Multipart
  Observable<BaseListModel<String>> upLoadImg(@Part MultipartBody.Part parts);

複製代碼

而後是Presenterapi

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.多圖上傳

ApiServerapp

@POST("index.php/PrivateApi/Goods/uploadPic")
    @Multipart
    Observable<BaseListModel<String>> upLoadImg(@Part MultipartBody.Part[] parts);
複製代碼

Presenteride

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.文件和普通參數混合

ApiServerpost

//上傳圖片(私有接口)
    @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);
複製代碼

Presenterspa

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文件上傳暫時告一段落。code

你的承認,是我堅持更新博客的動力,若是以爲有用,就請點個贊,謝謝

相關文章
相關標籤/搜索