• java调用天气预报接口案例


    免费天气接口:http://mobile.weather.com.cn/data/sk/城市ID.html

    例如: http://mobile.weather.com.cn/data/sk/101240701.html

    返回数据:{"sk_info":{"date":"20131012","cityName":"赣州","areaID":"101240701","temp":"32℃","tempF":"89.6℉","wd":"东北风","ws":"3级","sd":"27%","time":"15:10","sm":"暂无实况"}}

    城市编码点我下载

    代码:

        @Test
        public void testetWeatherInfo(){
            //南昌天气预报信息
            String u="http://mobile.weather.com.cn/data/sk/101240101.html";
            String info=WeatherUtil.getWeatherInfo(u);
            //输出
            System.out.println("info:"+info);
        }
    /**
     * @author hh
     */
    public class WeatherUtil {
        /**
         * 获取天气信息
         * @param urlPath 请求链接  eg:http://mobile.weather.com.cn/data/sk/101240701.html
         * @return eg:{"sk_info":{"date":"20131012","cityName":"赣州","areaID":"101240701","temp":"32℃","tempF":"89.6℉","wd":"东北风","ws":"3级","sd":"27%","time":"15:10","sm":"暂无实况"}}
         */
        public static String getWeatherInfo(String urlPath){
            //拼接接收的信息
            StringBuffer info=new StringBuffer();
            //读取每行的数据
            String inputline="";
            try {
                //实例化URL对象
                URL url= new URL(urlPath);
                //获取应用程序和 URL 之间的通信链接
                HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                // 请求方法
                conn.setRequestMethod("GET");
                //获取url的资源输入流
                InputStreamReader inReader=new InputStreamReader(conn.getInputStream(),"utf-8");
                //获取缓冲字符输入流
                BufferedReader bufferedReader=new BufferedReader(inReader);
                //读取每行数据(同时赋值,判断是否为空)
                while((inputline=bufferedReader.readLine())!=null){
                    //添加信息
                    info.append(inputline);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return info.toString();
        }
    }

    返回数据:

     

  • 相关阅读:
    JNI内存使用问题(转载)
    typearray和obtainStyledAttribute的作用
    handler looper代码总结(原创)精品推荐
    Appium和Robotium在文字输入上的区别
    老李分享:robotium3.6与4.0 later 的区别 2
    老李分享:robotium3.6与4.0 later 的区别 1
    老李分享:robotium常用API 2
    老李分享:robotium常用API 1
    老李分享:Android -自动化埋点 3
    老李分享:Android -自动化埋点 2
  • 原文地址:https://www.cnblogs.com/hhmm99/p/9599417.html
Copyright © 2020-2023  润新知