我使用BufferedReader处理网页的输出。当网页的输出为空时(我在网页端使用了Response.Clear
),最后一行Log.e("status","finish")
无任何作用。 reader.readLine()
是否卡在空输出中?如果是,在使用阅读器之前,我应该如何检查响应是否为空?
URLConnection connection = new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("accept-Charset","utf-8");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded;charset=" + "utf-8");
connection.connect(); // The code works same without this. Do I need this?
try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes("utf-8"));
Log.e("status","post Done"); // This works
}
InputStream response = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(response));
String line="";
while ((line = reader.readLine()) != null) {
urlData += line;
}
reader.close();
Log.e("status","finish");