精选案例网络编程Java web

java 免费第三方api,天气预报

2021-03-23  本文已影响0人  叶子槐

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

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

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

java代码部分

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

        <!--http必要-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.5</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <!--io可选-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!--jackson可选-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.7</version>
        </dependency>

需要http依赖

创建一个maven项目,建立类,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<WeatherInfo> 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<WeatherInfo> 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("-----------------");
        }

    }
}
输出效果部分
上一篇下一篇

猜你喜欢

热点阅读