我正在尝试在我的应用中执行发布请求,但是我却不断收到表示“ Connection上的strean意外结束”的异常。我检查了我的请求结构几次,找不到任何错误。 如果有人可以指出我的要求出了什么问题,我将不胜感激。 预先感谢。
这是我的代码段:
@Override
protected JSONObject doInBackground(JSONObject... jsonObjects) {
JSONObject serverResponse = null;
String response = "";
String data = jsonObjects[0].toString();
try {
URL myURL = new URL(GET_INFO);
HttpURLConnection connection = (HttpURLConnection)myURL.openConnection();
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.setRequestProperty("Content-Type", "application/json");
OutputStream outputPost = new BufferedOutputStream(connection.getOutputStream());
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(outputPost, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
outputPost.close();
if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){
//
}
else{
Log.e("getDeviceInfo", "Error occurred while trying to connect to server");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return serverResponse;
}