利用OkHttp调用第三方天气接口
1.获取第三方接口api
private final String WEATHER_API_URL = "https://cn.apihz.cn/api/tianqi/tqybbaiducn.php?id="+id+"&key="+id+"&sheng=省份&place=城市";
2.创建OkHttpclient对象
private final OkHttpClient client = new OkHttpClient();
3.返回结果
public Map<String, String> getWeatherAnePm() throws Exception {Map<String, String> weatherAndPm = new HashMap<>();// 天气请求Request request = new Request.Builder().url(WEATHER_API_URL).build();try (Response response = client.newCall(request).execute()) {if (response.isSuccessful()) {ObjectMapper objectMapper = new ObjectMapper();JsonNode jsonNode = objectMapper.readTree(response.body().string());String weather = String.valueOf(jsonNode.get("result").get("now").get("temp"));weatherAndPm.put("weather", weather);} else {weatherAndPm.put("weather", "天气超时");}}