【问题标题】:retrofit:2.0.0-beta2 post array of files改造:2.0.0-beta2 发布文件数组
【发布时间】:2016-09-08 17:51:55
【问题描述】:

我正在使用改造改造:2.0.0-beta2 向服务器发送多个数据。 数据包含文件数组ArrayList<File>。 我正在使用此代码发送分开文件:

public Call<User> requestUpdateProfile3(String token, File image) {    
RequestBody requestBodyImage = RequestBody.create(MediaType.parse("multipart/form-data"), image);
            return apiService.updateProfile3(token, requestBodyProfile, requestBodyImage);

这在 ApiService 上

@Multipart
    @POST("/shanyraq/profile/update")
    Call<User> updateProfile3(
            @Header(value = "X-AUTH-TOKEN") String toke,
            @Part("1\"; filename=\"1.jpg") RequestBody image);

问题是:如何使用 retrofit:2.0.0-beta2 发布文件数组??

【问题讨论】:

    标签: android retrofit multipartform-data


    【解决方案1】:

    我已上传多张图片,如下所示。它会上传带有他们名字的图片。

    服务接口:

        @Multipart
        @POST(ConstantsWebService.UPLOAD_SERVICES_IMAGE)
        Call<List<String>> uploadImage(
                 @Header("Authorization") String token,
                 //@Part("file\"; filename=\"902367000083-1.jpg") RequestBody mapFileAndName); //for sending only one image
                 @PartMap() Map<String,RequestBody> mapFileAndName); //for sending multiple images
    

    请求:

        HashMap<String,RequestBody> map=new HashMap<>(listOfNames.size());
        RequestBody file=null;
        File f=null;
    
        for(int i=0,size=listOfNames.size(); i<size;i++){
            try {
                f = new File(context.getCacheDir(), listOfNames.get(i)+".jpg");
                FileOutputStream fos = new FileOutputStream(f);
                Bitmap bitmap = getImageFromDatabase(listOfNames.get(i));
                if(bitmap!=null){
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 0 /*ignored for PNG*/, fos);
                    fos.flush();
                    fos.close();
                }else{
                    view.showErrorView("imageNotFound"); //todo
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
                view.showErrorView("imageNotFound || file not created"); //todo
                return;
            }
    
            file=RequestBody.create(MediaType.parse("multipart/form-data"), f);
            map.put("file\"; filename=\""+listOfNames.get(i)+".jpg",file);
            file=null;
            f = null;
        }
    
        serviceOperation.uploadImage(token,map).enqueue(){..}
    

    所以你改变了返回类型、标题名称、图像名称。

    【讨论】:

    • 我可以在一个 RequestBody 中发送文件数组吗?
    • RequestBody 类具有名为 create 的方法,它们都以 File 或 MediaType 对象作为参数。所以我认为你不能在RequestBody中存储数组,但你可以将RequestBody数组发送到webservice
    • 你如何指定名称/值。如果我想将它发送到“images[]”下的服务器,我该怎么做?
    • @Sree:有答案吗?
    • @Code_Life 没有。如果您找到答案,请在此处发布
    【解决方案2】:

    如果这也能对您有所帮助,我会很高兴和高兴。

    服务器 API 合约: 输入类型:Multipart-from data

    要在正文中针对名为“images”的键发送的数据

    @POST("feeds")
    Call<> createFeeds(@Body RequestBody file);
    
    
     MultipartBody.Builder builder = new MultipartBody.Builder();
        builder.setType(MultipartBody.FORM);
        builder.addFormDataPart("content", textContent);
    
        for(String filePath : imagePathList){
            File file = new File(filePath);
            builder.addFormDataPart("images", file.getName(),
                    RequestBody.create(MediaType.parse("image/*"), file));
        }
    
        MultipartBody requestBody = builder.build();
        Call<SocialCreateFeedResponse> call = mSocialClient.createFeeds( requestBody);
    

    【讨论】:

      【解决方案3】:
          Interface structure:
          @Multipart
          @POST("user_edit_profile_save")
          Call<EditProfileDto> saveEditProfile(@Part("first_name") RequestBody first_name, 
                                               @Part MultipartBody.Part profile_picture);
      
      
          Calling:
      RequestBody requestBodyUserId = RequestBody.create(
                          MediaType.parse("multipart/form-data"), "5");
      MultipartBody.Part body = null;
                  if (path != null && path.length() > 0) {
                      file = new File(path);
                      isImageSet = true;
                      requestBodyPicture = RequestBody.create(
                              MediaType.parse("multipart/form-data"), file);
                      body = MultipartBody.Part.createFormData("profile_picture", file.getName(), requestBodyPicture);
      
                  } else {
                      requestBodyPicture = RequestBody.create(
                              MediaType.parse("multipart/form-data"), "");
                  }
      

      【讨论】:

      • 请在写代码审查之前给出一些描述
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 2016-05-30
      相关资源
      最近更新 更多