DolDurma
DolDurma

Reputation: 17279

Retrofit 2 simple upload file to server

i'm newbie in retrofit and in this code i want to upload file as an image or other format to server, i read Retrofit document about that, but i can't do that,

i can create provider and i can use that to POST or GET from server, but my problem is for that is upload file.

Thanks to advance

My SignalProvider class:

public class SignalProvider {
    private SignalRetrofitServiceProviders signalRetrofitServiceProviders;
    public SignalProvider(){
        OkHttpClient httpClient = new OkHttpClient();

        Retrofit retrofit = new Retrofit.Builder()
                 .baseUrl(ClientSettings.SignalWebBaseUrl)
                .client(httpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        signalRetrofitServiceProviders = retrofit.create(SignalRetrofitServiceProviders.class);
    }
    public SignalRetrofitServiceProviders getServices(){
        return signalRetrofitServiceProviders;
    }
}

My SignalRetrofitServiceProviders class:

public interface SignalRetrofitServiceProviders {
    @POST("storeLists")
    Call<List<StoreLists>> getStoreLists();

    @Multipart
    @POST("upload/newVitrine/{userId}")
    Call<ResponseBody> uploadImageToServer(
            @Path("id") int id,
            @Part("description")
            RequestBody description,
            @Part MultipartBody.Part file);
}

and then Upload file method:

private void uploadFile() {
    // create upload service client
    SignalProvider                 signalProvider = new SignalProvider();
    SignalRetrofitServiceProviders service        = signalProvider.getServices();

    File file = new File(Application.IMAGES + "/" + "test_image.jpg");

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

    MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

    String descriptionString = "hello, this is description speaking";
    RequestBody description = RequestBody.create(
            MediaType.parse("multipart/form-data"), descriptionString);

    Call<ResponseBody> call = service.uploadImageToServer("1",description, body);

    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call,
                Response<ResponseBody> response) {
            Log.e("Upload", "success");
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e("Upload error:", t.getMessage());
        }
    });
}

I get this error:

    Error:(112, 42) error: method uploadImageToServer in interface 
SignalRetrofitServiceProviders cannot be applied to given types;
    required: int,com.squareup.okhttp.RequestBody,Part
    found: int,okhttp3.RequestBody,Part
    reason: actual argument okhttp3.RequestBody cannot be converted to 
com.squareup.okhttp.RequestBody by method invocation conversion

Upvotes: 2

Views: 6544

Answers (1)

snkashis
snkashis

Reputation: 2991

I think one of the first major problems that needs to get fixed here is that your upload route @POST("upload/newVitrine/{userId}") includes userId in the path but your @Parts only include "id" and "description". Also, can we see your imports?

Upvotes: 1

Related Questions