根据网页地址获取页面内容
public JSONObject urlConn(String urlStr, String portStr){
String port = getPort(portStr);
urlStr = (urlStr != null)?(host + port + "/?" + urlStr):(host + port);
int responseCode;
try {
url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
responseCode = conn.getResponseCode();//獲取返回碼
if( responseCode == HttpURLConnection.HTTP_OK ){
is = conn.getInputStream();//獲取輸入流
//讀取數據流
bufferedReader = new BufferedReader(new InputStreamReader(is));
//建立字符串操作對象
builder = new StringBuilder();
String line = "";
while( (line = bufferedReader.readLine()) != null ){
builder.append(line);
}
bufferedReader.close();
is.close();
//json解析
jsonObject = new JSONObject(builder.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
轉載于:https://www.cnblogs.com/xingmeng/archive/2012/04/13/2446121.html
總結
以上是生活随笔為你收集整理的根据网页地址获取页面内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 做梦梦到蛇咬伤自己是啥意思
- 下一篇: 梦到蛇变猪什么意思