第二次命中API时出现意外的流错误结束

问题描述 投票:0回答:1

我正在尝试Okhttp3库,我是新手。我阅读了一些教程及其工作正常,直到弹出这个用例:

我有一个外部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 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)
java android android-asynctask okhttp3
1个回答
0
投票

试试这个

OkHttpClient client = new OkHttpClient.Builder()
    .retryOnConnectionFailure(true)
    .build();


   try {
         if (response.isSuccessful()) 
              responseBody = response.body().string();
       } finally {
         response.close();
       }

来源here

© www.soinside.com 2019 - 2024. All rights reserved.