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

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

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


相关推荐

  • 打包的技巧图解(手机软件打包)

    我们打包APP需要用到HBuilder,所以先讲解如何安装使用HBuilder的下载与安装HBuilder的官网下载地址:https://www.dcloud.io/点击DOWNLOAD后会弹出如上显示的两种版本下载Windows版和MacOS版根据我们电脑版本下载即可,我们一定要下载标准版哦(正式版中的)我这里是Windows系统,所以我选择的是window版下载完是一个压缩包右击压缩一下即可打开文件夹后找到如下HBuilderX.exe文件双击即可运行为了方便我们

    2022年4月17日
    61
  • mysql删除约束

    mysql删除约束1、删除notnull约束altertable表名modify列名类型;2、删除unique约束altertable表名dropindex唯一约束名;3、删除primarykey约束altertable表名dropprimarykey;4、删除foreignkey约束altertable表名dropforeignkey外键名;…

    2022年6月24日
    25
  • Swoole的多进程模块[通俗易懂]

    Swoole的多进程模块

    2022年2月11日
    48
  • Netty系列之Netty安全性

    Netty系列之Netty安全性

    2022年3月12日
    31
  • layer弹出层详解

    layer弹出层详解前言:学习layer弹出框,之前项目是用bootstrap模态框,后来改用layer弹出框,在文章的后面,我会分享项目的一些代码(我自己写的)。layer至今仍作为layui的代表作,她的受众广泛并非偶然,而是这五年多的坚持,不断完善和维护、不断建设和提升社区服务,使得猿们纷纷自发传播,乃至于成为今天的Layui最强劲的源动力。目前,layer已成为国内最多人使用的web弹层组件,GitHub…

    2022年7月13日
    22
  • Python使用captcha制作验证码[通俗易懂]

    Python使用captcha制作验证码[通俗易懂]在使用Keras搭建验证码识别模型时,需要大量的验证码图片。在这里,使用captcha模块生成验证码图片,验证码图片名称为验证码上显示的字符串。1生成单张验证码图片并显示生成一张验证码的代码如下:fromcaptcha.imageimportImageCaptchaimportmatplotlib.pyplotaspltimportnumpyasnpimport……

    2022年9月20日
    0

发表回复

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

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