当响应不成功时,如何读取改装中的错误正文



我在Android中使用改装来执行api。

样本片段

Call<UniversalPojo> call = apiInterface.storeData(AppClass.getInstance().getLoggedInUser().getRemember_token(), requestBody);
call.enqueue(new Callback<UniversalPojo>() {
@Override
public void onResponse(Call<UniversalPojo> call, Response<UniversalPojo> response) {

if (response.isSuccessful()) {

} else {

//I want to read code at this stage in string.

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

t.printStackTrace();

}
});

我的问题是如何在if(response.isSuccessful(((.的else块中获取String中的错误

使用OkHttpClient类的addInterceptor(interceptor: Interceptor)函数

覆盖intercept(chain: Interceptor.Chain)函数并按预期抛出异常:

class NetInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
val response = chain.proceed(request)
val code = response.code
val body = response.body
// if body is null or something unexpected
throw IOException("receive empty body")
// else do nothing
}
}