根据网页地址获取页面内容

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/p/2446121.html