Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 改造2在onFailure()中获取响应主体_Java_Android_Http_Networking_Retrofit - Fatal编程技术网

Java 改造2在onFailure()中获取响应主体

Java 改造2在onFailure()中获取响应主体,java,android,http,networking,retrofit,Java,Android,Http,Networking,Retrofit,当我无法从服务器解析json时,我会尝试收集情况。 使用实现拦截器的类,我可以看到服务器给了我什么 但是,我似乎无法获取“onFailure()”中的值,在这种情况下,我需要收集错误。因为它只提供“Call”和“Throwable”。如何从“onFailure()”中的服务器获取原始数据 下面是我的代码 伐木拦截器 public class LoggingInterceptor implements Interceptor { //로그에 쓰일 tag private static final

当我无法从服务器解析json时,我会尝试收集情况。 使用实现拦截器的类,我可以看到服务器给了我什么 但是,我似乎无法获取“onFailure()”中的值,在这种情况下,我需要收集错误。因为它只提供“Call”和“Throwable”。如何从“onFailure()”中的服务器获取原始数据

下面是我的代码

伐木拦截器

public class LoggingInterceptor implements Interceptor {

//로그에 쓰일 tag
private static final String TAG = CalyApplication.class.getSimpleName() + "/" + LoggingInterceptor.class.getSimpleName();

@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

    long t1 = System.nanoTime();
    Response response = chain.proceed(request);
    long t2 = System.nanoTime();
    String responseString = new String(response.body().bytes());

    //yes, I can see response in here. but I need it in 'onFailure()'.
    Logger.i(TAG, "code : " + response.code() + "\n" + responseString);


    return  response.newBuilder()
            .body(ResponseBody.create(response.body().contentType(), responseString))
            .build();
    }

}
实际性

void fetchData(){

    ApiClient.getService().test(
            "test"
    ).enqueue(new Callback<BasicResponse>() {
        @Override
        public void onResponse(Call<BasicResponse> call, Response<BasicResponse> response) {
            BasicResponse body = response.body();
            switch (response.code()){
                case 200:
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailure(Call<BasicResponse> call, Throwable t) {
            //I want get Response object in here!
            //but it only provides Call&Throwable
        }
    });
}
void fetchData(){
ApiClient.getService()测试(
“测试”
).enqueue(新的回调函数(){
@凌驾
公共void onResponse(调用、响应){
BasicResponse body=response.body();
开关(response.code()){
案例200:
打破
违约:
打破
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
//我想在这里得到响应对象!
//但它只提供呼叫和可丢弃功能
}
});
}
谢谢

如果获得4xx或5xx(错误)状态代码,则调用onResponse,而不是onFailure。只有在调用成功时,才会相应地获得响应正文(2xx)或错误正文。因此,在onResponse中,您应该具有以下结构:

if (response.isSuccessful()) {
   // Get response body
} else if (response.errorBody() != null) {
   // Get response errorBody 
   String errorBody = response.errorBody().string();
}

编辑:可以找到有关如何检索errorBody的更多信息。

是的,在200-500状态代码中调用onResponse()。但是,如果状态代码为200且json解析失败,则调用onFailure()。然后需要修复响应模型类。你为什么需要回复?您可以通过throwable对象记录/报告错误。我认为您无法在OnFailure中获得响应对象,这就是改型2.Callback接口的定义方式。如果(response.errorBody()!=null)仅供参考,可能会禁用