解析json获取天气信息(中央气象台)

发布时间:2020-04-08 22:03:31 作者:王村桥
来源:网络 阅读:474

 

  1. private String b; 
  2. new Thread(new Runnable() { 
  3.             public void run() { 
  4.                  String aconnServerForResult("http://m.weather.com.cn/data/101090101.html"); 
  5.                 final String v"石家庄?"+parseJson(a); 
  6.                 System.out.println("zheliv"+v); 
  7.                 tq.post(new Runnable(){ 
  8.                     public void run(){ 
  9.                         tq.setText(v.replace("?","\n"));      
  10.                         System.out.println("zheliv"+v); 
  11.                     }});}}).start();} 
  12.    private String connServerForResult(String strUrl) { 
  13.     //获取HttpGet对象 
  14.     HttpGet httpRequest = new HttpGet(strUrl); 
  15.     String strResult = ""
  16.     try { 
  17.     // HttpClient对象 
  18.     HttpClient httpClient = new DefaultHttpClient(); 
  19.     // 获得HttpResponse对象 
  20.     HttpResponse httpResponse = httpClient.execute(httpRequest); 
  21.     if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { 
  22.     // 取得返回的数据 
  23.     strResult = EntityUtils.toString(httpResponse.getEntity()); 
  24.     System.out.println("zheli"); 
  25.     } 
  26.     } catch (ClientProtocolException e) { 
  27.     e.printStackTrace(); 
  28.     } catch (IOException e) { 
  29.     e.printStackTrace(); 
  30.     } 
  31.     Log.i("Infor", strResult); 
  32.     return strResult; //返回结果 
  33.     } 
  34.     //对于返回的结果我们通过Json解析工具进行解析。下面是解析函数的代码,其参数就是要解析的Json格式数据字符串。 
  35.  private String parseJson(String strResult) { 
  36.     try { 
  37.     JSONObject jsonObj = new JSONObject(strResult).getJSONObject("weatherinfo"); 
  38.      b =jsonObj.getString("temp1"); //当前日期 
  39.     //dayofweekjsonObj.getString("week"); //当前星期 
  40.     //city.setText(jsonObj.getString("city")); //城市名称 
  41.     //ftimejsonObj.getInt("fchh"); //更新时间(整点)【更新时间确定temp1属于哪天】 
  42.      //由于数据较多此处省略了部分代码,其他数据解析方法相同,大家可以照葫芦画瓢。 
  43.     System.out.println("zhelinn"); 
  44.     } catch (JSONException e) { 
  45.     Log.i("Erorr","Json parse error"); 
  46.     e.printStackTrace();} 
  47.     System.out.println("zheli  v"+b); 
  48.         return b; 
  49.  }} 
  50. //时间紧张,就读取了一个字符串,其他雷同
     

 

推荐阅读:
  1. libcurl第三课 HTTP获取天气信息
  2. python爬取哈尔滨天气信息

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

信息 天气 中央气象台

上一篇:Centos6.5 安装Apache下的Thrift

下一篇:datagrid(数据表格)Query EasyUI中文帮助手册

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》