【发布时间】:2019-11-10 12:56:01
【问题描述】:
我使用 BufferedReader 来处理网页的输出。当网页的输出为空时(我在网页端使用Response.Clear),最后一行Log.e("status","finish")什么都不做。 reader.readLine() 是否被困在空输出中?如果是,在使用阅读器之前,我应该如何检查response是否为空?
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");
【问题讨论】:
标签: android bufferedreader urlconnection