Clone Retrofit 2拦截器中的响应对象

时间:2017-02-08 10:22:51

标签: android retrofit interceptor

问题

我想使用Custom Interceptor在代码中发送到onResponse之前捕获数据。 这是我的Interceptor代码:

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

        try {
            Response response = chain.proceed(request);
            Response savedResponse = response.newBuilder().build();

            if (savedResponse.isSuccessful()){
                String result = savedResponse.body().string();
                ApiInterceptor apiInterceptor = new Gson().fromJson(result, ApiInterceptor.class);

                Log.i("hieu", "");
                if (activity != null){
                    if (apiInterceptor.getApi_result() != null){
                        if (apiInterceptor.getApi_result().getMainte() == AppConstants.MAINTENANCE_CODE){
                            // show maintenance activity
                        }else{
                            if (apiInterceptor.getApi_result().getTransfer_status() == AppConstants.TRANSFERING_CODE){
                                // hien thi man hinh chuyen doi

                            }else{
                                if (apiInterceptor.getApi_result().getOnly_payment_status() == AppConstants.ONLY_PAYMENT_CODE){
                                    // hien thi man hinh qua han
                                }else{
                                    if (apiInterceptor.getApi_result().getResult() != 0){
                                        showMessage(apiInterceptor.getApi_result().getMessage());
                                    }else{
                                        // normal
                                    }
                                }
                            }
                        }
                    }
                }

            }else{
                showMessage(savedResponse.message());
            }

            return savedResponse;
        }catch (Exception e){
            LogUtils.d(SettingInterceptor.this.getClass().getName(), e.getMessage());
            if (activity != null)
                showMessage(activity.getString(R.string.network_error));
            throw e;
        }
    }

因为我知道在chain.proceed之后我只能访问一次响应,所以我想克隆该响应,以便我可以使用该响应访问和处理超过1次。所以我用了

Response savedResponse = response.newBuilder().build();

但是当我在调试模式下运行时,此代码运行的时间超过1次。

第一次,result返回正确的JSON数据。调试行运行到return savedResponse;后,它再次从Response savedResponse = response.newBuilder().build();savedResponse.isSuccessful() return success开始运行,但result返回空字符串。

如何解决?感谢。

1 个答案:

答案 0 :(得分:0)

您需要创建自定义回调。

自定义响应拦截器

public abstract class CustomCallbacks implements Callback {

    public abstract void onCustomResponse(Call call, Response response);

    public abstract void onCustomFailure(Call call, Throwable response);

    @Override
    public void onResponse(Call call, Response response) {
        //TODO: process response here
        if(response.isSuccessful()) {
            onCustomResponse(call, response);
        }else{
           onCustomFailure(call, new Exception("request failed"));
        }
    }

    @Override
    public void onFailure(Call call, Throwable t) {
        onCustomFailure(call, t);
    }
}

实施

call.enqueue(new CustomCallbacks() {
            @Override
            public void onCustomResponse(Call call, Response response) {

            }

            @Override
            public void onCustomFailure(Call call, Throwable response) {

            }
        });