调用中央气象台提供的开放接口简单实现获取天气信息

调用中央气象台提供的开放接口简单实现获取天气信息

package WeatherInfo; import java.net.URL; import java.util.ArrayList; import GeographicalLocation.GeoLocation; import MyHttpClient.MyHttpClient; public class CityCode { private String code_province; private String code_district; private String code_city; private String city; private String district; private String province; public  CityCode(String province,String district,String city){ this.city = city; this.district = district; this.province = province; //System.out.println(province+" "+district+" "+city); } public String getCodeByURL(String location,String url){ String code = null; try { MyHttpClient httpClient_weather = new MyHttpClient(); String str = httpClient_weather.getContentByURL(url); //System.out.println(str); String[] strArray = str.split(","); for(int i = 0;i < strArray.length;i++){ //System.out.println(strArray[i]); String[] strArr = strArray[i].split("\\|"); if(location.indexOf(strArr[1]) != -1){ code = strArr[0]; //System.out.println(code); } } } catch (Exception e) { // TODO: handle exception } return code; } public String getCodeByURL(String url){ String code = null; try { MyHttpClient httpClient_weather = new MyHttpClient(); String str = httpClient_weather.getContentByURL(url); //System.out.println(str); String[] strArray = str.split("\\|"); code = strArray[1]; //System.out.println(code) } catch (Exception e) { // TODO: handle exception } return code; } public void getProvinceCode(){ String url_province = "http://m.weather.com.cn/data5/city.xml"; code_province = this.getCodeByURL(province, url_province); //System.out.println(code_province); } public void getDistrictCode(){ try { URL url_district = new URL("http://m.weather.com.cn/data5/city"+code_province+".xml"); String urlStr = url_district.toString(); code_district = this.getCodeByURL(district,urlStr); //System.out.println(code_district); } catch (Exception e) { // TODO: handle exception } } public void getCityCode(){ try { this.getProvinceCode(); this.getDistrictCode(); URL url_city = new URL("http://m.weather.com.cn/data5/city"+code_district+".xml"); String urlStr = url_city.toString(); code_city = this.getCodeByURL(city,urlStr); //System.out.println(code_city); } catch (Exception e) { // TODO: handle exception } } public String getWeatherCode(){ String code_weather = null; getCityCode(); try { URL url_weatherCode = new URL("http://m.weather.com.cn/data5/city"+code_city+".xml"); String urlStr = url_weatherCode.toString();  code_weather = this.getCodeByURL(urlStr); //System.out.println(code_weather); return code_weather; } catch (Exception e) { return null;// TODO: handle exception } } public static void main(String[] args){ GeoLocation myGeoLocation = new GeoLocation(); ArrayList<String> location = myGeoLocation.getGeoLocation(); //System.out.println(province+" "+district+" "+city); //String url = "http://m.weather.com.cn/data5/city.xml"; CityCode cityCode = new CityCode(location.get(0),location.get(1),location.get(2)); //cityCode.getCodeByURL(province, url); cityCode.getCityCode(); String cityCode_weather = cityCode.getWeatherCode(); String urlStr = null; try { URL url = new URL("http://m.weather.com.cn/data/"+cityCode_weather+".html"); //URL url = new URL("http://m.weather.com.cn/data/"+101010100+".html"); urlStr = url.toString(); System.out.println(urlStr); } catch (Exception e) { // TODO: handle exception } } } package WeatherInfo; import java.net.URL; import java.util.ArrayList; import net.sf.json.JSONObject; import GeographicalLocation.GeoLocation; import MyHttpClient.MyHttpClient; public class WeatherInfo { public String urlProducer(){ String urlStr = null; GeoLocation myGeoLocation = new GeoLocation(); ArrayList<String> mylocation = myGeoLocation.getGeoLocation(); //System.out.println(province+" "+district+" "+city); CityCode my_CityCode = new CityCode(mylocation.get(0),mylocation.get(1),mylocation.get(2)); //my_CityCode.getCityCode(); String cityCode_weather = my_CityCode.getWeatherCode(); try{ URL url = new URL("http://m.weather.com.cn/data/"+cityCode_weather+".html"); //URL url = new URL("http://m.weather.com.cn/data/"+101010100+".html"); urlStr = url.toString(); //System.out.println(urlStr); } catch (Exception e) { // TODO: handle exception } return urlStr; } public void getWeatherInfo(){ String urlStr = this.urlProducer(); MyHttpClient myHttpClient = new MyHttpClient(); String weatherInfo = myHttpClient.getContentByURL(urlStr); //System.out.println(weatherInfo); this.parseJSONData(weatherInfo); } private void parseJSONData(String weatherInfo){ try { JSONObject data = JSONObject.fromObject(weatherInfo).getJSONObject("weatherinfo"); //JSONObject data = obj.getJSONObject("weatherinfo"); //The following data can be put in a ArrayList or a HashMap;s String city = data.getString("city"); String date = data.getString("date_y"); String week = data.getString("week"); String temp = data.getString("temp1"); String weather = data.getString("weather1"); String wind = data.getString("wind1"); //String fx = data.getString("fx1"); String fl = data.getString("fl1"); String index = data.getString("index"); String index_d = data.getString("index_d"); String index_uv = data.getString("index_uv"); String index_co = data.getString("index_co"); System.out.println("城市: "+city); System.out.println(date+"  "+week); System.out.println("气温: "+temp); System.out.println("天气: "+weather); System.out.println("风速: "+wind); System.out.println("风速级别: "+fl); System.out.println("穿衣指数: "+index+","+index_d); System.out.println("紫外线: "+index_uv); System.out.println("舒适指数: "+index_co); } catch (Exception e) { // TODO: handle exception } } public static void main(String[] args){ WeatherInfo weatherInfo = new WeatherInfo(); weatherInfo.getWeatherInfo(); } }

转载于:https://my.oschina.net/u/1184080/blog/190515

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109937.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ubuntu GPG error 解决办法

    ubuntu GPG error 解决办法下载公钥:添加公钥:key-server: keyserver.Ubuntu.com

    2022年7月3日
    23
  • Pytest(6)重复运行用例pytest-repeat「建议收藏」

    Pytest(6)重复运行用例pytest-repeat「建议收藏」前言平常在做功能测试的时候,经常会遇到某个模块不稳定,偶然会出现一些bug,对于这种问题我们会针对此用例反复执行多次,最终复现出问题来。自动化运行用例时候,也会出现偶然的bug,可以针对单个用例,

    2022年7月31日
    3
  • 飞机订票系统源代码

    飞机订票系统源代码问题及代码:/****************************************************  *版权所有(C)2017,张思琦 *文件名称:飞机订票系统  *文件标识:无  *内容摘要:实现录入航班信息、订票、退票、预约、 *          查询航班、查询订单、查看预约、修改航班功能。 *其他内容:无  *当前版本:VC++6.0  *作    者:

    2022年6月16日
    27
  • Prometheus TSDB存储原理

    Prometheus TSDB存储原理Python 微信订餐小程序课程视频 https blog csdn net m0 article details Python 实战量化交易理财系统 https blog csdn net m0 article details Prometheus 包含一个存储在本地磁盘的时间序列数据库 同时也支持与远程存储系统集成 比如 grafanacloud 提供的免费云存储 API 只需将 remote write 接口信息填写在 Prome

    2025年7月16日
    4
  • c语言编程 sort()什么意思,void sort在C语言中什么意思?「建议收藏」

    c语言编程 sort()什么意思,void sort在C语言中什么意思?「建议收藏」void是C语言中的一个关键字,其含义因其应用形式不同,一般分为两种:void和void*void的字面意思是“无类型”,void*则为“无类型指针”,void*可以指向任何类型的数据。void几乎只有“注释”和限制程序的作用,定义一个void变量没有意义,不妨试着定义:这行语句编译时会出错,提示“illegaluseoftype’void’”。不过,即使voida的编译不会出错,…

    2022年5月12日
    48
  • struts2拦截器不起作用「建议收藏」

    struts2拦截器不起作用「建议收藏」为什么拦截器不起作用

    2022年10月6日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号