Android OkHttp3 java.io.IOException:流已关闭

时间:2019-01-15 20:30:45

标签: java android okhttp3

我使用OkHttp3发布带有value键的json。我已经用邮递员尝试了API网址,它的工作原理。但这在我的android应用中无法正常工作,它总是打印java.io.IOException:stream close。我该怎么办?

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$2.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$1.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

1 个答案:

答案 0 :(得分:0)

您正在尝试消耗响应正文两次。您需要将主体读取为字符串,然后将逻辑应用于该局部变量。

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