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

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

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


相关推荐

  • Java与C++的优劣:谁更胜一筹?零基础实习学C++还是java呢?

    Java与C++的优劣:谁更胜一筹?零基础实习学C++还是java呢?很多人都说会C++就能会快掌握Jave。C++是不好学,但是我告诉你java也不好学。C++难是难在语言本身,java难是难在各种框架和库。你单纯学个java语法,你什么玩意也做不了,java的东西都在java之外。上图仅供娱乐,不过今天我们主要探讨Java与C++间的优劣。基础介绍Java与C++都拥有相当长的历史,且语法相当类似,并被众多业界巨头所采用。事实上,Java本来就是从C++衍生出来的。不过尽管相似,二者之间仍然存在显著区别。很多人都说会C++就能会快掌…

    2022年7月8日
  • ASP.NET报错:failed due to the following error: 8000401a.「建议收藏」

    ASP.NET报错:failed due to the following error: 8000401a.「建议收藏」出现错误的原因是:配置标识不正确,系统无法开始服务器进程解决方案:1.开始-dcomcnfg,启动组件服务。2.找到MicrosoftExcelApplication、MicrosoftOfficePowerPoint幻灯片、MicrosoftOfficeWord97-2003文档,-属性-安全-自定义,添加启动和激活权限、访问权限,并添加NETWORKSER…

    2022年7月25日
  • 一键批量打印EXCEL、WORD文档

    一键批量打印EXCEL、WORD文档

    2021年10月10日
  • EPPlus 使用小结

    EPPlus 使用小结文章目录简介导入导出简单导出样式格式化其他总结简介EPPlus是一个使用OpenOfficeXML(xlsx)文件格式,能读写Excel2007/2010文件的开源组件,在导出Excel的时候不需要电脑上安装office,它的一个缺点就是不支持导出2003版的Excel(xls)。导入这部分相对简单,直接看下代码:using(ExcelPackagepackage=newExcelPackage(existingFile)){ExcelWorksheetworksh

    2022年6月15日
  • 广东省-IT红黑榜排名公司名称

    广东省-IT红黑榜排名公司名称

    2021年12月17日
  • c语言将时速转换成配速,配速与时速换算(跑步配速和时速换算)

    c语言将时速转换成配速,配速与时速换算(跑步配速和时速换算)时间除以路程,一般时间单位用分,路程单位用千米。比如20分钟跑了3千米,那配速就是20/3=6.66,也就是6分40秒每公里,一般说成640的配速。配速是针对长跑来说的,对短跑和中跑是不采用这个概念的。马拉松运动讲究匀速,在匀速的状态下才能更好地发挥自己的实力。很多跑步爱好者很注意控制速度。他们根.时速的意思是当时跑步的速度,配速是平均每公里耗时几分钟消耗是指跑步消耗的热量相当于每小时跑多少千…

    2022年6月28日

发表回复

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

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