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

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

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


相关推荐

  • idea2021永久激活码【中文破解版】

    (idea2021永久激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月30日
    198
  • 二分类变量相关性分析spss_两个有序分类变量相关性的卡方检验-SPSS教程

    二分类变量相关性分析spss_两个有序分类变量相关性的卡方检验-SPSS教程一、问题与数据研究者想探索类风湿关节炎躯体感觉的症状数量与疼痛等级之间的关系,从一家大型医院入院治疗的类风湿关节炎病人中随机招募了364例研究对象。类风湿关节炎躯体感觉共有6种症状,研究者请研究对象报告其患有症状。类风湿关节炎门诊的医生使用疼痛量表对每个研究对象的疼痛进行评级。研究对象自报的类风湿关节炎躯体感觉症状数量在1-6个之间,为有序分类变量,变量名为symptoms。医生将研究对象的疼痛分…

    2022年5月17日
    131
  • 基于mysql的简单图书管理系统_图书管理系统

    基于mysql的简单图书管理系统_图书管理系统因为这两天要做数据库课设但是本人又很菜就做了一个简单的系统,简单的实现了增删改查,数据库用的是mysql,但是数据库的权限功能都没有实现,就是程序比较简陋,新手程序员可以参考一下~题目内容图书管理系统设计说明(1) 开发内容做出图书管理系统的需求分析,概念结构分析,逻辑结构分析,数据库的实施及维护。开发要求○1进行新书入库、现有图书信息修改以及删除;②能够实现对读者基本信息的查询和编辑管理;③能够实现预约功能;④能够实现借阅信息的查询功能;(2) 开发环境及工具系统前台开发软

    2022年10月15日
    2
  • 记录关于我与SCOM的事情

    记录关于我与SCOM的事情

    2022年3月13日
    38
  • navicat premium 15激活码 mac【2021免费激活】[通俗易懂]

    (navicat premium 15激活码 mac)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~747EFQ8BIF-eyJsaWNlb…

    2022年3月31日
    90
  • BigDecimal 与 int,long,double之间的互转[通俗易懂]

    BigDecimal 与 int,long,double之间的互转[通俗易懂]BigDecimal与int,long,double之间的互转转换关系如下:int转换成BigDecimal/***int转Bigdecimal*/@Testpublicvoiddemo04(){inta=101;BigDecimalbig=newBigDecimal(a);System.out.prin…

    2022年5月26日
    61

发表回复

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

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