使用和风天气接口获取天气信息

本文介绍了如何利用和风天气提供的API接口获取实时天气、未来7天天气和24小时天气预报。详细阐述了接口调用的URL、参数设置以及返回数据的解析,包括温度、风向、湿度等多个关键气象指标,为开发天气相关应用提供了实用指南。
摘要由CSDN通过智能技术生成

最近项目中使用到了获取天气相关信息,这里我使用了和风天气接口来实现此功能

和风天气接口API:API开发文档 | 和风天气开发平台

一:获取实时天气

1:接口地址

商业版:https://api.qweather.com/v7/weather/now?

开发版:https://devapi.qweather.com/v7/weather/now?

2:请求参数

key:用户认证key

location:需要查询地区的经纬度(经度,纬度)

3:返回数据

// 北京实况天气 
// 商业版 https://api.qweather.com/v7/weather/now?location=101010100&key=你的KEY
// 开发版 https://devapi.qweather.com/v7/weather/now?location=101010100&key=你的KEY


{
  "code": "200",
  "updateTime": "2020-06-30T22:00+08:00",
  "fxLink": "http://hfx.link/2ax1",
  "now": {
    "obsTime": "2020-06-30T21:40+08:00",
    "temp": "24",
    "feelsLike": "26",
    "icon": "101",
    "text": "多云",
    "wind360": "123",
    "windDir": "东南风",
    "windScale": "1",
    "windSpeed": "3",
    "humidity": "72",
    "precip": "0.0",
    "pressure": "1003",
    "vis": "16",
    "cloud": "10",
    "dew": "21"
  },
  "refer": {
    "sources": [
      "Weather China"
    ],
    "license": [
      "commercial license"
    ]
  }
}

参数

描述

code

API状态码,具体含义请参考状态码

updateTime

当前API的最近更新时间

fxLink

当前数据的响应式页面,便于嵌入网站或应用

now.obsTime

数据观测时间

now.temp

温度,默认单位:摄氏度

now.feelsLike

体感温度,默认单位:摄氏度

now.icon

天气状况和图标的代码,图标可通过天气状况和图标下载

now.text

天气状况的文字描述,包括阴晴雨雪等天气状态的描述

now.wind360

风向360角度

now.windDir

风向

now.windScale

风力等级

now.windSpeed

风速,公里/小时

now.humidity

相对湿度,百分比数值

now.precip

当前小时累计降水量,默认单位:毫米

now.pressure

大气压强,默认单位:百帕

now.vis

能见度,默认单位:公里

now.cloud

云量,百分比数值

now.dew

露点温度

refer.sources

原始数据来源,或数据源说明,可能为空

refer.license

数据许可或版权声明,可能为空



二:获取未来七天天气

1:接口地址

商业版:https://api.qweather.com/v7/weather/7d?

开发版:https://devapi.qweather.com/v7/weather/7d?

2:请求参数

key:用户认证key

location:需要查询地区的经纬度(经度,纬度)

3:返回数据



// 北京3天预报 
// 商业版 https://api.qweather.com/v7/weather/3d?location=101010100&key=你的KEY
// 开发版 https://devapi.qweather.com/v7/weather/3d?location=101010100&key=你的KEY

{
  "code": "200",
  "updateTime": "2021-11-15T16:35+08:00",
  "fxLink": "http://hfx.link/2ax1",
  "daily": [
    {
      "fxDate": "2021-11-15",
      "sunrise": "06:58",
      "sunset": "16:59",
      "moonrise": "15:16",
      "moonset": "03:40",
      "moonPhase": "盈凸月",
      "moonPhaseIcon": "803",
      "tempMax": "12",
      "tempMin": "-1",
      "iconDay": "101",
      "textDay": "多云",
      "iconNight": "150",
      "textNight": "晴",
      "wind360Day": "45",
      "windDirDay": "东北风",
      "windScaleDay": "1-2",
      "windSpeedDay": "3",
      "wind360Night": "0",
      "windDirNight": "北风",
      "windScaleNight": "1-2",
      "windSpeedNight": "3",
      "humidity": "65",
      "precip": "0.0",
      "pressure": "1020",
      "vis": "25",
      "cloud": "4",
      "uvIndex": "3"
    },
    {
      "fxDate": "2021-11-16",
      "sunrise": "07:00",
      "sunset": "16:58",
      "moonrise": "15:38",
      "moonset": "04:40",
      "moonPhase": "盈凸月",
      "moonPhaseIcon": "803",
      "tempMax": "13",
      "tempMin": "0",
      "iconDay": "100",
      "textDay": "晴",
      "iconNight": "101",
      "textNight": "多云",
      "wind360Day": "225",
      "windDirDay": "西南风",
      "windScaleDay": "1-2",
      "windSpeedDay": "3",
      "wind360Night": "225",
      "windDirNight": "西南风",
      "windScaleNight": "1-2",
      "windSpeedNight": "3",
      "humidity": "74",
      "precip": "0.0",
      "pressure": "1016",
      "vis": "25",
      "cloud": "1",
      "uvIndex": "3"
    },
    {
      "fxDate": "2021-11-17",
      "sunrise": "07:01",
      "sunset": "16:57",
      "moonrise": "16:01",
      "moonset": "05:41",
      "moonPhase": "盈凸月",
      "moonPhaseIcon": "803",
      "tempMax": "13",
      "tempMin": "0",
      "iconDay": "100",
      "textDay": "晴",
      "iconNight": "150",
      "textNight": "晴",
      "wind360Day": "225",
      "windDirDay": "西南风",
      "windScaleDay": "1-2",
      "windSpeedDay": "3",
      "wind360Night": "225",
      "windDirNight": "西南风",
      "windScaleNight": "1-2",
      "windSpeedNight": "3",
      "humidity": "56",
      "precip": "0.0",
      "pressure": "1009",
      "vis": "25",
      "cloud": "0",
      "uvIndex": "3"
    }
  ],
  "refer": {
    "sources": [
      "QWeather",
      "NMC",
      "ECMWF"
    ],
    "license": [
      "commercial license"
    ]
  }
}

参数

描述

code

API状态码,具体含义请参考状态码

updateTime

当前API的最近更新时间

fxLink

当前数据的响应式页面,便于嵌入网站或应用

daily.fxDate

预报日期

daily.sunrise

日出时间

daily.sunset

日落时间

daily.moonrise

月升时间

daily.moonset

月落时间

daily.moonPhase

月相名称

daily.moonPhaseIcon

月相图标代码,图标可通过天气状况和图标下载

daily.tempMax

预报当天最高温度

daily.tempMin

预报当天最低温度

daily.iconDay

预报白天天气状况的图标代码,图标可通过天气状况和图标下载

daily.textDay

预报白天天气状况文字描述,包括阴晴雨雪等天气状态的描述

daily.iconNight

预报夜间天气状况的图标代码,图标可通过天气状况和图标下载

daily.textNight

预报晚间天气状况文字描述,包括阴晴雨雪等天气状态的描述

daily.wind360Day

预报白天风向360角度

daily.windDirDay

预报白天风向

daily.windScaleDay

预报白天风力等级

daily.windSpeedDay

预报白天风速,公里/小时

daily.wind360Night

预报夜间风向360角度

daily.windDirNight

预报夜间当天风向

daily.windScaleNight

预报夜间风力等级

daily.windSpeedNight

预报夜间风速,公里/小时

daily.precip

预报当天总降水量,默认单位:毫米

daily.uvIndex

紫外线强度指数

daily.humidity

相对湿度,百分比数值

daily.pressure

大气压强,默认单位:百帕

daily.vis

能见度,默认单位:公里

daily.cloud

云量,百分比数值

refer.sources

原始数据来源,或数据源说明,可能为空

refer.license

数据许可或版权声明,可能为空



三:未来24小时天气

1:接口地址

商业版:https://api.qweather.com/v7/weather/24h?

开发版:https://devapi.qweather.com/v7/weather/24h?

2:请求参数

key:用户认证key

location:需要查询地区的经纬度(经度,纬度)

3:返回数据



// 北京未来24小时逐小时天气 
// 商业版 https://api.qweather.com/v7/weather/24h?location=101010100&key=你的KEY
// 开发版 https://devapi.qweather.com/v7/weather/24h?location=101010100&key=你的KEY

{
  "code": "200",
  "updateTime": "2021-02-16T13:35+08:00",
  "fxLink": "http://hfx.link/2ax1",
  "hourly": [
    {
      "fxTime": "2021-02-16T15:00+08:00",
      "temp": "2",
      "icon": "100",
      "text": "晴",
      "wind360": "335",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "20",
      "humidity": "11",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "0",
      "dew": "-25"
    },
    {
      "fxTime": "2021-02-16T16:00+08:00",
      "temp": "1",
      "icon": "100",
      "text": "晴",
      "wind360": "339",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "24",
      "humidity": "11",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "0",
      "dew": "-26"
    },
    {
      "fxTime": "2021-02-16T17:00+08:00",
      "temp": "0",
      "icon": "100",
      "text": "晴",
      "wind360": "341",
      "windDir": "西北风",
      "windScale": "4-5",
      "windSpeed": "25",
      "humidity": "11",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1026",
      "cloud": "0",
      "dew": "-26"
    },
    {
      "fxTime": "2021-02-16T18:00+08:00",
      "temp": "0",
      "icon": "150",
      "text": "晴",
      "wind360": "344",
      "windDir": "西北风",
      "windScale": "4-5",
      "windSpeed": "25",
      "humidity": "12",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-16T19:00+08:00",
      "temp": "-2",
      "icon": "150",
      "text": "晴",
      "wind360": "349",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "24",
      "humidity": "13",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-16T20:00+08:00",
      "temp": "-3",
      "icon": "150",
      "text": "晴",
      "wind360": "353",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "22",
      "humidity": "14",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-16T21:00+08:00",
      "temp": "-3",
      "icon": "150",
      "text": "晴",
      "wind360": "355",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "20",
      "humidity": "14",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1026",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-16T22:00+08:00",
      "temp": "-4",
      "icon": "150",
      "text": "晴",
      "wind360": "356",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "18",
      "humidity": "16",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1026",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-16T23:00+08:00",
      "temp": "-4",
      "icon": "150",
      "text": "晴",
      "wind360": "356",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "18",
      "humidity": "16",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1026",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-17T00:00+08:00",
      "temp": "-4",
      "icon": "150",
      "text": "晴",
      "wind360": "354",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "16",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1027",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-17T01:00+08:00",
      "temp": "-4",
      "icon": "150",
      "text": "晴",
      "wind360": "351",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "16",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1028",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-17T02:00+08:00",
      "temp": "-4",
      "icon": "150",
      "text": "晴",
      "wind360": "350",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "16",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1028",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-17T03:00+08:00",
      "temp": "-5",
      "icon": "150",
      "text": "晴",
      "wind360": "350",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "16",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1028",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-17T04:00+08:00",
      "temp": "-5",
      "icon": "150",
      "text": "晴",
      "wind360": "351",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "15",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1027",
      "cloud": "0",
      "dew": "-28"
    },
    {
      "fxTime": "2021-02-17T05:00+08:00",
      "temp": "-5",
      "icon": "150",
      "text": "晴",
      "wind360": "352",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "14",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1026",
      "cloud": "0",
      "dew": "-29"
    },
    {
      "fxTime": "2021-02-17T06:00+08:00",
      "temp": "-5",
      "icon": "150",
      "text": "晴",
      "wind360": "355",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "14",
      "humidity": "16",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "0",
      "dew": "-27"
    },
    {
      "fxTime": "2021-02-17T07:00+08:00",
      "temp": "-7",
      "icon": "150",
      "text": "晴",
      "wind360": "359",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "20",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1024",
      "cloud": "0",
      "dew": "-26"
    },
    {
      "fxTime": "2021-02-17T08:00+08:00",
      "temp": "-5",
      "icon": "100",
      "text": "晴",
      "wind360": "1",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "14",
      "humidity": "19",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1023",
      "cloud": "0",
      "dew": "-26"
    },
    {
      "fxTime": "2021-02-17T09:00+08:00",
      "temp": "-4",
      "icon": "100",
      "text": "晴",
      "wind360": "356",
      "windDir": "北风",
      "windScale": "3-4",
      "windSpeed": "14",
      "humidity": "17",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1023",
      "cloud": "0",
      "dew": "-25"
    },
    {
      "fxTime": "2021-02-17T10:00+08:00",
      "temp": "-1",
      "icon": "100",
      "text": "晴",
      "wind360": "344",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "14",
      "humidity": "14",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1024",
      "cloud": "0",
      "dew": "-26"
    },
    {
      "fxTime": "2021-02-17T11:00+08:00",
      "temp": "0",
      "icon": "100",
      "text": "晴",
      "wind360": "333",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "14",
      "humidity": "12",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1024",
      "cloud": "0",
      "dew": "-26"
    },
    {
      "fxTime": "2021-02-17T12:00+08:00",
      "temp": "1",
      "icon": "100",
      "text": "晴",
      "wind360": "325",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "14",
      "humidity": "10",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "16",
      "dew": "-28"
    },
    {
      "fxTime": "2021-02-17T13:00+08:00",
      "temp": "2",
      "icon": "100",
      "text": "晴",
      "wind360": "319",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "8",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "32",
      "dew": "-29"
    },
    {
      "fxTime": "2021-02-17T14:00+08:00",
      "temp": "2",
      "icon": "100",
      "text": "晴",
      "wind360": "313",
      "windDir": "西北风",
      "windScale": "3-4",
      "windSpeed": "16",
      "humidity": "9",
      "pop": "0",
      "precip": "0.0",
      "pressure": "1025",
      "cloud": "48",
      "dew": "-27"
    }
  ],
  "refer": {
    "sources": [
      "Weather China"
    ],
    "license": [
      "commercial license"
    ]
  }
}

参数

描述

code

API状态码,具体含义请参考状态码

updateTime

当前API的最近更新时间

fxLink

当前数据的响应式页面,便于嵌入网站或应用

hourly.fxTime

预报时间

hourly.temp

温度,默认单位:摄氏度

hourly.icon

天气状况和图标的代码,图标可通过天气状况和图标下载

hourly.text

天气状况的文字描述,包括阴晴雨雪等天气状态的描述

hourly.wind360

风向360角度

hourly.windDir

风向

hourly.windScale

风力等级

hourly.windSpeed

风速,公里/小时

hourly.humidity

相对湿度,百分比数值

hourly.precip

当前小时累计降水量,默认单位:毫米

hourly.pop

逐小时预报降水概率,百分比数值,可能为空

hourly.pressure

大气压强,默认单位:百帕

hourly.cloud

云量,百分比数值

hourly.dew

露点温度

refer.sources

原始数据来源,或数据源说明,可能为空

refer.license

数据许可或版权声明,可能为空



四:天气预警

1:接口地址

商业版:https://api.qweather.com/v7/warning/now?

开发版:https://devapi.qweather.com/v7/warning/now?

2:请求参数

key:用户认证key

location:需要查询地区的经纬度(经度,纬度)

3:返回数据



// 北京灾害预警 
// 商业版:https://api.qweather.com/v7/warning/now?location=101010100&key=你的KEY
// 开发版:https://devapi.qweather.com/v7/warning/now?location=101010100&key=你的KEY

{
  "code": "200",
  "updateTime": "2021-10-10T12:20+08:00",
  "fxLink": "http://hfx.link/2ax5",
  "warning": [
    {
      "id": "10101010020211009154607668935939",
      "sender": "北京市气象局",
      "pubTime": "2021-10-09T15:46+08:00",
      "title": "北京市气象台2021年10月09日15时40分发布大风蓝色预警信号",
      "startTime": "2021-10-09T15:40+08:00",
      "endTime": "2021-10-10T15:40+08:00",
      "status": "active",
      "level": "蓝色",
      "type": "11B06",
      "typeName": "大风",
      "text": "市气象台2021年10月9日15时40分发布大风蓝色预警信号:预计,9日22时至10日19时,本市大部分地区有4级左右偏北风,阵风6、7级,山区阵风可达8级左右,请注意防范。",
      "related": ""
    }
  ],
  "refer": {
    "sources": [
      "12379"
    ],
    "license": [
      "commercial license"
    ]
  }
}

参数

描述

code

API状态码,具体含义请参考状态码

updateTime

当前API的最近更新时间

fxLink

当前数据的响应式页面,便于嵌入网站或应用

warning.id

本条预警的唯一标识,可判断本条预警是否已经存在

warning.sender

预警发布单位,可能为空

warning.pubTime

预警发布时间

warning.title

预警信息标题

warning.startTime

预警开始时间,可能为空

warning.endTime

预警结束时间,可能为空

warning.status

预警状态,可能为空 active 预警中或首次预警 update 预警信息更新 cancel 取消预警

warning.level

预警等级

warning.type

预警类型ID

warning.typeName

预警类型名称

warning.text

预警详细文字描述

warning.related

与本条预警相关联的预警ID,当预警状态为cancel或update时返回。可能为空

refer.sources

原始数据来源,或数据源说明,可能为空

refer.license

数据许可或版权声明,可能为空

简单实例(以Yii框架为例):

$url = 'https://devapi.qweather.com/v7/weather/now?key=XXX&location=XXX,XXXX';
$client = new Client();
$response = $client->createRequest()
    ->setMethod('GET') // 请求方式
    ->setUrl($url)      // 请求地址
    ->send();
if ($response->isOk) {
    $data = json_decode(gzdecode($response->content));
}

  • 1
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Android和天气接口使用,你可以按照以下步骤进行: 1. 注册和天气开发者账号,并创建应用,获取应用的Key值。 2. 在Android Studio中创建一个新项目,然后在项目的build.gradle文件中添加以下依赖库: ```gradle implementation 'com.squareup.okhttp3:okhttp:3.14.9' implementation 'com.google.code.gson:gson:2.8.6' ``` 3. 在代码中发送HTTP请求,获取天气数据。以下是示例代码: ```java OkHttpClient client = new OkHttpClient(); String apiKey = "your_api_key"; String city = "beijing"; String url = "https://free-api.heweather.net/s6/weather/now?key=" + apiKey + "&location=" + city; Request request = new Request.Builder().url(url).build(); try { Response response = client.newCall(request).execute(); String responseData = response.body().string(); // 解析JSON数据 Gson gson = new Gson(); WeatherResponse weatherResponse = gson.fromJson(responseData, WeatherResponse.class); if (weatherResponse != null && "ok".equals(weatherResponse.getHeWeather6().get(0).getStatus())) { // 获取天气数据 String temperature = weatherResponse.getHeWeather6().get(0).getNow().getTmp(); String weatherInfo = weatherResponse.getHeWeather6().get(0).getNow().getCond_txt(); } else { // 请求失败 } } catch (IOException e) { e.printStackTrace(); } ``` 4. 解析JSON数据获取天气信息。以上面的代码为例,可以使用Gson库对JSON数据进行解析,获取温度和天气信息。 希望这些步骤能够对你有所帮助。不过需要注意的是,和天气接口使用需要符合相关的开发者协议和服务条款。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值