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

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

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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • armv6、armv7、armv7s及arm64

    armv6、armv7、armv7s及arm64指令集:指令集是存储在CPU内部,对CPU运算进行指导和优化的硬程序。拥有这些指令集,CPU就可以更高效地运行。Intel有x86,EM64T,MMX,SSE,SSE2,SSE3,SSSE3(SuperSSE3),SSE4.1,SSE4.2,AVX。AMD主要是x86,x86-64,3D-Now!指令集。Arm处理器,因为其低功耗和小尺寸而闻名,几乎所有的手机处理器都基

    2022年6月15日
    40
  • 隐马尔可夫模型实战

    隐马尔可夫模型实战

    2021年9月3日
    50
  • clion 激活_最新在线免费激活

    (clion 激活)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html0BXA05X8YC-eyJsaWNlbnNlSWQi…

    2022年3月30日
    41
  • 简单理解伽马校正

    简单理解伽马校正伽马校正相关的资料说明很多,但其中不少内容都写的比较繁杂,令人难以理解,本文尝试简单解释一下伽马校正的相关内容~早期的CRT显示器存在非线性输出的问题,简单来说,你给CRT显示器输入(input)一个0.5(**注意,输入范围为[0,1]),CRT显示器的输出(output)并不是0.5,而是约等于0.218,输入与输出间存在一个指数大概为2.2的幂次关系:outp…

    2022年9月25日
    3
  • Cocos2d-x lua游戏开发之安装Lua到mac系统

    Cocos2d-x lua游戏开发之安装Lua到mac系统

    2022年1月27日
    45
  • 计算机应用应届求职简历,计算机应用应届生个人简历模板

    计算机应用应届求职简历,计算机应用应届生个人简历模板计算机应用应届生个人简历模板计算机应用应届生个人简历模板一基本信息姓名:×××性别:男政治面貌:团员学历:中专毕业院校:江苏省盐城市职业技术学院专业:计算机应用技术出生日期:19xx.04.04电话:籍贯:江苏省射阳县海河镇复兴村邮箱:xxxxx工作年限:2.7工作職稱:助理领班教育经历20xx/09—20xx/07就读于江苏省盐城市职业技术学院主修课程计算机应用专业工作内容与专长1.负…

    2022年5月11日
    39

发表回复

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

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