Retrofit v2.0.0-beta2 how to upload a file using RequestBody

I am trying to send images to a server. Here are my server side php variable names provided to me by the script:

$value1 = $_POST["value1"]; // string value
$value2 = $_POST["value2"]; // string value

$img_1 = $_FILES["fileToUpload1"]["name"]; // image file
$img_2 = $_FILES["fileToUpload2"]["name"]; // image file

and here is my code for placing the values ​​-

RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), path.toString());
RequestBody fileBody1 = RequestBody.create(MediaType.parse("image/png"), path.toString());

Call<uploadResponseModel> call = adapter.uploadValues("test1","test2",fileBody1,fileBody);

call.enqueue(new Callback<uploadResponseModel>() {
    @Override
    public void onResponse(Response<uploadResponseModel> response, Retrofit retrofit) {
         Log.i("TAG", "retro onResponse :" + response.body().getResponse().getStatus());
    }

    @Override
    public void onFailure(Throwable t) {
        Log.i("TAG", "retro onFailure :" + t.getCause());
    }
});

adapter class:

public interface RetrofitAdapter {
 @Multipart
 @POST("/TestApp/img_upload.php")
 Call<uploadResponseModel> uploadValues(@Part("value1")String  value1,@Part("value2")String  value2,@Part("fileToUpload1") RequestBody file1,@Part("fileToUpload2") RequestBody file2);
}

Both string values ​​are published to the server, but not images. Please correct me if I am mistaken in publishing image files. I am new to modernization.

+4
source share
6 answers

, (), fileToUpload1 fileToUpload2. , , , RetrofitAdapter.

RetrofitAdapter :

public interface RetrofitAdapter {
 @Multipart
 @POST("/TestApp/img_upload.php")
 Call<uploadResponseModel> uploadValues(@Part("value1")String  value1,@Part("value2")String  value2,@Part("fileToUpload1\"; filename=\"image\" ") RequestBody file1,@Part("fileToUpload2\"; filename=\"image\" ") RequestBody file2);
}

@Part ( "fileToUpload1 \"; filename =\ "image \" ") RequestBody1

fileToUpload1 fileToUpload2, .

, :)

+6

:

@Multipart
@POST("http://....")
Call<UploadResponse> uploadPicture(@PartMap Map<String,RequestBody>  pic1, @PartMap Map<String, RequestBody> pic2);

:

  public void uploadPicture(File pic1, File pic2) {
      Map<String, RequestBody> pic1Map = new HashMap<>();
      Map<String, RequestBody> pic2Map = new HashMap<>();
      RequestBody rb1 = RequestBody.create(MediaType.parse("image/*"), pic1);
      RequestBody rb2 = RequestBody.create(MediaType.parse("image/*"), pic2);
      pic1Map.put("file\"; filename=\"" + pic1.getName(), rb1);
      pic2Map.put("file\"; filename=\"" + pic2.getName(), rb2);

      Call<UploadResponse> call = apiClient.getAPIService().uploadPicture(pic1Map, pic2Map); 
      call.enqueue...
  }

, @PartMap . - OkHttp .

+2

RequestBody RequestBody. Fix uploadValues Retrofit:

           public interface RetrofitAdapter {
           @Multipart
           @POST("/TestApp/img_upload.php")
           Call<uploadResponseModel> uploadValues(@Part("value1")String  value1,@Part("value2")String  value2,@Part("fileToUpload1") TypedFile file1,@Part("fileToUpload2") TypedFile file2);
           }

:

           TypedFile file1 = new TypedFile("application/octet-stream", new File(path1));
           TypedFile file2 = new TypedFile("application/octet-stream", new File(path2));
           Call<uploadResponseModel> call = adapter.uploadValues("test1","test2",file1,file2);

           call.enqueue(new Callback<uploadResponseModel>() {
               @Override
               public void onResponse(Response<uploadResponseModel> response, Retrofit retrofit) {
                   Log.i("TAG", "retro onResponse :" + response.body().getResponse().getStatus());

               }

               @Override
               public void onFailure(Throwable t) {
                   Log.i("TAG", "retro onFailure :" + t.getCause());

               }
           });
0

create RequestBody. , . File. , path. File, . create -

File image = new File(path.toString());
RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), image);

fileBody1.

0

, .

@Part("file\"; filename=\"fileToUpload1.png\"") RequestBody file1

@Part("fileToUpload1") RequestBody file1

@Part("image\"; filename=\"fileToUpload1.png\"") RequestBody file1
0
source

There are some issues with multi-file uploading using a tag @Multipart.

The problem is solved using a tag @POSTwith a custom multipart constructor in the request body.

@POST ("userwebservices/app_upload_pic")
Call<APIRespone> uploadUserImages ( @Body RequestBody file );

Code for multi-page loading by transferring the path of the image file obtained from the android application.

 File file = new File(filePath);

 MediaType MEDIA_TYPE_IMAGE = MediaType.parse("image/*");
 RequestBody requestBody = new MultipartBuilder()
         .type(MultipartBuilder.FORM)
         .addPart(
                 Headers.of("Content-Disposition", "form-data;name=\"uploaded_file\";filename=\"" + filePath + "\""),
                 RequestBody.create(MEDIA_TYPE_IMAGE, file))
         .build();

Here uploaded_fileis a validation line in a PHP script.

-1
source

Source: https://habr.com/ru/post/1613585/


All Articles