cmanuel1
cmanuel1

Reputation: 1

Why isn't my post retrofit request working?

I have an app that should upload the following parameters

enter image description here

enter image description here

and here I have my code

 @POST("task")
Call<ResponsetTask> API_Task(@Header("Authorization") String key, @Body RequestBody body);

[enter image description here3

and

 private void Analizar()  {
    File file=new File(path);



    RequestBody requestBody =RequestBody.create(MediaType.parse("image/jpg"),file);
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);

    builder.addFormDataPart("message", Constantes.MESSAGE);
    builder.addFormDataPart("filecomment", Constantes.FILECOMMENT);
    builder.addFormDataPart("api_token", Constantes.api_token);
    builder.addFormDataPart("user_id", Integer.toString(Constantes.id));
    builder.addFormDataPart("image","image.jpg",requestBody);

    MultipartBody body = builder.build();

    Call<ResponsetTask>call=conexion2.API_Task(Constantes.AUTH,body);
    call.enqueue(new Callback<ResponsetTask>() {
        @Override
        public void onResponse(Call<ResponsetTask> call, Response<ResponsetTask> response) {
            if(response.isSuccessful()){
                Constantes.api_task=response.body().getTaskId();
            }
        }

        @Override
        public void onFailure(Call<ResponsetTask> call, Throwable t) {

        }
    });


}

enter image description here

The problem is that the post does not work and does not tell me why I have a breakpoint in my code the BodyRequest is built but when it comes to the call it simply jumps to the end that is to say the onResponse () and the onFailure () skip the code seems work and the app does not hang or give Exception

I appreciate any help friends

Upvotes: 0

Views: 74

Answers (1)

Eldhopj
Eldhopj

Reputation: 3519

This issue is that you are trying to pass data with multipart, Its an easy fix Add this library

implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version"

and this convertor factory

.addConverterFactory(ScalarsConverterFactory.create()) in your retrofit builder .sample code is given below

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

Upvotes: 1

Related Questions