JAVA第三方api 天气预报(转载)

时间:2021-6-4 作者:qvyue

作者地址:https://www.jianshu.com/p/365dd499af03
首先
说一下我们用的api的url,可以直接在浏览器上输入查询,但是会出现乱码问题。
http://wthrcdn.etouch.cn/weather_mini?city=海淀

这里我们可以使用线上的测试工具。
https://www.sojson.com/convert/word2spell.html

JAVA第三方api 天气预报(转载)
image

注意,这里是get请求UTF-8,输入地址,发送请求就可以查看天气了。

JAVA第三方api 天气预报(转载)
image

java代码部分

第一步,加入依赖 (pom文件)

        
        org.apache.httpcomponentshttpclient4.5.5org.projectlomboklombok1.18.16org.apache.commonscommons-io1.3.2com.fasterxml.jackson.corejackson-databind2.8.7

需要http依赖

创建一个类,main方法,以下是代码部分就可以测试了。

       //1.构建客户端
        CloseableHttpClient aDefault = HttpClients.createDefault();
        //2.请求对象
        HttpGet dpGet=new HttpGet("http://wthrcdn.etouch.cn/weather_mini?city=海淀");
        //3.发送请求
        CloseableHttpResponse closeableHttpResponse = aDefault.execute(dpGet);
        //输出内容
        //获取对象
        String ent = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
        System.out.println(ent);

最后的成品代码,需要加入可选部分的依赖

这里添加的4个类将返回值以一种友好的方式呈现出来。

//Data类字段
Yesterday yesterday;
    String city;
    List forecast;
    String ganmao;
    String wendu;
//WeatherForecast类字段
    Data data;
    String status;
    String desc;
//WeatherInfo类字段
    String date;
    String high;
    String fengli;
    String low;
    String fengxiang;
    String type;
//Yesterday类字段
   String date;
    String high;
    String fx;
    String low;
    String fl;
    String type;

以下是代码部分就可以测试了

package com.enjoy.weather.ui;
import com.enjoy.weather.model.WeatherForecast;
import com.enjoy.weather.model.WeatherInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

/**
 * @ClassName WeatherApp
 * @Description TODO
 * @Author ly
 * @Date 2021/3/22 15:23
 * @Version 1.0
 */
public class WeatherApp {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        String city = scanner.next();
        //1.构建客户端
        CloseableHttpClient aDefault = HttpClients.createDefault();
        //2.请求对象
        HttpGet dpGet=new HttpGet("http://wthrcdn.etouch.cn/weather_mini?city="+city);
        //3.发送请求
        CloseableHttpResponse closeableHttpResponse = aDefault.execute(dpGet);
        //输出内容
        //获取对象
        String ent = EntityUtils.toString(closeableHttpResponse.getEntity(),"UTF-8");
        System.out.println(ent);
        //解析返回值
        ObjectMapper objectMapper = new ObjectMapper();
        WeatherForecast weatherForecast = objectMapper.readValue(ent, WeatherForecast.class);
        //解析对象的信息 获取城市信息
        if(weatherForecast.getStatus().equals("1002")){
            System.out.println("您输入的城市不存在");
            return;
        }
        String cityName = weatherForecast.getData().getCity();
        System.out.println("所在城市:"+cityName);
       //  今后5天的天气信息
        List forecast = weatherForecast.getData().getForecast();
        for (WeatherInfo weatherInfo:forecast){
            System.out.println("日期:"+weatherInfo.getDate());
            System.out.println("天气:"+weatherInfo.getType());
            System.out.println("最高温度:"+weatherInfo.getHigh());
            System.out.println("最低温度:"+weatherInfo.getLow());
            System.out.println("风力:"+weatherInfo.getFengli());
            System.out.println("风向:"+weatherInfo.getFengxiang());
            System.out.println("-----------------");
        }

    }
}

结果案例输出:

JAVA第三方api 天气预报(转载)
image.png
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:qvyue@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。