Android 文章

retrofit2 rxjava2 okhttp3 上传多个文件

2019-01-28  本文已影响12人  大象屁股

Interface 层


    @Multipart
    @POST("/commitment/published")
    Observable<YiDongResponseDto<Object>> uploadPromise(@Part List<MultipartBody.Part> parts,
                                                        @Query("text") String text,
                                                        @Query("sessionId") String sessionId);



    @POST("/commitment/published")
    Observable<YiDongResponseDto<Object>> uploadPromise(@Body MultipartBody multipartBody,
                                                        @Query("text") String text,
                                                        @Query("sessionId") String sessionId);

文件放置

  public static MultipartBody imagesToMultipartBody(String key,List<File> files) {
        return filesToMultipartBody(key, MediaType.parse("image/png"), files);
    }

    public static MultipartBody filesToMultipartBody(String key, @Nullable MediaType contentType, List<File> files) {
        MultipartBody.Builder builder = new MultipartBody.Builder();

        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见,没有判断file的类型
            RequestBody requestBody = RequestBody.create(contentType, file);
            builder.addFormDataPart(key, file.getName(), requestBody);
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();
        return multipartBody;
    }

    public static List<MultipartBody.Part> imagesToMultipartBodyParts(@NonNull String key, @NonNull List<File> files) {
        return filesToMultipartBodyParts(key, MediaType.parse("image/png"), files);
    }


    public static List<MultipartBody.Part> filesToMultipartBodyParts(@NonNull String key, @Nullable MediaType contentType, @NonNull List<File> files) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见,没有判断file的类型
            RequestBody requestBody = RequestBody.create(contentType, file);
            MultipartBody.Part part = MultipartBody.Part.createFormData(key, file.getName(), requestBody);
            parts.add(part);
        }
        return parts;
    }

使用


        Disposable images = YdPromiseHttpApi.getInstance().uploadPromise(HttpRequest.imagesToMultipartBodyParts("images", listFiles),
                mInputText.getText().toString(),
                AppPreferences.getInstance().getMobile())
                .subscribe(new Consumer<YiDongResponseDto<Object>>() {
                    @Override
                    public void accept(YiDongResponseDto<Object> objectYiDongResponseDto)  {

                    }
                }, new DefaultError());

        Disposable images1 = YdPromiseHttpApi.getInstance().uploadPromise(HttpRequest.imagesToMultipartBody("images", listFiles),
                mInputText.getText().toString(),
                AppPreferences.getInstance().getMobile())
                .subscribe(new Consumer<YiDongResponseDto<Object>>() {
                    @Override
                    public void accept(YiDongResponseDto<Object> objectYiDongResponseDto) throws Exception {

                    }
                }, new DefaultError());
上一篇下一篇

猜你喜欢

热点阅读