DataOutputSteam给我一个’java.io.IOException:意外的流结束’?
发布时间:2020-05-25 11:07:14 所属栏目:Java 来源:互联网
导读:我正在尝试使用HttpUrlConnection从 Android应用程序发出请求到WebService.但有时候它有效,有时它不起作用. 当我尝试发送此值: JSON值 {Calle:Calle Pérez 105,DetalleDireccion:,HoraPartida:May 18, 2014 9:17:10 AM,Numero:0,PuntoPartidaL
|
我正在尝试使用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();
...
}
解决方法以下是以下更改的解决方案:它摆脱了DataOutputStream,这当然是错误的使用. 尝试一下: // 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","application/json; charset=UTF-8");
OutputStream os = httpCon.getOutputStream();
os.write(bytes);
os.close();
...
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
