DataOutputSteam正在给我一个“java.io.IOException:意外的流结束”?
2022-09-03 15:22:24
我正在尝试使用HttpUrlConnection从Android应用程序向WebService发出请求。但有时它有效,有时它不起作用。
当我尝试发送此值时:
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"}
我在DataOutputStream关闭函数中得到了一个“意外的流结束”异常。
这是我的代码:
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();
...
}