2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 根据网页地址获取页面内容

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

时间:2019-06-09 03:26:42

相关推荐

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

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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。