代码改变世界

通过URl直接获取URL页面的内容

2017-07-26 09:49  Lose丶word  阅读(869)  评论(0编辑  收藏  举报

StringBuffer resultLR = new StringBuffer();
try {
  URL url = new URL("xxx");
try {
  //获得此 URL 的内容
  URLConnection conn = url.openConnection();
  InputStream is = conn.getInputStream();
  byte[] b = new byte[1024];
  int len = 0;
  String str = "";
  while((len=is.read(b))!=-1){
    str = str + new String(b,0,len,"UTF-8");
  }


  // 把获取的内容转为 JSONObject
  JSONObject json = JSONObject.fromObject(str);

  // 获取json的一个数组
  JSONArray jsonArray = JSONArray.fromObject(json.getString("features"));
  if (jsonArray != null && jsonArray.size() > 0) {
    String sresult = "";
    for (int i=0;i<jsonArray.size();i++) {

    }
  }


} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}