【发布时间】:2019-02-26 20:37:44
【问题描述】:
我正在尝试 Okhttp3 库,但我是新手。在弹出这个用例之前,我阅读了一些教程并且它工作正常:
我有一个外部 Asynctask 类,我在其中执行我的 POST 和 GET 代码。因此,当我第一次调用 Asynctask 时,它工作正常。我第二次调用同一个 Asynctask 它给了我错误 java.io.IOException: unexpected end of stream on okhttp3.Address.....
有人能解释一下为什么会这样吗?我在活动中创建 OkHttpClient 对象并将其传递给 Asynctask。
这是构造函数:
public Post(JsonObject jsonParams){
this.jsonParams = jsonParams;
}
这是后台任务:
Response response = null;
try {
OkHttpClient client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
RequestBody body = RequestBody.create(JSON, jsonParams);
Request.Builder requestBuilder = new Request.Builder()
.url(url)
.post(body)
.addHeader(APP_AUTHORIZATION_KEY, AUTHORIZATION_KEY);
Request request = requestBuilder.build();
response = client.newCall(request).execute();
if (response.isSuccessful()) {
responseBody = response.body().string();
Log.v("POST RESPONSE", "response" + responseBody);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (response != null)
response.close();
}
我正在发布我的日志:
java.io.IOException: unexpected end of stream on okhttp3.Address@d8a95c17
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:199)
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:125)
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:775)
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:86)
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:760)
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:613)
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.RealCall.getResponse(RealCall.java:244)
12-02 10:30:11.544 20214-22391/com.test.network W/System.err: at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:201)
12-02 10:30:11.564 20214-22391/com.test.network W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
12-02 10:30:11.574 20214-22391/com.test.network W/System.err: at okhttp3.RealCall.execute(RealCall.java:57)
12-02 10:30:11.574 20214-22391/com.test.network W/System.err: at com.test.network.tasks.Post.doInBackground(Post.java:60)
12-02 10:30:11.574 20214-22391/com.test.network W/System.err: at com.test.network.tasks.Post.doInBackground(Post.java:21)
12-02 10:30:11.584 20214-22391/com.test.network W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-02 10:30:11.584 20214-22391/com.test.network W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-02 10:30:11.584 20214-22391/com.test.network W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-02 10:30:11.584 20214-22391/com.test.network W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-02 10:30:11.584 20214-22391/com.test.network W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-02 10:30:11.604 20214-22391/com.test.network W/System.err: at java.lang.Thread.run(Thread.java:841)
12-02 10:30:11.604 20214-22391/com.test.network W/System.err: Caused by: java.io.EOFException: \n not found: size=0 content=…
12-02 10:30:11.614 20214-22391/com.test.network W/System.err: at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:215)
12-02 10:30:11.614 20214-22391/com.test.network W/System.err: at okhttp3.internal.http.Http1xStream.readResponse(Http1xStream.java:184)
【问题讨论】:
-
显示完整的日志帮助解决
-
@RajeshRajendiran 我已经发布了我的日志
-
如果使用 OkHttp 异步请求,则不需要 AsyncTask。使用
client.newCall(request).enqueue...见github.com/square/okhttp/wiki/Recipes#asynchronous-get -
另外,请注意:如果您仍然使用 OkHttp 处理 JSON 内容,请查看 Retrofit
-
@raphPradhan 试试我的answer
标签: java android android-asynctask okhttp3