retrofit2 rxjava2 okhttp3 上传多个文件示例

Interface 层


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



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

文件放置

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

    public static MultipartBody filesToMultipartBody(String key, @Nullable MediaType contentType, List 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 imagesToMultipartBodyParts(@NonNull String key, @NonNull List files) {
        return filesToMultipartBodyParts(key, MediaType.parse("image/png"), files);
    }


    public static List filesToMultipartBodyParts(@NonNull String key, @Nullable MediaType contentType, @NonNull List files) {
        List 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>() {
                    @Override
                    public void accept(YiDongResponseDto objectYiDongResponseDto)  {

                    }
                }, new DefaultError());

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

                    }
                }, new DefaultError());
 

                            
                        
                    
                    
                    

你可能感兴趣的:(retrofit2 rxjava2 okhttp3 上传多个文件示例)