前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >019android初级篇之获取天气信息

019android初级篇之获取天气信息

作者头像
上善若水.夏
发布2018-09-28 10:38:40
1.2K0
发布2018-09-28 10:38:40
举报
文章被收录于专栏:上善若水

本文从http://m.weather.com.cn/获取天气信息。

Android RGB颜色查询对照表

获取城市编号

一级城市列表:

  1. 数据源: http://m.weather.com.cn/data5/city.xm
  2. 得到的数据: 01|北京,02|上海,03|天津,04|重庆,05|黑龙江,06|吉林,07|辽宁,08|内蒙古,09|河北,10|山西,11|陕西,12|山东,13|新疆,14|西藏,15|青海,16|甘肃,17|宁夏,18|河南,19|江苏,20|湖北,21|浙江,22|安徽,23|福建,24|江西,25|湖南,26|贵州,27|四川,28|广东,29|云南,30|广西,31|海南,32|中国香港,33|中国澳门,34|中国台湾

二级城市列表

  1. 数据源,例如广东 http://m.weather.com.cn/data5/city28.xml
  2. 得到的数据 2801|广州,2802|韶关,2803|惠州,2804|梅州,2805|汕头,2806|深圳,2807|珠海,2808|佛山,2809|肇庆,2810|湛江,2811|江门,2812|河源,2813|清远,2814|云浮,2815|潮州,2816|东莞,2817|中山,2818|阳江,2819|揭阳,2820|茂名,2821|汕尾

三级城市列表,

  1. 数据源,例如深圳2806 http://m.weather.com.cn/data5/city2806.xml
  2. 得到的数据: 280601|深圳

由城市三级码得到城市编码

  1. 数据源,如河北.唐山.迁西(ps 哪里的板栗很出名哦) http://m.weather.com.cn/data5/city090507.xml
  2. 城市编码: 090507|101090507 则河北.唐山.迁西的城市编码为101090507

获取城市天气

  1. 今日及未来天气接口【内容最详细】

接口已经停用了,http://m.weather.com.cn/data/101090507.html,需要使用新接口。

  1. 查询今日天气: http://www.weather.com.cn/data/cityinfo/101090507.html

得到数据

代码语言:javascript
复制
{"weatherinfo":{"city":"迁西","cityid":"101090507","temp1":"16℃","temp2":"1℃","weather":"多云","img1":"d1.gif","img2":"n1.gif","ptime":"08:00"}}

相关代码

从Web获取json

代码语言:javascript
复制
String src ="http://www.weather.com.cn/data/cityinfo/101090507.html";

  String getJson(String src) {
        try {
            URL url = new URL(src);
            int lineIndex=0;
            HttpURLConnection httpConnect = (HttpURLConnection) url.openConnection();
            InputStreamReader inputStreamReader = new InputStreamReader(httpConnect.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String line ="";
            String jsonStr = "";
            while((line = bufferedReader.readLine())!=null){
                lineIndex++;
                jsonStr += line;
            }
            Log.e(TAG, jsonStr);
            return jsonStr;

        } catch (IOException e){
            e.printStackTrace();
        }
        return "";
    }

此段代码的返回值,即是json格式的天气信息。

解析json,获得想要信息

代码语言:javascript
复制
String  getWeatherInfo(String json){
    String weatherInfo ="";
    try{
        String filed ="";
        JSONObject obj = new JSONObject(json);
        filed = obj.getString("weatherinfo");
        JSONObject objsub=new JSONObject(filed);
        weatherInfo+="深圳 ,";
        weatherInfo += objsub.getString("temp1") + "--";
        weatherInfo += objsub.getString("temp2") + ", ";
        weatherInfo += objsub.get("weather");

    } catch (Exception e) {
        e.printStackTrace();

    }
    Log.e(TAG,weatherInfo);
    return weatherInfo;
}

参考链接

  1. Android访问中央气象台的天气预报API得到天气数据
  2. 深圳天气
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015.11.20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取城市编号
    • 一级城市列表:
      • 二级城市列表
        • 三级城市列表,
          • 由城市三级码得到城市编码
            • 获取城市天气
            • 相关代码
              • 从Web获取json
                • 解析json,获得想要信息
                • 参考链接
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档