Java 天气接口 天气查询

第一步:配置yml

spring:
  #天气接口配置
  weather:
    appid: 82573591
    appsecret: oIVw8PWM
    #每个天气接口的version值都不同,如要更换接口,请同步更换version值

    #收费版 支持全球十万+ 城市天气
    #version: day
    #url: https://v0.tianqiapi.com

    #免费版 支持国内--七日天气--查询
    #version: v1
    #url: https://tianqiapi.com/api

    #免费版 支持国内天气查询
    version: v6
    url: https://tianqiapi.com/api

第二步:单独写一个WeatherConfig类

package com.techen.ami.config;

import com.alibaba.fastjson.JSONObject;
import com.techen.tap.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Configuration
public class WeatherConfig {


    @Value("${spring.weather.appid}")
    public String appid;

    @Value("${spring.weather.appsecret}")
    public String appsecret;

    @Value("${spring.weather.version}")
    public String version;

    @Value("${spring.weather.url}")
    public String url;

    @Autowired
    public RestTemplate restTemplate;

    //天气查询条件: 城市名称 坐标  如: query=北京  query=36.68,116.99
    String query = "北京";

    //设备ip  默认值47.95.242.134
    String ip = "47.95.242.134";

    //语言  zh  en    默认zh
    String language = "zh";

    //温度单位   m--摄氏度    f--华氏度   默认摄氏度
    String unit = "m";

    public String getUrl(HttpServletRequest request)
    {
        beforeSetting(request);
        //return url + "/?version=" + version + "&unit=" + unit + "&language=" + language + "&query=" + query + "&appid=" + appid + "&appsecret=" + appsecret;
        return url + "/?version=" + version  + "&city=" + query + "&appid=" + appid + "&appsecret=" + appsecret;
    }


    //按坐标获取天气url
    public String getUrlByCoordinate(Map paraMap , HttpServletRequest request)
    {
        String coordinate = "";
        if(paraMap.containsKey("longitude"))
        {
            String value = paraMap.get("longitude").toString();
            if(!StringUtil.isEmpty(value))
            {
                coordinate += value + ",";
            }
        }
        if(paraMap.containsKey("latitude"))
        {
            String value = paraMap.get("latitude").toString();
            if(!StringUtil.isEmpty(value))
            {
                coordinate += value ;
            }
        }
        if(!StringUtil.isEmpty(coordinate)){query = coordinate;}
        return getUrl(request);
    }

    //按城市获取天气url
    public String getUrlByCity(Map paraMap , HttpServletRequest request)
    {
        String city = "";
        if(paraMap.containsKey("city"))
        {
            String value = paraMap.get("city").toString();
            if(!StringUtil.isEmpty(value))
            {
                city += value ;
            }
        }
        if(!StringUtil.isEmpty(city)){query = city;}
        return getUrl(request);
    }

    //设置语言类型和温度单位
    public void beforeSetting(HttpServletRequest request)
    {
        String lang = request.getHeader("lang");
        if("en".equals(language))
        {
            language = lang;
            unit = "f";
        }
        else
        {
            language = lang;
            unit = "m";
        }
    }


    //由后台发送请求,接收返回的天气参数  依照城市
    public Map  getWeatherByCity(HttpServletRequest request , Map paraMap)
    {
        String url = this.getUrlByCity( paraMap , request);
        JSONObject jsonObj = restTemplate.getForObject(url , JSONObject.class);
        Map resultMap = (Map<String , Object>) jsonObj;
        return resultMap;
    }

    //由后台发送请求,接收返回的天气参数  依照经纬度
    public Map  getUrlByCoordinate(HttpServletRequest request , Map paraMap)
    {
        String url = this.getUrlByCoordinate( paraMap , request);
        JSONObject jsonObj = restTemplate.getForObject(url , JSONObject.class);
        Map resultMap = (Map<String , Object>) jsonObj;
        return resultMap;
    }
}

第三步 , 编写Controller.

    @Autowired
    protected WeatherConfig weatherConfig;


    @GetMapping(value = "/weather")
    public ResponseResult<Object> weather(HttpServletRequest request)
    {
        //paraMap由get请求传入, 参数名: city   参数值:城市名称(不带 省  市 后缀)如:上海
        Map paraMap = new HashMap();
        paraMap.put("city" , "shanghai");
        paraMap = weatherConfig.getWeatherByCity(request , paraMap);
        return ResponseResult.ok(paraMap);
    }

具体城市名称大全 请参考:

https://gitee.com/wangjins/weather_api/blob/master/city.sql#

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值