gpt4 book ai didi

从 POST 获取InputStream 时出现 JAVA EOFException

转载 作者:行者123 更新时间:2023-12-01 06:44:49 25 4
gpt4 key购买 nike

我试图从 POST 获取响应,但是当我执行输入时抛出异常

     try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
// Marcamos a 3s el tiempo maximo de espera
conn.setReadTimeout(30000000);
conn.setUseCaches(true);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// post the request
// Abrimos una señal de salida
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.flush();
String buffer;
// Abrimos una señal de entrada
here is where android throws and exception
InputStreamReader ip = new InputStreamReader(conn.getInputStream());
BufferedReader in2 = new BufferedReader(ip);
// Bucle que recoge todas las respuestas

while ((buffer = in2.readLine()) != null) {
Log.e(" ", buffer);
}
// Cerramos las señales de entrada y salida
in2.close();
out.close();

} catch (IOException e) {
Log.e(TAG, "ERROR" + e.toString());
e.printStackTrace();
} finally {
conn.disconnect();
}

日志标签显示:

08-01 10:05:53.223: E/WooWMe GCM(798): ERRORjava.io.EOFException
08-01 10:05:53.223: W/System.err(798): java.io.EOFException
08-01 10:05:53.233: W/System.err(798): at libcore.io.Streams.readAsciiLine(Streams.java:203)
08-01 10:05:53.233: W/System.err(798): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:560)
08-01 10:05:53.233: W/System.err(798): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:813)
08-01 10:05:53.233: W/System.err(798): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
08-01 10:05:53.233: W/System.err(798): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-01 10:05:53.233: W/System.err(798): at com.egartec.woowme.ChatActivity$3.doInBackground(ChatActivity.java:230)
08-01 10:05:53.233: W/System.err(798): at com.egartec.woowme.ChatActivity$3.doInBackground(ChatActivity.java:1)
08-01 10:05:53.233: W/System.err(798): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-01 10:05:53.244: W/System.err(798): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-01 10:05:53.244: W/System.err(798): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-01 10:05:53.244: W/System.err(798): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-01 10:05:53.253: W/System.err(798): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-01 10:05:53.253: W/System.err(798): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-01 10:05:53.253: W/System.err(798): at java.lang.Thread.run(Thread.java:856)

最佳答案

如果你使用

conn.getInputStream()

每次,当您的请求不成功时,它都会抛出 java.io.FileNotFoundException ,基本上对于任何 400 或以上的 HTTP 响应代码。在这种情况下,您的响应正文位于

conn.getErrorStream()

因此,在决定从哪个流读取之前,您必须检查 HTTP 响应代码:

int status = conn.getResponseCode();
BufferedInputStream in;
if (status >= 400 ) {
in = new BufferedInputStream( conn.getErrorStream() );
} else {
in = new BufferedInputStream( conn.getInputStream() );
}

我没有包括从流中读取内容,因为这很简单:)

关于从 POST 获取InputStream 时出现 JAVA EOFException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17991347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com