资 源 简 介
Android 使用HttpURLConnection打开连接读取内容(流),使用循环来读取获得的数据:
try
{
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 得到读取的内容(流)
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
// 为输出创建BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
// 使用循环来读取获得的数据
while (((inputLine = buffer.readLine()) != null))
{
// 我们在每一行后面加上一个"
"来换行
resultData += inputLine + "
";
}
// 关闭InputStreamReader
in.close();
// 关闭http连接
urlConn.disconnect();
// 设置显示取得的内容
if (resultData != null)
{
mTextView.setText(resultData);
}
else
{
mTextView.setText("读取的内容为NULL");
}
}