打印okhttp3.ResponseBody(errorBody)后无法获取okhttp3.ResponseBody(errorBody)

时间:2019-07-17 11:23:47

标签: android retrofit2 okhttp3

Android Studio 3.4,java 1.8,Gradle 5.0

classpath 'com.android.tools.build:gradle:3.4.2'

在我的android项目中。 片段:

import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;   


  private static String getErrorBodyAsString(Response<?> response) {
        String errorBodyString = null;
        try {
            errorBodyString = response.errorBody().string();
        } catch (IOException e) {
            if (BuildConfig.DEBUG) {
                Log.e(TAG, e.getMessage(), e);
            }
        }
        return errorBodyString;
    }

    static ErrorResponse parseError(Response<?> response) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "parseError_response_RAW(lang = "
                    + Locale.getDefault().getLanguage() + ")"
                    + "\ncode = " + response.code()
                    + "\nmessage = " + response.message()
                    + "\nraw = " + response.raw()
            );
        }
        Retrofit retrofit = RestClientFactory.getRetrofit();
        Converter<ResponseBody, ErrorResponse> converter = retrofit.responseBodyConverter(ErrorResponse.class, new Annotation[0]);
        ErrorResponse errorResponse = new ErrorResponse();
        ResponseBody errorBody = response.errorBody();
        try {
            if (BuildConfig.DEBUG)
                Log.d(TAG, "parseError_errorBody = " + errorBody);
            errorResponse = converter.convert(errorBody);
            if (BuildConfig.DEBUG)
                Log.d(TAG, "parseError_errorResponse = " + errorResponse);
errorResponse.setCode(response.code());

,结果是logcat:

parseError_response_RAW(lang = en)
code = 401
message = Unauthorized
raw = Response{protocol=http/1.1, code=401, message=Unauthorized, url=http://myhost:8081//login)

parseError_errorBody = okhttp3.ResponseBody$1@b7a3476
parseError_errorResponse = ErrorResponse{code=0, message='Invalid login or password. Check your credentials and try again.', rawResponse=null}

很好,工作正常。

现在,在转换打印错误主体之前,我是这样的:

    static ErrorResponse parseError(Response<?> response) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "parseError_response_RAW(lang = "
                    + Locale.getDefault().getLanguage() + ")"
                    + "\ncode = " + response.code()
                    + "\nmessage = " + response.message()
                    + "\nerrorBody = " + getErrorBodyAsString(response)
                    + "\nraw = " + response.raw()
            );
        }
        Retrofit retrofit = RestClientFactory.getRetrofit();
        Converter<ResponseBody, ErrorResponse> converter = retrofit.responseBodyConverter(ErrorResponse.class, new Annotation[0]);
        ErrorResponse errorResponse = new ErrorResponse();
        ResponseBody errorBody = response.errorBody();
        try {
            if (BuildConfig.DEBUG)
                Log.d(TAG, "parseError_errorBody = " + errorBody);
            errorResponse = converter.convert(errorBody);
            if (BuildConfig.DEBUG)
                Log.d(TAG, "parseError_errorResponse = " + errorResponse);
errorResponse.setCode(response.code());

但是现在在这里导致logcat:

parseError_response_RAW(lang = en)
code = 401
message = Unauthorized
errorBody = {
  "en": "Invalid login or password. Check your credentials and try again."
}

raw = Response{protocol=http/1.1, code=401, message=Unauthorized, url=http://myhost:8081/login}
parseError_errorBody = okhttp3.ResponseBody$1@b383d50
parseError_errorResponse = null

为什么现在无法将json转换为ErrorResponse

如果我对此发表评论:

+ "\nerrorBody = " + getErrorBodyAsString(response)

然后再次将成功转换为 ResponseBody

我的代码有什么问题? 谢谢。

0 个答案:

没有答案