提问者:小点点

无法访问响应体的字符串表示


我有一个拦截器来监控我收到的所有服务器响应。在这个拦截器中,我想访问响应主体,然后是这个响应主体的字符串表示形式。

我的问题是string()方法返回一个IllegalStateException。

Interceptor authorizationInterceptor = chain -> {
        Request request = chain.request();
        Response response = chain.proceed(request);

        if (response != null) {
            // This is a Nullable
            ResponseBody responseBody = response.body();

            if (responseBody != null) {
                // This fails with an IllegalStateException and I cannot retrieve the string
                String responseBodyString = responseBody.string();
            }
            return response;
        } else {
            // Should not happen
            Timber.w("Cannot get the server response as a Response object");
        }


        return response;
};

但是,如果我执行以下操作,我可以检索字符串值:

String responseBodyString = response.body().string();

但我不再受到保护,免受response. body()为空…

我知道身体不能被阅读超过一次,但我认为这与我的问题无关。

如何保证响应体不为空,然后获取这个reponse体的字符串表示?

编辑

这是堆栈跟踪:

java.lang.IllegalStateException: closed
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:398)
at okio.RealBufferedSource.rangeEquals(RealBufferedSource.java:392)
at okhttp3.internal.Util.bomAwareCharset(Util.java:431)
at okhttp3.ResponseBody.string(ResponseBody.java:174)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
at okhttp3.RealCall.execute(RealCall.java:69)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
at io.reactivex.Observable.subscribe(Observable.java:10910)
at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
at io.reactivex.Observable.subscribe(Observable.java:10910)
at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)

共1个答案

匿名用户

这可能对你有帮助。

JSONArray arr=new JSONArray(结果); JSONObject jObj=arr.getJSONObject(0);字符串日期=jObj.getString("NeededString");