温馨提示×

温馨提示×

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

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

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

发布时间:2020-04-08 22:03:31 阅读:492 作者:王村桥 栏目:开发技术
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>
private String b; new Thread(new Runnable() {             public void run() {                  String a= connServerForResult("http://m.weather.com.cn/data/101090101.html");                 final String v= "石家庄?"+parseJson(a);                 System.out.println("zheliv"+v);                 tq.post(new Runnable(){                     public void run(){                         tq.setText(v.replace("?","\n"));                              System.out.println("zheliv"+v);                     }});}}).start();}    private String connServerForResult(String strUrl) {     //获取HttpGet对象     HttpGet httpRequest = new HttpGet(strUrl);     String strResult = "";     try {     // HttpClient对象     HttpClient httpClient = new DefaultHttpClient();     // 获得HttpResponse对象     HttpResponse httpResponse = httpClient.execute(httpRequest);     if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {     // 取得返回的数据     strResult = EntityUtils.toString(httpResponse.getEntity());     System.out.println("zheli");     }     } catch (ClientProtocolException e) {     e.printStackTrace();     } catch (IOException e) {     e.printStackTrace();     }     Log.i("Infor", strResult);     return strResult; //返回结果     }     //对于返回的结果我们通过Json解析工具进行解析。下面是解析函数的代码,其参数就是要解析的Json格式数据字符串。  private String parseJson(String strResult) {     try {     JSONObject jsonObj = new JSONObject(strResult).getJSONObject("weatherinfo");      b =jsonObj.getString("temp1"); //当前日期     //dayofweek= jsonObj.getString("week"); //当前星期     //city.setText(jsonObj.getString("city")); //城市名称     //ftime= jsonObj.getInt("fchh"); //更新时间(整点)【更新时间确定temp1属于哪天】      //由于数据较多此处省略了部分代码,其他数据解析方法相同,大家可以照葫芦画瓢。     System.out.println("zhelinn");     } catch (JSONException e) {     Log.i("Erorr","Json parse error");     e.printStackTrace();}     System.out.println("zheli  v"+b);         return b;  }} //时间紧张,就读取了一个字符串,其他雷同 

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

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

AI

开发者交流群×