【问题标题】:Cannot access string representation of ResponseBody无法访问 ResponseBody 的字符串表示
【发布时间】:2018-06-19 11:02:49
【问题描述】:

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

我的问题是 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() 为空...

我知道正文无法读取more than once,但我认为这与我的问题无关。

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

编辑

这是堆栈跟踪:

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)

【问题讨论】:

  • 发表您的回复

标签: android interceptor okhttp3


【解决方案1】:

这可能会对你有所帮助。

JSONArray arr = new JSONArray(result); JSONObject jObj = arr.getJSONObject(0); String date = jObj.getString("NeededString");

【讨论】:

    猜你喜欢
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    相关资源
    最近更新 更多