我正在嘗試Okhttp3庫,我對它很陌生。我讀了一些教程,它的工作正常,直到這個用例彈出:第二次打到API的流錯誤意外結束
我有一個外部Asynctask類,我正在執行我的POST和GET代碼。所以當我第一次打電話給Asynctask時,它工作正常。第二次我打電話給相同的Asynctask它給我的錯誤java.io.IOException:在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 [email protected]
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我已經發布我的日誌 – raphPradhan
如果使用OkHttp異步請求,你並不需要一個的AsyncTask。使用'client.newCall(request).enqueue' ...請參閱https://github.com/square/okhttp/wiki/Recipes#asynchronous-get –