1

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.

Navratan Soni
  • 179
  • 1
  • 8
  • If your issue has not been solved, IMO, please try http://stackoverflow.com/questions/36491096/retrofit-multipart-request-required-multipartfile-parameter-file-is-not-pre?lq=1 – BNK Apr 09 '16 at 23:07

1 Answers1

1

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());
            }
        });
Philipp Hellmayr
  • 312
  • 2
  • 11
  • (@Part("picture\"; filename=\"image\" ") changed picture with the parameter name and it worked for me. Full article https://github.com/square/retrofit/wiki/%5B1063%5D-How-to-upload-file---Retrofit---2.0 – Ankit Aggarwal Feb 06 '16 at 15:45
  • any idea about my question? https://stackoverflow.com/questions/57598276/how-do-i-pass-a-multi-part-body-parameter-for-rxjava2-androidnetworking-post-req –  Aug 24 '19 at 10:33