Android OkHttp3 java.io.IOException: 流关闭

Android OkHttp3 java.io.IOException: stream closed

我使用 OkHttp3 来 post 一个 json 和值 key 。我已经用 postman 尝试了 API url,它的工作。但这在我的 android app.It always print java.io.IOException: stream closed 中不起作用。我该怎么办?

protected String doInBackground(Void... voids) {
        Log.d("OKHttp3","Post Fuction");
        OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .retryOnConnectionFailure(true)
                .build();
        String Acc = Login_Text_Acc.getText().toString();
        String Pwd = Login_Text_Pwd.getText().toString();
        Log.d("123456",Acc);
        Log.d("123456",Pwd);
        RequestBody formBody = new FormBody.Builder()
                .add("userID", Acc)
                .add("pwd", Pwd)
                .build();
        Request request = new Request.Builder()
                .url(json_url)
                .post(formBody)
                .build();
        try {
            Response response = okHttpClient.newCall(request).execute();
            if (!response.isSuccessful()) {
                return null;
            }
            if(!response.body().string().equals(""))
            {
            return response.body().string();
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return e.toString();
        }

    }

这是错误代码:

01-15 20:22:22.269 23220-23249/android_serialport_api.sample W/System.err: java.io.IOException: stream closed
01-15 20:22:22.269 23220-23249/android_serialport_api.sample W/System.err:     at okhttp3.internal.framed.FramedStream$FramedDataSource.checkNotClosed(FramedStream.java:438)
01-15 20:22:22.269 23220-23249/android_serialport_api.sample W/System.err:     at okhttp3.internal.framed.FramedStream$FramedDataSource.read(FramedStream.java:343)
01-15 20:22:22.269 23220-23249/android_serialport_api.sample W/System.err:     at okio.ForwardingSource.read(ForwardingSource.java:35)
01-15 20:22:22.269 23220-23249/android_serialport_api.sample W/System.err:     at okio.Buffer.writeAll(Buffer.java:956)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at okio.RealBufferedSource.readByteArray(RealBufferedSource.java:92)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at okhttp3.ResponseBody.bytes(ResponseBody.java:83)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at okhttp3.ResponseBody.string(ResponseBody.java:109)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at android_serialport_api.Login.Login$BackgroundTask.doInBackground(Login.java:91)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at android_serialport_api.Login.Login$BackgroundTask.doInBackground(Login.java:51)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:295)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
01-15 20:22:22.270 23220-23249/android_serialport_api.sample W/System.err:     at java.lang.Thread.run(Thread.java:818)
01-15 20:22:22.270 23220-23220/android_serialport_api.sample D/result: java.io.IOException: stream closed

您正在尝试两次使用响应正文。您需要将正文读入字符串,然后将您的逻辑应用于该局部变量。

String body = response.body().string();
if (!body.equals("")) {
    return body;
}