Navratan Soni
Navratan Soni

Reputation: 179

Android : Unable to upload multipart Image file using Retrofit 2.0.0 beta 2

Hi I am unable to upload an image using retrofit 2.0.0-beta2. It gives me "Image missing" error. Although I checked that file exist in system and also if I show in an Image view it is showing properly.

URL on which it needs to be uploaded : http://mywebsite.com/signature/upload/

So my BASE_URL is = http://mywebsite.com/

my .gradle file has this :

compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
compile 'com.squareup.okhttp:logging-interceptor:2.6.0'

SeriveGenerator.java class is

public class ServiceGenerator {

public static final String API_BASE_URL = Urls.URL_BASE;

private static OkHttpClient httpClient = new OkHttpClient();



private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(API_BASE_URL)
                .addConverterFactory(GsonConverterFactory.create());

public static <S> S createService(Class<S> serviceClass) {
    Retrofit retrofit = builder.client(httpClient).build();
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    httpClient.interceptors().add(interceptor);
    return retrofit.create(serviceClass);
}

}

UploadService.java class is

public interface UploadService {
@Multipart
@POST("/signature/upload/")  
// Tried to change this to "/signature/upload" as well (no slash in end)
// then gives Method not allowed error"
Call<String> upload(
        @Part("image") RequestBody file,
        @Part("image_name") String name);
}

And finally in my android activity

// photoFile is the file, checked that it exist in system and 
// path is also correct

public void uploadFileToServer() {
    UploadService service =
            ServiceGenerator.createService(UploadService.class);

    String name = "xyz.jpg";

    RequestBody requestBody =
            RequestBody.create(MediaType.parse("multipart/form-data"), photoFile);

    Call<String> call = service.upload(requestBody, name);
    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Response<String> response, Retrofit retrofit) {

            Log.v("Upload", "success");
        }

        @Override
        public void onFailure(Throwable t) {
            Log.e("Upload", t.getMessage());
        }
    });
}

So I am getting success log "success" from activity but on server file is not there, using Postman, I tried manually upload the image in form data, is gets uploaded correctly.

Upvotes: 1

Views: 2911

Answers (1)

Philipp Hellmayr
Philipp Hellmayr

Reputation: 302

I faced a similar problem a few days ago. Here comes my working code.

At first you have to define the Interface. It is very important NOT to add a content type header. In the following you see the @Part("picture\"; the "picture" is the corresponding name, that the Backend expects (in my case). So you have to change this name to make it work with your Backend. The filename=\"image\" just defines the name of your uploaded image. In my case the method is in the "DiaryService.java".

    @Multipart
    @POST("signature/upload")
    public Call<Void> addImageElementToDiary(@Part("picture\"; filename=\"image\" ") RequestBody picture,
                                             @Part("sort") RequestBody sort);

Now you can call the above defined Service. It is important to parse the Mediatype correctly. You used "multipart/form-data", this did not work in my case. You should take a look at the following Mediatypes.

    File file = new File(((CreateDiaryImage) element).getImagePath());

        RequestBody sort = RequestBody.create(MediaType.parse("text/plain"), element.getSort() + "");

        RequestBody requestBody =
                RequestBody.create(MediaType.parse("image/*"), file);

        Call<Void> call = diaryService.addImageElementToDiary(requestBody, sort);
        call.enqueue(new Callback<Void>() {
            @Override
            public void onResponse(Response<Void> response, Retrofit retrofit) {
                Log.v("Upload", "success");
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e("Upload", t.getMessage());
            }
        });

Upvotes: 1

Related Questions