查询天气预报案例
第一步,找到天气服务
第二步,购买
第三步,按照对应的流程购买支付,然后可以在控制台中查看自己的appcode和秘钥
第四步,接口调用参考SDK,见文档示例
第五步,使用Hutool工具调用天气预报接口:
@Test public void testWeater() { String url = "https://aliv18.data.moji.com/whapi/json/alicityweather/forecast24hours"; String appcode = "d0bbfea3151e8bc1b";//修改成自己的appcode Map<String, Object> param = new HashMap<String, Object>(); param.put("cityId", "2"); HttpResponse response = HttpUtil.createRequest(Method.POST, url) .header("Authorization", "APPCODE " + appcode) .form(param) .execute(); if (response.getStatus() == 200) { System.out.println(response.body()); } }
响应的结果:
{ "code": 0, "data": { "city": { "cityId": 2, "counname": "中国", "ianatimezone": "Asia/Shanghai", "name": "北京市", "pname": "北京市", "secondaryname": "北京市", "timezone": "8" }, "hourly": [ { "condition": "晴", "conditionId": "5", "date": "2024-01-04", "hour": "20", "humidity": "20", "iconDay": "0", "iconNight": "30", "pop": "0", "pressure": "1014", "qpf": "0", "realFeel": "1", "snow": "0", "temp": "7", "updatetime": "2024-01-04 20:12:16", "uvi": "1", "windDegrees": "0", "windDir": "N", "windSpeed": "22.284", "windlevel": "4" }, { "condition": "晴", "conditionId": "5", "date": "2024-01-04", "hour": "21", "humidity": "39", "iconDay": "0", "iconNight": "30", "pop": "0", "pressure": "1022", "qpf": "0.0", "realFeel": "-1", "snow": "0", "temp": "5", "updatetime": "2024-01-04 20:12:16", "uvi": "1", "windDegrees": "315", "windDir": "WNW", "windSpeed": "24.08", "windlevel": "4" } ] }, "msg": "success", "rc": { "c": 0, "p": "success" } }