上传改造图像但返回空服务器响应

Retrofit image is uploaded but empty server response is returned

我无法使用改造获得响应,但它给出了成功响应:

Response{protocol=http/1.1, code=201, message=Created, url=http://test.some_url.in/api/upload/user/PostUserImage/}

并且图像已成功上传到服务器但是 响应正文或使用 Gson 均未显示任何内容:

我正在使用以下代码:

private void uploadImage(byte[] imageBytes) {

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);

RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), imageBytes);

MultipartBody.Part body = MultipartBody.Part.createFormData("image", "unnamed.jpg", requestFile);
Call<Response> call = retrofitInterface.uploadImage(body);
mProgressBar.setVisibility(View.VISIBLE);
call.enqueue(new Callback<Response>() {
    @Override
    public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {

        mProgressBar.setVisibility(View.GONE);

        Log.v("keys","-----response---------"+response);
        if (response.isSuccessful()) {

            Response responseBody = response.body();
            mBtImageShow.setVisibility(View.VISIBLE);
            assert responseBody != null;

        } else {

            ResponseBody errorBody = response.errorBody();

            Gson gson = new Gson();

            assert errorBody != null;
            Response errorResponse = gson.fromJson(errorBody.toString(), Response.class);


        }
    }

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

        mProgressBar.setVisibility(View.GONE);
        Log.d(TAG, "onFailure: "+t.getLocalizedMessage());
    }
});
}

请告诉我如何获得服务器响应.....

您可以使用 HttpLoggingInterceptor 在 logcat

中打印整个请求和响应
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();

OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
                    .connectTimeout(2, TimeUnit.MINUTES)
                    .writeTimeout(2, TimeUnit.MINUTES)
                    .readTimeout(2, TimeUnit.MINUTES)
                    .build();

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(okHttpClient)
        .build();

用于日志记录拦截器

implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'

RequestBody 中,您传递 File 而不是字节数组

RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), imageBytes);

为了从服务器获得正确的 raw 响应(ErrorSuccessResponseBody 作为 Response

的通用 class

因此您可以像这样更新您的代码:

 Call<ResponseBody> call = retrofitInterface.uploadImage(body);
    mProgressBar.setVisibility(View.VISIBLE);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {

            mProgressBar.setVisibility(View.GONE);

           // Log.v("keys","-----response---------"+response);
            if (response.isSuccessful()) {

               // Response responseBody = response.body();
                //mBtImageShow.setVisibility(View.VISIBLE);
                //assert responseBody != null;

                 try{
                Log.d("response","data: "+response.body().string())
              }catch(Exception e){
                e.printStackTrace();
                  }

            } else {

               // ResponseBody errorBody = response.errorBody();

                //Gson gson = new Gson();

                //assert errorBody != null;
                //Response errorResponse = gson.fromJson(errorBody.toString(), Response.class);


            try{
               Log.e("response","error: "+response.errorBody().string())
              }catch(Exception e){
               e.printStackTrace();
               }
            }
        }

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

            mProgressBar.setVisibility(View.GONE);
            Log.d(TAG, "onFailure: "+t.getMessage());
        }
    });

希望您能得到正确的回复