稳定高效的天气API接口是很少有的,本站前一段时间开发了一个万年历,需要带天气预报,花了几天功夫,集成和容错处理好对外开放的天气接口。天气接口支持返回   JSON  和  XML  结果。并且可以获取到昨天,以及今天和未来4 天的天气预报。那么下面直接介绍天气预报的接口吧。

天气API详情介绍 在 API 中心:http://www.sojson.com/api/weather.html ,下面只介绍下调用方式,然后 用  Java  语言调用演示一下。

天气预报 API 接口 JSON返回

public static void json() throws Exception{
    //参数url化
    String city = java.net.URLEncoder.encode("北京", "utf-8");
    
    //拼地址
    String apiUrl = String.format("http://www.sojson.com/open/api/weather/json.shtml?city=%s",city);
    //开始请求
    URL url= new URL(apiUrl);
    URLConnection open = url.openConnection();
    InputStream input = open.getInputStream();
    //这里转换为String,带上包名,怕你们引错包
    String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");
    //输出
    System.out.println(result);
}

天气预报 JSON 返回值:

    {
        "data": {
            "yesterday": {
                "date": "26日星期五",
                "high": "高温 31℃",
                "fx": "南风",
                "low": "低温 17℃",
                "fl": "微风",
                "type": "晴"
            },
            "city": "北京",
            "aqi": "83",
            "forecast": [
                {
                    "date": "27日星期六",
                    "high": "高温 34℃",
                    "fengli": "微风级",
                    "low": "低温 21℃",
                    "fengxiang": "西南风",
                    "type": "晴"
                },
                {
                    "date": "28日星期天",
                    "high": "高温 36℃",
                    "fengli": "3-4级",
                    "low": "低温 22℃",
                    "fengxiang": "北风",
                    "type": "多云"
                },
                {
                    "date": "29日星期一",
                    "high": "高温 29℃",
                    "fengli": "微风级",
                    "low": "低温 18℃",
                    "fengxiang": "东风",
                    "type": "阴"
                },
                {
                    "date": "30日星期二",
                    "high": "高温 30℃",
                    "fengli": "微风级",
                    "low": "低温 18℃",
                    "fengxiang": "南风",
                    "type": "阴"
                },
                {
                    "date": "31日星期三",
                    "high": "高温 32℃",
                    "fengli": "3-4级",
                    "low": "低温 20℃",
                    "fengxiang": "南风",
                    "type": "多云"
                }
            ],
            "ganmao": "各项气象条件适宜,发生感冒机率较低。但请避免长期处于空调房间中,以防感冒。",
            "wendu": "27"
        },
        "status": 200,
        "message": "OK"
    }