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
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, , .