0

I am trygin to upload a image with a POST request with the next requirements:

Parameter name: imageFile
Parameter type: File
File to be uploaded. It must be included in the body of the request as multipart part with value=image and type=image/*.

This is the request definition:

    @Multipart
    @POST("sample/uploadImage")
    Call<ImageResponse> UploadImage(@Part MultipartBody.Part file);

This is the code:

  RequestBody fbodyRequest = RequestBody.create(MediaType.parse("image/*"), destination);

[...]

RetrofitInterface apiService =
                retrofit.create(RetrofitInterface.class);
        Call<ImageResponse> call = apiService.UploadImage(fbodyRequest);
        call.enqueue(new Callback<ImageResponse>() {...

Thanks in advance.

Joterito
  • 153
  • 2
  • 15
  • Kindly refer: http://stackoverflow.com/questions/34562950/post-multipart-form-data-using-retrofit-2-0-including-image – Ramesh Kumar Mar 05 '17 at 08:04

1 Answers1

0

You can try the below code to upload the image in file type.

Retrofit Interface class:

public interface RetrofitInterface {
  @Multipart
    @POST("v1/photo/{deviceid}/")
    public Call<PhotoModel> photoValues(@Path("deviceid") String deviceid, @Query("key") String key, @Part MultipartBody.Part filePart)

    final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(50000, TimeUnit.SECONDS)
            .connectTimeout(50000, TimeUnit.SECONDS)
            .build();

    public static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(AppConstants.mBaseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build();
}

First get image file and converted into FilePart type then only we can send through the retrofit api. It will works fine.

Main Class:

RetrofitInterface service = RetrofitInterface.retrofit.create(RetrofitInterface.class);
        Call<PhotoModel> call = service.photoValues(deviceid, AppConstants.mApiKey, filePart);
        call.enqueue(new Callback<PhotoModel>() {
            @Override
            public void onResponse(Call<PhotoModel> call, Response<PhotoModel> response) {
                .....
            }

            @Override
            public void onFailure(Call<PhotoModel> call, Throwable t) {
                .....
            }
        });
Sathish Kumar VG
  • 2,154
  • 1
  • 12
  • 19