abbas jafari
abbas jafari

Reputation: 81

How to upload image and data with retrofit 2 library?

I want to post an image with some data like this postman image:

Inside body:

enter image description here

And inside header:

enter image description here

And I write some code for uploading image like postman but I got this error:

okhttp3.internal.http2.ConnectionShutdownException

And here is my code as below:

    File globalFileName;

    RequestBody requestBody1 = RequestBody.create(MediaType.parse("*/*"), globalFileName);

    MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("uploadFile",
            globalFileName.getName(), requestBody1);

    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
    RequestBody type = RequestBody.create(MultipartBody.FORM, "documents");
    RequestBody token = RequestBody.create(MultipartBody.FORM, "7220A3B7F8D2FD2C236092E0918B4EA3");

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.protocols( Collections.singletonList(Protocol.HTTP_1_1) );
    Call<ServerResponse> call = getResponse.uploadFile(userToken, fileToUpload1, type, token);
    call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
            ServerResponse serverResponse = response.body();
            Log.v(TAG, "Mahdi: Retrofit 2 onResponse: 0 " + serverResponse.toString());
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
            Log.e(TAG, "Mahdi: Retrofit 2 onFailure: ", t);
        }
    });

ApiConfig Interface code:

public interface ApiConfig {
    @Multipart
    @POST("/upload")
    Call<ServerResponse> uploadFile(
            @Header("token") String userToken,
            @Part MultipartBody.Part file,
            @Part("category") RequestBody documents,
            @Part("token") RequestBody token
    );
}

ServerResponse class code:

public class ServerResponse {
    // variable name should be same as in the json response from php  
    @SerializedName("status")
    boolean status;
    @SerializedName("message")
    String message;
    public String getMessage() {
        return message;  
    }  
    public boolean getSuccess() {
        return status;
    }  
}

AppConfig class code:

public class AppConfig {
    private static String BASE_URL = "https://taxiappapi.webfumeprojects.online";
    public static Retrofit getRetrofit() {
        return new Retrofit.Builder().baseUrl(AppConfig.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }
}

And I used this package:

implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'

please help me thanks.

Upvotes: 0

Views: 164

Answers (1)

mehul chauhan
mehul chauhan

Reputation: 1802

The correct structure for upload image retrofit like below but in your case i thing passing token causes issue

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

    MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("uploadFile",
            globalFileName.getName(), requestBody1);

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

    RequestBody token = RequestBody.create(MediaType.parse("multipart/form-data"), 
    "7220A3B7F8D2FD2C236092E0918B4EA3");

    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);

Upvotes: 1

Related Questions