为什么我的后期改造请求无效?

时间:2019-11-18 11:21:12

标签: java android retrofit

我有一个应上传以下参数的应用

enter image description here

enter image description here

这是我的代码

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

[{enter image description here 3

 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

问题是该帖子无法正常运行,并且没有告诉我为什么我的代码中有一个断点,因此BodyRequest是构建的,但是当涉及到调用时,它只是跳到结尾,即onResponse()并且onFailure()跳过代码似乎可以正常工作,并且该应用程序未挂起或未给出异常

感谢任何帮助朋友

1 个答案:

答案 0 :(得分:1)

此问题是您尝试通过多部分传递数据,这很容易解决 添加此库

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

这个转换器工厂

.addConverterFactory(ScalarsConverterFactory.create())在改造生成器中 .sample代码在下面

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