DataOutputSteam 向我抛出“java.io.IOException:流意外结束”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23722659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataOutputSteam is throwing me a 'java.io.IOException: unexpected end of stream'?
提问by Laggel
I'm trying to make a Request to a WebService from an android application, using HttpUrlConnection. But sometimes it works, and sometimes it does not.
我正在尝试使用 HttpUrlConnection 从 android 应用程序向 WebService 发出请求。但有时它有效,有时则不起作用。
When I try sending this value:
当我尝试发送此值时:
JSON value
JSON 值
{"Calle":"Calle Pérez 105","DetalleDireccion":"","HoraPartida":"May 18, 2014 9:17:10 AM","Numero":0,"PuntoPartidaLat":18.477295994621315,"PuntoPartidaLon":-69.93638522922993,"Sector":"Main Sector"}
I got an "unexpected end of stream" Exception in the DataOutputStream close function.
我在 DataOutputStream 关闭函数中遇到了“意外的流结束”异常。
Here is my code:
这是我的代码:
DataOutputStream printout;
// String json;
byte[] bytes;
DataInputStream input;
URL serverUrl = null;
try {
serverUrl = new URL(Config.APP_SERVER_URL + URL);
} catch (MalformedURLException e) {
...
}
bytes = json.getBytes();
try {
httpCon = (HttpURLConnection) serverUrl.openConnection();
httpCon.setDoOutput(true);
httpCon.setUseCaches(false);
httpCon.setFixedLengthStreamingMode(bytes.length);
httpCon.setRequestProperty("Authorization", tokenType + " "+ accessToken);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type", "application/json");
printout = new DataOutputStream(httpCon.getOutputStream());
printout.writeBytes(json);
printout.flush();
printout.close();
...
}
采纳答案by Codo
Here's a solution with the following changes:
这是具有以下更改的解决方案:
- It gets rid of the DataOutputStream, which is certainly the wrong thing to use.
- It correctly sets and delivers the content length.
- It doesn't depend on any defaults regarding the encoding, but explicitly sets UTF-8 in two places.
- 它摆脱了DataOutputStream,这肯定是错误的使用方式。
- 它正确设置并提供内容长度。
- 它不依赖于任何关于编码的默认值,而是在两个地方明确设置 UTF-8。
Try it:
尝试一下:
// String json;
URL serverUrl = null;
try {
serverUrl = new URL(Config.APP_SERVER_URL + URL);
} catch (MalformedURLException e) {
...
}
try {
byte[] bytes = json.getBytes("UTF-8");
httpCon = (HttpURLConnection) serverUrl.openConnection();
httpCon.setDoOutput(true);
httpCon.setUseCaches(false);
httpCon.setFixedLengthStreamingMode(bytes.length);
httpCon.setRequestProperty("Authorization", tokenType + " "+ accessToken);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
OutputStream os = httpCon.getOutputStream();
os.write(bytes);
os.close();
...
}
回答by working
From the oracle documentation here. We know that flush method of DataOutputStream calls the flush method of the underlying output stream. If you look at the URLConnection class in hereit says that every subclass of URLConnection must have this method overridden. If you see the HttpUrlConnection herewe see that flush method is not overridden. It could be one of the reasons for your problem.
来自此处的 oracle 文档。我们知道DataOutputStream的flush方法调用了底层输出流的flush方法。如果您查看此处的 URLConnection 类,它表示 URLConnection 的每个子类都必须覆盖此方法。如果您在此处看到 HttpUrlConnection ,我们会看到flush 方法未被覆盖。这可能是您的问题的原因之一。