加装2关闭错误响应主体

时间:2018-10-18 17:40:14

标签: android retrofit2

我正在使用改造2。

  

当我收到不成功的回复时,我必须手动关闭   erroBody();

public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
    // Success
} else {
    response.errorBody().close(); ???????????????????
}
 }

1 个答案:

答案 0 :(得分:0)

。响应失败时,无需调用response.errorBody().close()。以同样的方式,在成功响应期间无需调用response.raw().close();

call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call,
                                   @NonNull Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    // successful response from server: HTTP status code is between 200 and 300
                    // log user in, etc
                } else {
                    // unsuccessful response from server: HTTP status code is not between 200 and 300
                    // show toast, call onBackPressed(), etc
                }
            }

            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
                // network exception occurred while talking to the server
                // or an unexpected exception occurred while creating the request or processing the response

                // Log.d(...);
            }
        });