80,349
社区成员
发帖
与我相关
我的任务
分享
public String executeGet() throws Exception {
String httpUrl = "http://www.baidu.com/";
String resultData = "";
URL url = null;
try{
url = new URL(httpUrl);// 构造一个URL对象
}catch (MalformedURLException e){
}
if (url != null){
try{
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 使用HttpURLConnection打开连接
InputStream inputStream = urlConn.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);// 得到读取的内容(流)
BufferedReader buffer = new BufferedReader(in);// 为输出创建BufferedReader
String inputLine = null;
while (((inputLine = buffer.readLine()) != null)){// 使用循环来读取获得的数据
resultData += inputLine + "\n";// 我们在每一行后面加上一个"\n"来换行
}
in.close();// 关闭InputStreamReader
urlConn.disconnect();// 关闭http连接
if (resultData != null){
System.out.print(resultData);
// mTextView.setText(resultData);// 设置显示取得的内容
}else{
// mTextView.setText("读取的内容为NULL");
System.out.print("读取的内容为NULL");
}
}catch (IOException e){
// Log.e(DEBUG_TAG, "IOException");
}
}else{
// Log.e(DEBUG_TAG, "Url NULL");
}
return resultData;
}