Android: Cannot load multi-page image file using Retrofit 2.0.0 beta 2

Hi, I cannot upload an image using retrofit 2.0.0-beta2. This gives me the error "Image missing." Although I checked that the file exists in the system, and also if I show images in the view, it displays correctly.

URL to which it should be loaded: http://mywebsite.com/signature/upload/

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

My .gradle file has the following:

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'

Class SeriveGenerator.java -

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

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());
        }
    });
}

, "" , , Postman, , .

+1
1

. .

. . @Part("picture\"; "" - , ( ). , Backend. =\ "image \" . "DiaryService.java".

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

. Mediatype. "multipart/form-data", . 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());
            }
        });
+1

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


All Articles