微信天气预报接口代码

合集下载

微信小程序天气预报开发实例代码源码

微信小程序天气预报开发实例代码源码

微信⼩程序天⽓预报开发实例代码源码微信⼩程序天⽓预报实例主要功能1. ⾃动定位所在城市2. 根据所定位的城市获取天⽓信息3. 显⽰未来⼏天的天⽓情况4. 查看当天天⽓的详情信息先看效果图微信⼩程序-天⽓⾸页微信⼩程序-天⽓详情页思路及编码部份⾃动定位所在城市wx.getLocation:通过官⽅⽂档的API中可以看到wx.getLocation可以获取到当前的地理位置和速度,不过获取到的地理位置只是经纬度,⽽不是真正的城市名称,但我们可以根据这个经纬度来获取城市名称等信息(需要⽤到第三⽅接⼝),再通过城市名称和城市ID获取对应的天⽓信息。

在.js逻辑层增加函数:data:{weatherApikey:'', //天⽓apikey,在 上申请city:'', //城市名称areaid:'', //城市对应的idcurWd:{}, //当天天⽓情况indexs:{}, //当天天⽓详情说明forecast:{} //未来4天的天⽓情况},onLoad:function(options){// ⽣命周期函数--监听页⾯加载this.setData({weatherApikey:getApp().globalData.weatherApikey});this.loadLocation();},//获取当前的位置信息,即经纬度loadLocation: function() {var page = this;wx.getLocation({type: 'gcj02', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可⽤于 wx.openLocation 的坐标success: function(res){// successvar latitude = titude;var longitude = res.longitude;//获取城市page.loadCity(latitude, longitude);}})},//通过经纬度获取城市loadCity: function(latitude, longitude) {var page = this;//这个key是⾃⼰在上申请的var key = "XSWBZ-EVQ3V-UMLPA-U4TP6-6MQFZ-UUFSL";var url = "/ws/geocoder/v1/?location="+latitude+","+longitude+"&key="+key+"&get_poi=1";wx.request({url: url,data: {},method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT// header: {}, // 设置请求的 headersuccess: function(res){// successvar city = res.data.result.address_component.city;city = city.replace("市", ""); //将“市”去掉,要不然取不了天⽓信息page.setData({city: city});page.loadId(city);}})},//通过城市名称获取城市的唯⼀IDloadId: function(city) {var page = this;var url = "/apistore/weatherservice/citylist";wx.request({url: url,data: {cityname: city},header: {apikey:page.data.weatherApikey},method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECTsuccess: function(res){// successvar cityid = res.data.retData[0].area_id;page.setData({areaid: cityid});page.loadWeather(city, cityid);}})},//通过城市名称和城市ID获取天⽓情况loadWeather: function(city, areaId) {var page = this;var url = "/apistore/weatherservice/recentweathers";wx.request({url: url,data: {cityname:city,cityid: areaId},header: {apikey: page.data.weatherApikey},method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECTsuccess: function(res){// successpage.setData({curWd : res.data.retData.today, indexs: res.data.retData.today.index, forecast:res.data.retData.forecast});}})},//事件绑定,跳转到天⽓详情页⾯gotoDetail: function(event) {// console.log(this.data.areaid+"==在这⾥跳转=="+this.data.city);wx.navigateTo({url: '../detail/detail?city='+this.data.city+"&cityid="+this.data.areaid})}注意:page.setData或this.setData都是⽤来设置data中的数据值的。

基于GO的天气预报接口调用代码实例

基于GO的天气预报接口调用代码实例

基于GO的天气预报接口调用代码实例代码描述:基于GO的天气预报接口调用代码实例代码平台:聚合数据package mainimport("io/ioutil""net/http""net/url""fmt""encoding/json")//----------------------------------// 全国天气预报调用示例代码-聚合数据// 在线接口文档:/docs/39//----------------------------------const APPKEY = "*******************"//您申请的APPKEY func main(){//1.根据城市名/id查询天气Request1()//2.天气种类及标识列表Request2()//3.根据IP查询天气Request3()//4.根据GPS坐标查询天气Request4()//5.城市天气三小时预报Request5()//6.支持城市列表Request6()}//1.根据城市名/id查询天气func Request1(){//请求地址juheURL :="/weather/index"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("cityname","") //城市名或城市ID,如:"苏州",需要u tf8 urlencodeparam.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("format","") //未来6天预报(future)两种返回格式,1或2,默认1 param.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//2.天气种类及标识列表func Request2(){//请求地址juheURL :="/weather/uni"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("key",APPKEY) //应用APPKEY(应用详细页查询)param.Set("dtype","") //返回数据的格式,xml或json,默认json//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//3.根据IP查询天气func Request3(){//请求地址juheURL :="/weather/ip"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("ip","") //ip地址,如:58.215.185.154param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("format","") //未来6天预报(future)两种返回格式,1或2,默认1 param.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//4.根据GPS坐标查询天气func Request4(){//请求地址juheURL :="/weather/geo"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("lon","") //经度,如:116.39277param.Set("lat","") //纬度,如:39.933748param.Set("format","") //未来6天预报(future)两种返回格式,1或2,默认1 param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//5.城市天气三小时预报func Request5(){//请求地址juheURL :="/weather/forecast3h"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("cityname","") //城市名,如:"苏州"param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//6.支持城市列表func Request6(){//请求地址juheURL :="/weather/citys"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}// get 网络请求func Get(apiURL string,params url.Values)(rs[]byte,err error){var Url *url.URLUrl,err=url.Parse(apiURL)if err!=nil{fmt.Printf("解析url错误:\r\n%v",err)return nil,err}//如果参数中有中文参数,这个方法会进行URLEncodeUrl.RawQuery=params.Encode()resp,err:=http.Get(Url.String())if err!=nil{fmt.Println("err:",err)return nil,err}defer resp.Body.Close()return ioutil.ReadAll(resp.Body)}// post 网络请求 ,params 是url.Values类型func Post(apiURL string, params url.Values)(rs[]byte,err error){ resp,err:=http.PostForm(apiURL, params)if err!=nil{return nil ,err}defer resp.Body.Close()return ioutil.ReadAll(resp.Body)}。

简易天气预报小程序(微信小程序使用JavaScript开发)

简易天气预报小程序(微信小程序使用JavaScript开发)

简易天气预报小程序(微信小程序使用JavaScript开发)介绍天气预报是人们日常生活中常用的信息之一,为了方便用户获取天气信息,本文将介绍如何通过微信小程序的开发,使用JavaScript语言实现简易天气预报小程序。

1. 小程序基本介绍微信小程序是一种轻量级的应用程序,用户可以在微信中直接使用,无需下载、安装即可使用。

开发者可以使用JavaScript语言进行开发,为用户提供各种实用的功能。

2. 开发环境准备首先,我们需要安装微信开发者工具,该工具提供了小程序的开发、调试和发布等功能。

同时,我们需要在微信公众平台中申请小程序的开发者账号,获取到AppID等信息,以便后续的开发工作。

3. 小程序页面结构小程序的页面由多个组件构成,每个组件负责展示不同的功能和信息。

在天气预报小程序中,我们可以设计以下页面结构:- 首页:显示当前城市的天气信息- 切换城市页:用于用户手动选择其他城市的天气预报- 天气详情页:展示当前城市的详细天气信息4. 获取天气数据为了获取天气数据,我们可以利用第三方的天气API接口。

在小程序中,可以使用wx.request()方法发送HTTP请求获取数据。

通过解析响应数据,我们可以获取到城市名、温度、湿度、风力等天气信息。

5. 实现逻辑- 打开小程序时,获取用户当前位置,并调用天气API获取数据,展示当前城市的天气信息。

- 用户可以在首页手动切换城市,跳转到切换城市页,选择其他城市获取天气信息。

- 在切换城市页,用户可以搜索城市名称或者选择热门城市,展示该城市的天气预报。

- 在天气详情页中,用户可以查看当前城市的详细天气信息,包括今天和未来几天的天气情况。

6. UI设计为了使小程序界面简洁美观,我们可以利用微信小程序提供的各种组件和样式进行页面设计。

可以设置背景图、字体样式、图标等元素,使得用户能够快速获取到所需信息。

7. 功能优化为了提高用户体验,可以添加以下功能:- 下拉刷新:用户可以下拉页面刷新获取最新的天气数据。

微信小程序实现天气预报功能

微信小程序实现天气预报功能

微信⼩程序实现天⽓预报功能本⽂实例为⼤家分享了微信⼩程序实现天⽓预报功能的具体代码,供⼤家参考,具体内容如下这个案例是仿UC中天⽓界⾯做的中间也有点出⼊,预留了显⽰当前城市名字和刷新图标的位置,⾃⼰可以写下,也可以添加搜索城市。

值得注意的是100%这个设置好像已经不好使了,可以通过获取设备的⾼度通过数据绑定设置⾼度。

地址:界⾯主要分为四部分:第⼀部分这⾥是预留的⼀块可以⾃⾏添加补充下<view class="newTopView"><!--左边添加当前城市名字,点击跳转选择城市右边添加刷新当前天⽓--> </view>第⼆部分:<view class="topView"><view class="degreeView"><!--当前温度--><text class="degree">{{currentTemperature}}</text><!--度数图标--><image class="circle" src="../../image/circle.png"></image></view><view class="detailInfo"><view class="degreeView"><!--夜间天⽓情况--><text class="detailInfoDegree">{{nightAirTemperature}}</text><image class="detailInfoCircle" src="../../image/circle.png" /><text class="detailInfoLine">/</text><!--⽩天天⽓--><text class="detailInfoDegree">{{dayAirTemperature}}</text><!-- style优先级⽐class⾼会覆盖class中相同属性 --><image class="detailInfoCircle" style="margin-left: 57rpx; margin-right: 40rpx" src="../../image/circle.png" /> <!-- 当前天⽓名字 --><text class="detailInfoName">{{weather}}</text></view></view></view>第三部分:<!-- 中间部分 --><view class="centerView"><view class="centerItem" style="margin-right: 25rpx;"><image class="centerItemImage" src="../../image/leaf.png" /><!-- 相同属性抽出来! --><!--污染指数--><text class="centerItemText" style="margin-left: 10rpx; margin-right: 10rpx">{{aqi}}</text><!--污染指数对应name--><text class="centerItemText">{{quality}}</text></view><view class="centerItem" style="margin-left: 25rpx"><image class="centerItemImage" src="../../image/wind.png" /><text class="centerItemText" style="margin-left: 10rpx; margin-right: 10rpx">{{windPower}}</text><!--风--><text class="centerItemText">{{windDirection}}</text></view></view>第四部分:<!-- 底部view --><view class="bottomView"><!--数据返回的不是数组在js中拼接的数组--><block wx:for-items="{{list}}"><view class="bottomItemView"><image class="bottomImage" src="{{item.day_weather_pic}}" style="margin-bottom: 15rpx;" /><text wx:if="{{item.weekday == 1}}" class="bottomText">星期⼀</text><text wx:elif="{{item.weekday == 2}}" class="bottomText">星期⼆</text><text wx:elif="{{item.weekday == 3}}" class="bottomText">星期三</text><text wx:elif="{{item.weekday == 4}}" class="bottomText">星期四</text><text wx:elif="{{item.weekday == 5}}" class="bottomText">星期五</text><text wx:elif="{{item.weekday == 6}}" class="bottomText">星期六</text><text wx:else="{{item.weekday == 7}}" class="bottomText">星期⽇</text><view class="degreeView" style="margin-top: 20rpx;"><text class="detailInfoDegree">{{item.night_air_temperature}}</text><image class="detailInfoCircle" src="../../image/circle.png" /><text class="detailInfoLine">/</text><text class="detailInfoDegree">{{item.day_air_temperature}}</text><!-- style优先级⽐class⾼会覆盖class中相同属性 --><image class="detailInfoCircle" style="margin-left: 57rpx; margin-right: 40rpx" src="../../image/circle.png" /> </view></view>js//index.js//获取应⽤实例var app = getApp()Page({data: {//加载状态loadingHidden: false,//当前温度currentTemperature: '',//夜间温度nightAirTemperature: '',//⽩天温度dayAirTemperature: '',//当前天⽓weather: '',//污染指数aqi: '',//污染程度quality: '',//风⼒windPower: '',//风向windDirection: '',//因为数据返回不是数组所以要⾃⼰封装⼀个数组list: [],height: 0,},onLoad: function () {console.log('onLoad')var that = this//100%好像不好使可以获取设备⾼度wx.getSystemInfo({success: function (res) {that.data.height = res.windowHeight;}})wx.getLocation({success: function (res) {//通过经纬度请求数据wx.request({//这个⽹站有免费API赶紧收藏url: '/9-5',data: {showapi_appid: '11697',showapi_sign: '6c0c15c5ec61454dac5288cea2d32881',//from: '5',lng: res.longitude,lat: titude,//获取⼀周情况 0是不获取needMoreDay: '1',needIndex: '1'},success: function (res) {console.log(res)console.log(res.data.showapi_res_body.now.api)that.setData({//改变加载状态loadingHidden: true,currentTemperature: res.data.showapi_res_body.now.temperature,nightAirTemperature: res.data.showapi_res_body.f1.night_air_temperature,dayAirTemperature: res.data.showapi_res_body.f1.day_air_temperature,weather: res.data.showapi_res_body.now.weather,aqi: res.data.showapi_res_body.now.aqi,quality: res.data.showapi_res_body.now.aqiDetail.quality,windPower: res.data.showapi_res_body.now.wind_power,windDirection: res.data.showapi_res_body.now.wind_direction,//拼接数组list: [{'day_weather_pic': res.data.showapi_res_body.f1.day_weather_pic,'weekday': res.data.showapi_res_body.f1.weekday,'day_air_temperature': res.data.showapi_res_body.f1.day_air_temperature, 'night_air_temperature': res.data.showapi_res_body.f1.night_air_temperature },{'day_weather_pic': res.data.showapi_res_body.f2.day_weather_pic,'weekday': res.data.showapi_res_body.f2.weekday,'day_air_temperature': res.data.showapi_res_body.f2.day_air_temperature, 'night_air_temperature': res.data.showapi_res_body.f2.night_air_temperature },{'day_weather_pic': res.data.showapi_res_body.f3.day_weather_pic,'weekday': res.data.showapi_res_body.f3.weekday,'day_air_temperature': res.data.showapi_res_body.f3.day_air_temperature, 'night_air_temperature': res.data.showapi_res_body.f3.night_air_temperature },{'day_weather_pic': res.data.showapi_res_body.f4.day_weather_pic,'weekday': res.data.showapi_res_body.f4.weekday,'day_air_temperature': res.data.showapi_res_body.f4.day_air_temperature, 'night_air_temperature': res.data.showapi_res_body.f4.night_air_temperature },{'day_weather_pic': res.data.showapi_res_body.f5.day_weather_pic,'weekday': res.data.showapi_res_body.f5.weekday,'day_air_temperature': res.data.showapi_res_body.f5.day_air_temperature, 'night_air_temperature': res.data.showapi_res_body.f5.night_air_temperature },{'day_weather_pic': res.data.showapi_res_body.f6.day_weather_pic,'weekday': res.data.showapi_res_body.f6.weekday,'day_air_temperature': res.data.showapi_res_body.f6.day_air_temperature, 'night_air_temperature': res.data.showapi_res_body.f6.night_air_temperature },{'day_weather_pic': res.data.showapi_res_body.f7.day_weather_pic,'weekday': res.data.showapi_res_body.f7.weekday,'day_air_temperature': res.data.showapi_res_body.f7.day_air_temperature, 'night_air_temperature': res.data.showapi_res_body.f7.night_air_temperature }]})}})}})}})wxss.container {display: flex;flex-direction: column;justify-content: space-between;}.newTopView {display: flex;flex-direction: row;justify-content: space-between;}/* 头部样式上半部分*/.topView {flex-direction: column;align-self: center;margin-top: -450rpx;}/*当前度数样式*/.degreeView {flex-direction: row;position: relative;}/*当前温度度数图标*/.circle {width: 35rpx;height: 35rpx;position: absolute;left: 130rpx;}/*当前度数数组*/.degree {color: white;font-size: 130rpx}/*详细View样式*/.detailInfoView {flex-direction: row;}/*当前最⾼和最低温度度数图标*/ .detailInfoCircle {width: 20rpx;height: 20rpx;position: absolute;left: 30rpx;}/*当前度数数组*/.detailInfoDegree {color: white;font-size: 30rpx}/*斜线*/.detailInfoLine {color: white;margin-left: 15rpx;font-size: 30rpx;}/*⽐如阴天晴天,暴⾬*/.detailInfoName {font-size: 30rpx;color: white;margin-left: 20rpx;align-self: center}/*中间view样式*/.centerView {display: flex;flex-direction: row;margin-top: -550rpx;justify-content: center;align-items: center;}/*中间view分为两个view*/.centerItem {display: flex;flex-direction: row;align-items: center;justify-content: center;}/*Item中图⽚样式*/.centerItemImage {width: 25rpx;height: 25rpx;}/*⽂字样式*/.centerItemText {font-size: 28rpx;color: white;}/*底部view样式*/.bottomView {margin-top: -200rpx;display: flex;flex-direction: row;justify-content: space-around;align-items: center;}.bottomItemView {display: flex;flex-direction: column;align-items: center;margin-bottom: 200rpx;}/*最近七天样式*/.bottomImage {width: 70rpx;height: 70rpx;}.bottomText {font-size: 28rpx;color: white;}PS:开发者⼯具⽆法显⽰问题:是因为View没有获得⾼度,默认个⾼度或者直接修改wxml中height⾼度即可。

微信小程序实现天气预报功能(附源码)

微信小程序实现天气预报功能(附源码)

微信⼩程序实现天⽓预报功能(附源码)⽬录前⾔效果图天⽓API获取微信⼩程序后台域名配置页⾯代码注意问题(必看)前⾔最近在学⼩程序开发,刚好学到天⽓预报功能的制作,于是给⼤家分享下。

效果图天⽓API获取这⾥我⽤的是,打开官⽹注册或者登陆你的账号进⼊控制台,新建应⽤这是刚刚我们创建好的应⽤,点击添加KEY选择WebAPI这注册好我们的API了微信⼩程序后台域名配置登陆⼩程序后台,分别点击开发和开发设置页⾯代码.wxml<view class="header"><view class="top"><view class="city">{{city}}</view><view class="search"><input placeholder="输⼊城市名" bindinput="bindKeyInput" placeholder-style="color:white"></input><view class="bt_search" bindtap="search"><icon type="search" size="18" color="white"></icon></view></view></view><view class="center"><view class="tmp">{{tmp}}°</view><image mode="widthFix" class="cond-image" src="https://moyv.top/wechat/images/weather/{{imgsrc}}.png"> </image></view><view class="bottom"><view>{{wind_dir}} {{wind_sc}}级</view><view>湿度 {{hum}}%</view><view>⽓压 {{pres}}Pa</view></view></view><view class="container"><view class="hourly_title">24⼩时预报</view><scroll-view scroll-x="true" class="hourly"><view class="h_item" wx:for="{{hourly}}" wx:key="index"><text class="h_time">{{item.time}}</text><view class="h_img"><image mode="widthFix" src="https://moyv.top/wechat/images/weather/{{item.imgsrc}}.png"></image> </view><text class="h_tmp">{{item.tmp}}°</text><text class="h_wind_dir">{{item.wind_dir}}</text><text class="h_wind_sc">{{item.wind_sc}}级</text></view></scroll-view><view class="hourly_title">7天预报</view><scroll-view scroll-x="true" class="daily"><view class="d_item" wx:for="{{daily_forecast}}" wx:key="index"><text class="d_txt">{{item.d_txt}}</text><text class="d_date">{{item.d_date}}</text><view class="h_img"><image mode="widthFix" src="https://moyv.top/wechat/images/weather/{{item.imgsrc_d}}.png"></image> </view><text class="h_tmp">{{item.tmp_min}}°~{{item.tmp_max}}°</text><view class="h_img"><image mode="widthFix" src="https://moyv.top/wechat/images/weather/{{item.imgsrc_n}}.png"></image> </view><text class="d_wind_dir">{{item.wind_dir}}</text><text class="d_wind_sc">{{item.wind_sc}}级</text></view></scroll-view></view><view class="footer">-天⽓数据来⾃和风天⽓api-</view>.wxsspage {background-color: #f6f6f6;}.header {background-color:#64c8fa;/* background-image: linear-gradient(to right, #64a0f8, #64c8fa); */height: 450rpx;padding-top: 32rpx;text-align: center;}.top {display: flex;justify-content: space-between;align-content: center;align-items: center;}.city {text-align: center;color: white;display: inline-block;font-size: 52rpx;margin-left: 32rpx;}.search {margin-right: 32rpx;border-radius: 8rpx;display: inline-flex;justify-content: center;align-content: center;align-items: center;background-color: rgba(0, 0, 0, 0.1);height: 70rpx;}.search input {width: 200rpx;padding: 18rpx 32rpx;text-align: left;color: white;display: inline-block;}.bt_search {border-radius: 0 8rpx 8rpx 0;height: 100%;background-color: rgba(0, 0, 0, 0.1);display: inline-flex;justify-content: center;align-content: center;align-items: center;}.bt_search icon {margin: 8rpx 18rpx;}.center {display: flex;justify-content: space-between;align-content: center;align-items: center;}.tmp {margin-left: 18rpx;display: inline-block;font-size: 180rpx;color: white;}.cond-image{width: 200rpx;margin-right: 32rpx;margin-top: 32rpx;}.bottom{display: flex;justify-content: space-between;align-content: center;align-items: center;}.bottom view{color: white;margin: 32rpx;}.hourly_title{font-weight: bold;font-size: 42rpx;padding: 18rpx 32rpx;}.hourly {width: 718rpx;margin: 0 18rpx;border-radius: 18rpx;box-shadow: 0.1rem 0.1rem 0.5rem rgba(0, 0, 0, 0.15);white-space: nowrap;background-color: white;}.h_item {margin: 18rpx 0;display: inline-block;width: 143.5rpx;text-align: center;font-size: 28rpx;}.h_img {margin: 64rpx 0;}.h_img image {width: 60rpx;}.h_item text {display: block;}.h_time {color: gray;}.h_wind_dir {margin-top: 32rpx;}.h_wind_sc {color: gray;}.h_tmp {color: #027aff;}.daily {width: 718rpx;white-space: nowrap;margin: 0 18rpx;background-color: white;border-radius: 18rpx;box-shadow: 0.1rem 0.1rem 0.5rem rgba(0, 0, 0, 0.15);}.d_item {margin: 18rpx 0;display: inline-block;width: 179.5rpx;text-align: center;font-size: 28rpx;}.d_item text {display: block;}.d_date {color: gray;}.d_wind_dir {margin-top: 32rpx;}.d_wind_sc {color: gray;}.footer{font-size: 28rpx;color: gray;text-align: center;margin-top: 50rpx;margin-bottom: 18rpx;}.jsPage({/*** 页⾯的初始数据*/data: {search_city: '',imgsrc:100},/*** 根据城市获取天⽓预报*/getWeather(city) {let that = this//获取实况天⽓wx.request({url: 'https:///s6/weather/now?key=你后台的key&location=' + city, success: function(res) {if (res.data.HeWeather6[0].status == 'unknown location') {wx.showToast({title: '抱歉!没有该城市的天⽓预报',icon: 'none',duration: 2000})return;}console.log(res)that.setData({city: city,tmp: res.data.HeWeather6[0].now.tmp,imgsrc: res.data.HeWeather6[0].now.cond_code,wind_dir: res.data.HeWeather6[0].now.wind_dir,wind_sc: res.data.HeWeather6[0].now.wind_sc,hum: res.data.HeWeather6[0].now.hum,pres: res.data.HeWeather6[0].now.pres})//获取24⼩时天⽓预报wx.request({url: 'https:///s6/weather/hourly?key=你后台的key&location=' + city,success: function(res) {var arr = res.data.HeWeather6[0].hourlyvar hourly = []for (var i = 0; i < arr.length; i++) {hourly[i] = {"imgsrc": arr[i].cond_code,"tmp": arr[i].tmp,"time": arr[i].time.substring(11),"wind_dir": arr[i].wind_dir,"wind_sc": arr[i].wind_sc}}that.setData({hourly: hourly})var weekArray = new Array("周⽇", "周⼀", "周⼆", "周三", "周四", "周五", "周六");//获取未来7天天⽓预报wx.request({url: 'https:///s6/weather/forecast?key=你后台的key&location=' + city,success: function(result) {//console.log(result)var arr = result.data.HeWeather6[0].daily_forecastvar daily_forecast = []for (var i = 0; i < arr.length; i++) {daily_forecast[i] = {d_txt: i == 0 ? "今天" : weekArray[new Date(arr[i].date).getDay()],d_date: arr[i].date.substring(5),imgsrc_d: arr[i].cond_code_d,imgsrc_n: arr[i].cond_code_n,wind_dir: arr[i].wind_dir,wind_sc: arr[i].wind_sc,tmp_max: arr[i].tmp_max,tmp_min: arr[i].tmp_min,cond_txt_d: arr[i].cond_txt_d}}that.setData({daily_forecast: daily_forecast})}})}})}})},bindKeyInput(e) {this.setData({search_city: e.detail.value})},search() {this.getWeather(this.data.search_city)},/*** ⽣命周期函数--监听页⾯加载*/onLoad: function(options) {this.getWeather("⼴州")},}).json{"usingComponents": {},"navigationBarTitleText": "天⽓预报"}注意问题(必看)由于我的项⽬有⽤到天⽓预报的逐⼩时预报和7天预报,和风天⽓⼜必须实名才能获取到此数据,所以请登录和风天⽓后台进⾏实名认证到此这篇关于微信⼩程序实现天⽓预报功能(附源码)的⽂章就介绍到这了,更多相关⼩程序实现天⽓预报内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。

动手开发一个名为“微天气”的微信小程序(上)

动手开发一个名为“微天气”的微信小程序(上)

动⼿开发⼀个名为“微天⽓”的微信⼩程序(上)引⾔:在智能⼿机软件的装机量中,天⽓预报类的APP排在⽐較靠前的位置。

说明⽤户对天⽓的关注度⾮常⾼。

由于⼈们不管是⼯作还是度假旅游等各种活动都须要依据⾃然天⽓来安排。

跟着本⽂开发⼀个“微天⽓”⼩程序,⽅便微信⽹友随时查看天⽓。

接下来两天⼩编将同您⼀起开发⼀个“微天⽓”⼩程序,本⽂⾸先向⼤家介绍“微天⽓”的API与界⾯代码编写。

本⽂选⾃《从零開始学微信⼩程序开发》。

在⼀套软件系统中,微信⼩程序⼀般是作为前端来使⽤的,⼀般还须要有后端的系统提供⽀持。

这就须要开发⼈员(或运营者)购买云server(或有⾃⼰的独⽴主机),将后端系统部署其上。

对于⾮常多刚開始学习的⼈来说。

这些条件不easy达到。

但我们能够选择使⽤在线免费API接⼝,开发⼈员仅仅须要编写好前端系统(微信⼩程序),在前端系统中直接调⽤这些免费API就可以获得对应的数据。

1 天⽓预报API 要开发天⽓预报类APP。

⾸先要考虑的问题就是天⽓预报数据的来源。

有了天⽓预报的数据来源。

才⼲按须要在微信⼩程序中显⽰出来。

事实上。

微信⼩程序就是⼀个显⽰天⽓信息的前端系统。

⽽天⽓预报API就是后端系统。

因为天⽓预报API能够在⽹上免费获取,因此。

本案例中开发⼈员不须要开发后端系统,仅仅须要依据API的要求进⾏訪问就可以。

中华万年历的天⽓预报接⼝{"desc": "OK","status": 1000,"data": {"wendu": "15","ganmao": "昼夜温差较⼤,较易发⽣感冒。

请适当增减⾐服。

体质较弱的朋友请注意防护。

","forecast": [{"fengxiang": "北风","fengli": "3-4级","high": "⾼温 14℃","type": "晴","low": "低温 3℃","date": "19⽇星期六"},{"fengxiang": "⽆持续风向","fengli": "微风级","high": "⾼温 4℃","type": "⾬夹雪","low": "低温 -1℃","date": "20⽇星期天"},{"fengxiang": "北风","fengli": "3-4级","high": "⾼温 0℃","type": "⼩雪","low": "低温 -7℃","date": "21⽇星期⼀"},{"fengxiang": "北风","fengli": "3-4级","high": "⾼温 -3℃","type": "晴","low": "低温 -9℃","date": "22⽇星期⼆"},{"fengxiang": "⽆持续风向","fengli": "微风级","high": "⾼温 -3℃","type": "多云","low": "低温 -10℃","date": "23⽇星期三"}],"yesterday": {"fl": "微风","fx": "⽆持续风向","high": "⾼温 10℃","type": "霾","low": "低温 6℃","date": "18⽇星期五"},"aqi": "40","city": "北京"}} 能够看出。

国家气象局免费天气预报接口,城市代码(JSON格式)

国家气象局免费天气预报接口,城市代码(JSON格式)

{"城市代码": [{"省": "北京","市": [{"市名": "北京","编码": "101010100"},{"市名": "朝阳","编码": "101010300"},{"市名": "顺义","编码": "101010400"},{"市名": "怀柔","编码": "101010500"},{"市名": "通州","编码": "101010600"},{"市名": "昌平","编码": "101010700"},{"市名": "延庆","编码": "101010800"},{"市名": "丰台","编码": "101010900"},{"市名": "石景山","编码": "101011000"},{"市名": "大兴","编码": "101011100"{"市名": "房山","编码": "101011200" },{"市名": "密云","编码": "101011300" },{"市名": "门头沟","编码": "101011400" },{"市名": "平谷","编码": "101011500" },{"市名": "八达岭","编码": "101011600" },{"市名": "佛爷顶","编码": "101011700" },{"市名": "汤河口","编码": "101011800" },{"市名": "密云上甸子","编码": "101011900" },{"市名": "斋堂","编码": "101012000" },{"市名": "霞云岭","编码": "101012100" },{"市名": "北京城区","编码": "101012200"{"市名": "海淀","编码": "101010200"}]},{"省": "天津市","市": [{"市名": "天津","编码": "101030100"},{"市名": "宝坻","编码": "101030300"},{"市名": "东丽","编码": "101030400"},{"市名": "西青","编码": "101030500"},{"市名": "北辰","编码": "101030600"},{"市名": "蓟县","编码": "101031400"},{"市名": "汉沽","编码": "101030800"},{"市名": "静海","编码": "101030900"},{"市名": "津南","编码": "101031000"},{"市名": "塘沽","编码": "101031100"},{"市名": "大港","编码": "101031200"},{"市名": "武清","编码": "101030200"},{"市名": "宁河","编码": "101030700"}]},{"省": "上海","市": [{"市名": "上海","编码": "101020100"},{"市名": "宝山","编码": "101020300"},{"市名": "嘉定","编码": "101020500"},{"市名": "南汇","编码": "101020600"},{"市名": "浦东","编码": "101021300"},{"市名": "青浦","编码": "101020800"},{"市名": "松江","编码": "101020900"},{"市名": "奉贤","编码": "101021000"},{"市名": "崇明","编码": "101021100"},{"市名": "徐家汇","编码": "101021200"},{"市名": "闵行","编码": "101020200"},{"市名": "金山","编码": "101020700"}]},{"省": "河北","市": [{"市名": "石家庄","编码": "101090101"},{"市名": "张家口","编码": "101090301"},{"市名": "承德","编码": "101090402"},"市名": "唐山","编码": "101090501"},{"市名": "秦皇岛","编码": "101091101"},{"市名": "沧州","编码": "101090701"},{"市名": "衡水","编码": "101090801"},{"市名": "邢台","编码": "101090901"},{"市名": "邯郸","编码": "101091001"},{"市名": "保定","编码": "101090201"},{"市名": "廊坊","编码": "101090601"}]},{"省": "河南","市": [{"市名": "郑州","编码": "101180101"},{"市名": "新乡","编码": "101180301"{"市名": "许昌","编码": "101180401" },{"市名": "平顶山","编码": "101180501" },{"市名": "信阳","编码": "101180601" },{"市名": "南阳","编码": "101180701" },{"市名": "开封","编码": "101180801" },{"市名": "洛阳","编码": "101180901" },{"市名": "商丘","编码": "101181001" },{"市名": "焦作","编码": "101181101" },{"市名": "鹤壁","编码": "101181201" },{"市名": "濮阳","编码": "101181301" },{"市名": "周口","编码": "101181401"{"市名": "漯河","编码": "101181501"},{"市名": "驻马店","编码": "101181601"},{"市名": "三门峡","编码": "101181701"},{"市名": "济源","编码": "101181801"},{"市名": "安阳","编码": "101180201"}]},{"省": "安徽","市": [{"市名": "合肥","编码": "101220101"},{"市名": "芜湖","编码": "101220301"},{"市名": "淮南","编码": "101220401"},{"市名": "马鞍山","编码": "101220501"},{"市名": "安庆",},{"市名": "宿州","编码": "101220701" },{"市名": "阜阳","编码": "101220801" },{"市名": "亳州","编码": "101220901" },{"市名": "黄山","编码": "101221001" },{"市名": "滁州","编码": "101221101" },{"市名": "淮北","编码": "101221201" },{"市名": "铜陵","编码": "101221301" },{"市名": "宣城","编码": "101221401" },{"市名": "六安","编码": "101221501" },{"市名": "巢湖","编码": "101221601" },{"市名": "池州",},{"市名": "蚌埠","编码": "101220201"}]},{"省": "浙江","市": [{"市名": "杭州","编码": "101210101"},{"市名": "舟山","编码": "101211101"},{"市名": "湖州","编码": "101210201"},{"市名": "嘉兴","编码": "101210301"},{"市名": "金华","编码": "101210901"},{"市名": "绍兴","编码": "101210501"},{"市名": "台州","编码": "101210601"},{"市名": "温州","编码": "101210701"},{"市名": "丽水","编码": "101210801"},{"市名": "衢州","编码": "101211001"},{"市名": "宁波","编码": "101210401"}]},{"省": "重庆","市": [{"市名": "重庆","编码": "101040100"},{"市名": "合川","编码": "101040300"},{"市名": "南川","编码": "101040400"},{"市名": "江津","编码": "101040500"},{"市名": "万盛","编码": "101040600"},{"市名": "渝北","编码": "101040700"},{"市名": "北碚","编码": "101040800"},"市名": "巴南","编码": "101040900" },{"市名": "长寿","编码": "101041000" },{"市名": "黔江","编码": "101041100" },{"市名": "万州天城","编码": "101041200" },{"市名": "万州龙宝","编码": "101041300" },{"市名": "涪陵","编码": "101041400" },{"市名": "开县","编码": "101041500" },{"市名": "城口","编码": "101041600" },{"市名": "云阳","编码": "101041700" },{"市名": "巫溪","编码": "101041800" },{"市名": "奉节","编码": "101041900" },"市名": "巫山","编码": "101042000" },{"市名": "潼南","编码": "101042100" },{"市名": "垫江","编码": "101042200" },{"市名": "梁平","编码": "101042300" },{"市名": "忠县","编码": "101042400" },{"市名": "石柱","编码": "101042500" },{"市名": "大足","编码": "101042600" },{"市名": "荣昌","编码": "101042700" },{"市名": "铜梁","编码": "101042800" },{"市名": "璧山","编码": "101042900" },{"市名": "丰都","编码": "101043000" },"市名": "武隆","编码": "101043100"},{"市名": "彭水","编码": "101043200"},{"市名": "綦江","编码": "101043300"},{"市名": "酉阳","编码": "101043400"},{"市名": "秀山","编码": "101043600"},{"市名": "沙坪坝","编码": "101043700"},{"市名": "永川","编码": "101040200"}]},{"省": "福建","市": [{"市名": "福州","编码": "101230101"},{"市名": "泉州","编码": "101230501"},{"市名": "漳州","编码": "101230601"{"市名": "龙岩","编码": "101230701"},{"市名": "晋江","编码": "101230509"},{"市名": "南平","编码": "101230901"},{"市名": "厦门","编码": "101230201"},{"市名": "宁德","编码": "101230301"},{"市名": "莆田","编码": "101230401"},{"市名": "三明","编码": "101230801"}]},{"省": "甘肃","市": [{"市名": "兰州","编码": "101160101"},{"市名": "平凉","编码": "101160301"},{"市名": "庆阳",},{"市名": "武威","编码": "101160501" },{"市名": "金昌","编码": "101160601" },{"市名": "嘉峪关","编码": "101161401" },{"市名": "酒泉","编码": "101160801" },{"市名": "天水","编码": "101160901" },{"市名": "武都","编码": "101161001" },{"市名": "临夏","编码": "101161101" },{"市名": "合作","编码": "101161201" },{"市名": "白银","编码": "101161301" },{"市名": "定西","编码": "101160201" },{"市名": "张掖",}]},{"省": "广东","市": [{"市名": "广州","编码": "101280101"},{"市名": "惠州","编码": "101280301"},{"市名": "梅州","编码": "101280401"},{"市名": "汕头","编码": "101280501"},{"市名": "深圳","编码": "101280601"},{"市名": "珠海","编码": "101280701"},{"市名": "佛山","编码": "101280800"},{"市名": "肇庆","编码": "101280901"},{"市名": "湛江","编码": "101281001"},{"编码": "101281101" },{"市名": "河源","编码": "101281201" },{"市名": "清远","编码": "101281301" },{"市名": "云浮","编码": "101281401" },{"市名": "潮州","编码": "101281501" },{"市名": "东莞","编码": "101281601" },{"市名": "中山","编码": "101281701" },{"市名": "阳江","编码": "101281801" },{"市名": "揭阳","编码": "101281901" },{"市名": "茂名","编码": "101282001" },{"市名": "汕尾","编码": "101282101" },{"编码": "101280201"}]},{"省": "广西","市": [{"市名": "南宁","编码": "101300101"},{"市名": "柳州","编码": "101300301"},{"市名": "来宾","编码": "101300401"},{"市名": "桂林","编码": "101300501"},{"市名": "梧州","编码": "101300601"},{"市名": "防城港","编码": "101301401"},{"市名": "贵港","编码": "101300801"},{"市名": "玉林","编码": "101300901"},{"市名": "百色","编码": "101301001"},{"市名": "钦州","编码": "101301101"},{"市名": "河池","编码": "101301201"},{"市名": "北海","编码": "101301301"},{"市名": "崇左","编码": "101300201"},{"市名": "贺州","编码": "101300701"}]},{"省": "贵州","市": [{"市名": "贵阳","编码": "101260101"},{"市名": "安顺","编码": "101260301"},{"市名": "都匀","编码": "101260401"},{"市名": "兴义","编码": "101260906"},{"市名": "铜仁","编码": "101260601"},{"市名": "毕节","编码": "101260701"},{"市名": "六盘水","编码": "101260801"},{"市名": "遵义","编码": "101260201"},{"市名": "凯里","编码": "101260501"}]},{"省": "云南","市": [{"市名": "昆明","编码": "101290101"},{"市名": "红河","编码": "101290301"},{"市名": "文山","编码": "101290601"},{"市名": "玉溪","编码": "101290701"},{"市名": "楚雄","编码": "101290801"},{"市名": "普洱","编码": "101290901"},{"市名": "昭通","编码": "101291001"},{"市名": "临沧","编码": "101291101"},{"市名": "怒江","编码": "101291201"},{"市名": "香格里拉","编码": "101291301"},{"市名": "丽江","编码": "101291401"},{"市名": "德宏","编码": "101291501"},{"市名": "景洪","编码": "101291601"},{"市名": "大理","编码": "101290201"},{"市名": "曲靖","编码": "101290401"},{"市名": "保山","编码": "101290501"}]},{"省": "内蒙古","市": [{"市名": "呼和浩特","编码": "101080101"},{"市名": "乌海","编码": "101080301"},{"市名": "集宁","编码": "101080401"},{"市名": "通辽","编码": "101080501"},{"市名": "阿拉善左旗","编码": "101081201"},{"市名": "鄂尔多斯","编码": "101080701"},{"市名": "临河","编码": "101080801"},{"市名": "锡林浩特","编码": "101080901"},{"市名": "呼伦贝尔","编码": "101081000"},{"市名": "乌兰浩特","编码": "101081101"},{"市名": "包头","编码": "101080201"},{"市名": "赤峰","编码": "101080601"}]},{"省": "江西","市": [{"市名": "南昌","编码": "101240101"},{"市名": "上饶","编码": "101240301"},{"市名": "抚州","编码": "101240401"},{"市名": "宜春","编码": "101240501"},{"市名": "鹰潭","编码": "101241101"},{"市名": "赣州","编码": "101240701"},{"市名": "景德镇","编码": "101240801"},{"市名": "萍乡","编码": "101240901"},"市名": "新余","编码": "101241001"},{"市名": "九江","编码": "101240201"},{"市名": "吉安","编码": "101240601"}]},{"省": "湖北","市": [{"市名": "武汉","编码": "101200101"},{"市名": "黄冈","编码": "101200501"},{"市名": "荆州","编码": "101200801"},{"市名": "宜昌","编码": "101200901"},{"市名": "恩施","编码": "101201001"},{"市名": "十堰","编码": "101201101"},{"市名": "神农架","编码": "101201201"{"市名": "随州","编码": "101201301"},{"市名": "荆门","编码": "101201401"},{"市名": "天门","编码": "101201501"},{"市名": "仙桃","编码": "101201601"},{"市名": "潜江","编码": "101201701"},{"市名": "襄樊","编码": "101200201"},{"市名": "鄂州","编码": "101200301"},{"市名": "孝感","编码": "101200401"},{"市名": "黄石","编码": "101200601"},{"市名": "咸宁","编码": "101200701"}]},{"省": "四川","市": [{"市名": "成都","编码": "101270101"},{"市名": "自贡","编码": "101270301"},{"市名": "绵阳","编码": "101270401"},{"市名": "南充","编码": "101270501"},{"市名": "达州","编码": "101270601"},{"市名": "遂宁","编码": "101270701"},{"市名": "广安","编码": "101270801"},{"市名": "巴中","编码": "101270901"},{"市名": "泸州","编码": "101271001"},{"市名": "宜宾","编码": "101271101"},{"市名": "内江","编码": "101271201"},{"市名": "资阳","编码": "101271301"},{"市名": "乐山","编码": "101271401"},{"市名": "眉山","编码": "101271501"},{"市名": "凉山","编码": "101271601"},{"市名": "雅安","编码": "101271701"},{"市名": "甘孜","编码": "101271801"},{"市名": "阿坝","编码": "101271901"},{"市名": "德阳","编码": "101272001"},{"市名": "广元","编码": "101272101"},{"市名": "攀枝花","编码": "101270201"}]},{"省": "宁夏","市": [{"市名": "银川","编码": "101170101"},{"市名": "中卫","编码": "101170501"},{"市名": "固原","编码": "101170401"},{"市名": "石嘴山","编码": "101170201"},{"市名": "吴忠","编码": "101170301"}]},{"省": "青海省","市": [{"市名": "西宁","编码": "101150101"},{"市名": "黄南","编码": "101150301"},{"市名": "海北","编码": "101150801"},{"市名": "果洛","编码": "101150501"},{"市名": "玉树","编码": "101150601"},{"市名": "海西","编码": "101150701"},{"市名": "海东","编码": "101150201"},{"市名": "海南","编码": "101150401"}]},{"省": "山东","市": [{"市名": "济南","编码": "101120101"},{"市名": "潍坊","编码": "101120601"},{"市名": "临沂","编码": "101120901"},{"市名": "菏泽","编码": "101121001"},{"市名": "滨州","编码": "101121101"},{"市名": "东营","编码": "101121201"{"市名": "威海","编码": "101121301" },{"市名": "枣庄","编码": "101121401" },{"市名": "日照","编码": "101121501" },{"市名": "莱芜","编码": "101121601" },{"市名": "聊城","编码": "101121701" },{"市名": "青岛","编码": "101120201" },{"市名": "淄博","编码": "101120301" },{"市名": "德州","编码": "101120401" },{"市名": "烟台","编码": "101120501" },{"市名": "济宁","编码": "101120701" },{"市名": "泰安","编码": "101120801"。

基于C#的天气预报接口调用代码实例

基于C#的天气预报接口调用代码实例

基于C#的天气预报接口调用代码实例代码描述:基于C#的天气预报接口调用代码实例代码平台:聚合数据usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;;usingSystem.IO;;usingSystem.Diagnostics;usingSystem.Web;//----------------------------------// 天气预报调用示例代码-聚合数据// 在线接口文档:/docs/73// 代码中JsonObject类下载地址:/download/gcm32060211556 65/7458439//----------------------------------namespaceConsoleAPI{classProgram{staticvoidMain(string[] args){stringappkey = "*******************"; //配置您申请的ap pkey//1.根据城市查询天气stringurl1 = "/onebox/weather/query";var parameters1 = newDictionary<string, string>();parameters1.Add("cityname", ""); //要查询的城市,如:温州、上海、北京parameters1.Add("key", appkey);//你申请的keyparameters1.Add("dtype", ""); //返回数据的格式,xml或js on,默认jsonstringresult1 = sendPost(url1, parameters1, "get");JsonObject newObj1 = newJsonObject(result1);String errorCode1 = newObj1["error_code"].Value;if(errorCode1 == "0"){Debug.WriteLine("成功");Debug.WriteLine(newObj1);}else{//Debug.WriteLine("失败");Debug.WriteLine(newObj1["error_code"].Value+":" +newObj1["reason"].Value);}}/// <summary>/// Http (GET/POST)/// </summary>/// <param name="url">请求URL</param>/// <param name="parameters">请求参数</param>/// <param name="method">请求方法</param>/// <returns>响应内容</returns>staticstringsendPost(stringurl, IDictionary<string, string> par ameters, stringmethod){if(method.ToLower() == "post"){HttpWebRequest req = null;HttpWebResponse rsp = null;System.IO.Stream reqStream = null;try{req = (HttpWebRequest)WebRequest.Create (url);req.Method = method;req.KeepAlive = false;req.ProtocolVersion = HttpVersion.Versi on10;req.Timeout = 5000;req.ContentType = "application/x-www-fo rm-urlencoded;charset=utf-8";byte[] postData = Encoding.UTF8.GetByte s(BuildQuery(parameters, "utf8"));reqStream = req.GetRequestStream();reqStream.Write(postData, 0, postData.L ength);rsp = (HttpWebResponse)req.GetResponse ();Encoding encoding = Encoding.GetEncodin g(rsp.CharacterSet);returnGetResponseAsString(rsp, encodin g);}catch(Exception ex){returnex.Message;}finally{if(reqStream != null) reqStream.Close ();if(rsp != null) rsp.Close();}}else{//创建请求HttpWebRequest request = (HttpWebRequest)WebReq uest.Create(url + "?"+ BuildQuery(parameters, "utf8"));//GET请求request.Method = "GET";request.ReadWriteTimeout = 5000;request.ContentType = "text/html;charset=UTF-8 ";HttpWebResponse response = (HttpWebResponse)req uest.GetResponse();Stream myResponseStream = response.GetResponseS tream();StreamReader myStreamReader = newStreamReader(m yResponseStream, Encoding.GetEncoding("utf-8"));//返回内容stringretString = myStreamReader.ReadToEnd();returnretString;}}/// <summary>/// 组装普通文本请求参数。

简单易懂的天气插件(代码分享)

简单易懂的天气插件(代码分享)

简单易懂的天⽓插件(代码分享)效果图如下:代码如下所⽰:<!DOCTYPE html><html><head><meta charset="UTF-8"><title>天⽓</title><meta name="viewport" content="width=device-width,initial-scale=1"/><style type="text/css">.xc{width: 200px;margin: 0 auto;}</style></head><body><div id="tp-weather-widget" class="xc"></div><script>(function(T, h, i, n, k, P, a, g, e) {g = function() {P = h.createElement(i);a = h.getElementsByTagName(i)[0];P.src = k;P.charset = "utf-8";P.async = 1;a.parentNode.insertBefore(P, a)};T["ThinkPageWeatherWidgetObject"] = n;T[n] || (T[n] = function() {(T[n].q = T[n].q || []).push(arguments)});T[n].l = +new Date();if(T.attachEvent) {T.attachEvent("onload", g)} else {T.addEventListener("load", g, false)}}(window, document, "script", "tpwidget", "///widget/chameleon.js"))</script><script>tpwidget("init", {"flavor": "slim","location": "WX4FBXXFKE4F","geolocation": "disabled","language": "zh-chs","unit": "c","theme": "chameleon","container": "tp-weather-widget","bubble": "enabled","alarmType": "badge","color": "#F47837","uid": "U605DCADA4","hash": "78f46a1198d54dafa0cda717efa717a9"});tpwidget("show");</script></body></html>以上就是本⽂的全部内容,希望本⽂的内容对⼤家的学习或者⼯作能带来⼀定的帮助,同时也希望多多⽀持!。

全国天气预报接口

全国天气预报接口

全国天气预报接口查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报接口可以根据根据城市名/id查询天气、根据IP查询天气、据GPS坐标查询天气、查询城市天气三小时预报,并且支持全国不同城市天气预报查询。

代码描述:基于JAVA的全国天气预报接口调用示例,根据文档中注明的需求参数,调用接口返回数据。

关联数据:全国天气预报接口接口地址:https:///docs/api/id/39step1:选择本文所示例的接口"全国天气预报接口" url:/docs/api/id/39/aid/87step2:每个接口都需要传入一个参数key,相当于用户的令牌,所以第一步你需要申请一个keystep3:看文档!!!学过java的同学们都知道,当我们对一个类或者方法不明白其意图和思想时,我们可以去查看文档,这里也不例外,而且对于英文不是特别好的同学来说很幸运的是,聚合网站上的文档都是中文版本的,比起阅读java源码里的英文文档应该轻松很多.全国天气预报接口下面有六个子接口,打开第一个接口链接,看文档发现需要传入一个城市名或者城市ID参数,这个参数我们可以通过第六个子接口获取(接口之间参数的调用类似于java中方法之间的调用),即支持城市列表获取.所以示例中我们先调用这个接口.调用接口涉及到请求网络资源的问题,这里我封装了一个工具类,包含GET和POST两种方法step4:上代码Demo1:网络访问工具类(封装get和post方法)1.package juheAPI;2.3.import java.io.BufferedReader;4.import java.io.BufferedWriter;5.import java.io.InputStream;6.import java.io.InputStreamReader;7.import java.io.OutputStream;8.import java.io.OutputStreamWriter;9.import .HttpURLConnection;10.import .URL;11.import java.util.Map;12.13.public class PureNetUtil {14.15.public static String get(String url){16.return post(url,null);17.}18.19.public static String post(String url,Map param){20.HttpURLConnection conn=null;21.try{22.URL u=new URL(url);23.conn=(HttpURLConnection) u.openConnection();24.StringBuffer sb=null;25.if(param!=null){//如果请求参数不为空26.sb=new StringBuffer();27.28.//默认为false,post方法需要写入参数,设定true29.conn.setDoOutput(true);30.//设定post方法,默认get31.conn.setRequestMethod("POST");32.//获得输出流33.OutputStream out=conn.getOutputStream();34.//对输出流封装成高级输出流35.BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));36.//将参数封装成键值对的形式37.for(Map.Entry s:param.entrySet()){38.sb.append(s.getKey()).append("=").append(s.getValue()).append("&");39.}40.//将参数通过输出流写入41.writer.write(sb.deleteCharAt(sb.toString().length()-1).toString());42.writer.close();//一定要关闭,不然可能出现参数不全的错误43.sb=null;44.}45.conn.connect();//建立连接46.sb=new StringBuffer();47.//获取连接状态码48.int recode=conn.getResponseCode();49.BufferedReader reader=null;50.if(recode==200){51.//Returns an input stream that reads from this open connection52.//从连接中获取输入流53.InputStream in=conn.getInputStream();54.//对输入流进行封装55.reader=new BufferedReader(new InputStreamReader(in));56.String str=null;57.sb=new StringBuffer();58.//从输入流中读取数据59.while((str=reader.readLine())!=null){60.sb.append(str).append(System.getProperty("line.separator"));61.}62.//关闭输入流63.reader.close();64.if(sb.toString().length() == 0) {65.return null;66.}67.return sb.toString().substring(0,68.sb.toString().length() - System.getProperty("line.separator").length());69.}70.} catch(Exception e) {71. e.printStackTrace();72.return null;73.}finally{74.if(conn!=null)//关闭连接75.conn.disconnect();76.}77.return null;78.}79.80.}Demo2:调用获取城市列表接口示例1.package juheAPI;2.3.import net.sf.json.JSONArray;4.import net.sf.json.JSONObject;5.6.7.8.public class GetCityList {9.10.public static String excute(){11.String url="/weather/citys?key=***a7558b2e0bedaa19673f74a6809ce";//接口URL12.//PureNetUtil是一个封装了get和post方法获取网络请求数据的工具类13.return PureNetUtil.get(url);//使用get方法14.}15.16.public static String getIDBycityName(String cityName) {17.String result=excute();//返回接口结果,得到json格式数据18.if(result!=null){19.JSONObject obj=JSONObject.fromObject(result);20.result=obj.getString("resultcode");//得到返回状态码21.if(result!=null&&result.equals("200")){//200表示成功返回数据22.result=obj.getString("result");//得到城市列表的json格式字符串数组23.JSONArray arr=JSONArray.fromObject(result);24.for(Object o:arr){//对arr进行遍历25.//将数组中的一个json个数字符串进行解析26.obj=JSONObject.fromObject(o.toString());27.28.//以city这个key为线索判断所需要寻找的这条记录29.result=obj.getString("district");30.//防止输入城市名不全,如苏州市输入为苏州,类似与模糊查询31.if(result.equals(cityName)||result.contains(cityName)){32.result=obj.getString("id");//得到ID33.return result;34.}35.}36.}37.}38.return result;39.}40.public static void main(String[] args) {41.System.out.println(getIDBycityName("香港"));42.}43.}Demo3:调用根据城市名/id查询天气1.package juheAPI;2.3.import net.sf.json.JSONObject;4.5.6.7.public class WeatherReportByCity {8.9.public static String excute(String cityName){10.String url=//此处以返回json格式数据示例,所以format=2,以根据城市名称为例,cityName传入中文11."/weather/index?cityname="+cityName+"&key=***a7558b2e0bedaa19673f74a6809ce";12.return PureNetUtil.get(url);//通过工具类获取返回数据13.}14.15.public static String GetTodayTemperatureByCity(String city) {16.String result=excute(city);17.if(result!=null){18.JSONObject obj=JSONObject.fromObject(result);19.20.result=obj.getString("resultcode");21.22.if(result!=null&&result.equals("200")){23.result=obj.getString("result");24.//此时result中数据有多个key,可以对其key进行遍历,得到对个属性25.obj=JSONObject.fromObject(result);26.//今日温度对应的key是today27.result=obj.getString("today");28.obj=JSONObject.fromObject(result);29.//今日温度对应当key是temperature30.result=obj.getString("temperature");31.return result;32.}33.}34.return result;35.}36.public static void main(String[] args) {37.System.out.println(GetTodayTemperatureByCity("苏州"));38.}39.}Demo4:调用天气种类及表示列表接口示例1.package juheAPI;2.3.import net.sf.json.JSONArray;4.import net.sf.json.JSONObject;5.6.7.8.public class GetWeatherSignAndTypeList {9.//接口地址,因为只需要传入一个固定的key为参数,所以设为常量10.private static final String URL= "/weather/uni?key=***a7558b2e0bedaa19673f74a6809ce";11.12.public static String excute(){13.return PureNetUtil.get(URL);//调用工具类获取接口数据14.}15.16.public static String getWeatherByWid(String wid) {17.String result=excute();//获取接口数据18.if(result!=null){19.JSONObject obj=JSONObject.fromObject(result);20.result=obj.getString("resultcode");21.22.if(result!=null&&result.equals("200")){23.24.result=obj.getString("result");25.JSONArray arr=JSONArray.fromObject(result);26.for(Object o:arr){//遍历数组27.obj=JSONObject.fromObject(o.toString());28.//如果遍历到需要的数据后直接返回结果,根据key(wid)得到value判断是否等于传入参数29.if(obj.getString("wid").equals(wid)){30.result=obj.getString("weather");31.return result;32.}33.}34.}35.}36.return result;37.}38.public static void main(String[] args) {39.System.out.println(getWeatherByWid("10"));40.}41.}step5:调用接口时候如果状态码不是200,仔细参考文档说明,也就是返回step3:看文档!。

天气预报使用Python语言和Django框架开发的小程序

天气预报使用Python语言和Django框架开发的小程序

天气预报使用Python语言和Django框架开发的小程序天气预报是人们生活中非常重要的一项服务,为了方便用户随时获取天气信息,开发一个小程序成为了必需。

本文将介绍如何使用Python语言和Django框架来实现一个功能完善的天气预报小程序。

1. 简介天气预报小程序是基于Python语言和Django框架开发的一个简单而实用的应用程序。

它通过调用天气数据接口,获取实时的天气信息,并以直观的方式展示给用户。

用户可以输入城市名或选择当前位置来获取天气信息,还可以查看未来几天的天气预报。

2. 技术实现2.1 Python语言Python是一种简单易学的编程语言,拥有丰富的库和框架,非常适合用于快速的Web应用程序开发。

天气预报小程序利用Python语言来处理用户的请求并调用天气数据接口,同时也负责后端逻辑的处理和数据的存储。

2.2 Django框架Django是一个高效、灵活且开发效率极高的Python web框架。

它提供了许多可以加快Web应用程序开发的工具和功能,例如URL路由、视图函数、模板引擎等。

天气预报小程序借助Django框架来构建用户界面和处理与前端的交互。

3. 功能设计3.1 用户输入天气预报小程序可以接收用户输入的城市名或通过定位功能获取当前位置的经纬度。

用户可以在搜索框中输入城市名,小程序将根据用户输入的内容进行查询并展示相应的天气数据。

3.2 天气查询小程序将用户输入的城市名或经纬度作为参数,调用天气数据接口,获取实时的天气信息。

通过解析返回的数据,小程序可以获取当前时间的温度、风力、湿度等天气信息,并将其展示给用户。

3.3 未来天气预报除了当前的天气信息,小程序还提供了未来几天的天气预报。

用户可以通过滑动或点击切换不同的日期,小程序会即时更新对应日期的天气预报信息,包括最高温度、最低温度、天气状况等。

4. 开发步骤4.1 安装Python和Django首先,确保已经安装了Python和Django。

微信公众平台开发(82)天气预报

微信公众平台开发(82)天气预报

微信公众平台开发(82)天⽓预报在这篇教程中,我们将介绍如何在微信公众平台上开发天⽓预报功能。

我们将使⽤中国天⽓⽹的⽓象数据接⼝来获取天⽓信息。

这篇教程将介绍以下内容:1. 获取中国天⽓⽹的⽓象数据2. 在微信公众平台中回复天⽓⼀、中国天⽓⽹中国天⽓⽹提供了丰富的⽓象数据接⼝。

下⾯我们介绍主要的⼏个:国家根节点接⼝:/wmaps/xml/china.xml该接⼝以XML格式列出所有的省(⾃治区、直辖市)的中⽂名称(quName)和拼⾳名称(pyName)及省会城市的天⽓信息,其中⼴东的数据如下:<city quName="⼴东" pyName="guangdong" cityname="⼴州" state1="2" state2="7" stateDetailed="阴转⼩⾬" tem1="8" tem2="15" windState="微风"/>该接⼝的主要功能是通过它获取省级节点的拼⾳名称。

以⼴东为例:其pyName是“Guangdong”。

省级节点接⼝(以⼴东为例):/wmaps/xml/Guangdong.xml该接⼝以XML格式列出当前省下辖所有市的中⽂名称(quName)、拼⾳名称(pyName)、中⼼坐标、天⽓信息以及城市编码。

其中深圳的数据如下所⽰:<city cityX="409" cityY="257" cityname="深圳" centername="深圳" fontColor="FFFFFF" pyName="shenzhen" state1="1" state2="1" stateDetailed="多云" tem1="12" tem2="18" temNow="12" windState="微风" windDir="东北风" windPower在这个接⼝中主要获取城市名称及相应的城市编码。

国家气象局免费天气预报接口,城市代码(JSON格式)

国家气象局免费天气预报接口,城市代码(JSON格式)

{"城市代码": [{"省": "北京","市": [{"市名": "北京","编码": "101010100"},{"市名": "朝阳","编码": "101010300"},{"市名": "顺义","编码": "101010400"},{"市名": "怀柔","编码": "101010500"},{"市名": "通州","编码": "101010600"},{"市名": "昌平","编码": "101010700"},{"市名": "延庆","编码": "101010800"},{"市名": "丰台","编码": "101010900"},{"市名": "石景山","编码": "101011000"},{"市名": "大兴","编码": "101011100"{"市名": "房山","编码": "101011200" },{"市名": "密云","编码": "101011300" },{"市名": "门头沟","编码": "101011400" },{"市名": "平谷","编码": "101011500" },{"市名": "八达岭","编码": "101011600" },{"市名": "佛爷顶","编码": "101011700" },{"市名": "汤河口","编码": "101011800" },{"市名": "密云上甸子","编码": "101011900" },{"市名": "斋堂","编码": "101012000" },{"市名": "霞云岭","编码": "101012100" },{"市名": "北京城区","编码": "101012200"{"市名": "海淀","编码": "101010200"}]},{"省": "天津市","市": [{"市名": "天津","编码": "101030100"},{"市名": "宝坻","编码": "101030300"},{"市名": "东丽","编码": "101030400"},{"市名": "西青","编码": "101030500"},{"市名": "北辰","编码": "101030600"},{"市名": "蓟县","编码": "101031400"},{"市名": "汉沽","编码": "101030800"},{"市名": "静海","编码": "101030900"},{"市名": "津南","编码": "101031000"},{"市名": "塘沽","编码": "101031100"},{"市名": "大港","编码": "101031200"},{"市名": "武清","编码": "101030200"},{"市名": "宁河","编码": "101030700"}]},{"省": "上海","市": [{"市名": "上海","编码": "101020100"},{"市名": "宝山","编码": "101020300"},{"市名": "嘉定","编码": "101020500"},{"市名": "南汇","编码": "101020600"},{"市名": "浦东","编码": "101021300"},{"市名": "青浦","编码": "101020800"},{"市名": "松江","编码": "101020900"},{"市名": "奉贤","编码": "101021000"},{"市名": "崇明","编码": "101021100"},{"市名": "徐家汇","编码": "101021200"},{"市名": "闵行","编码": "101020200"},{"市名": "金山","编码": "101020700"}]},{"省": "河北","市": [{"市名": "石家庄","编码": "101090101"},{"市名": "张家口","编码": "101090301"},{"市名": "承德","编码": "101090402"},"市名": "唐山","编码": "101090501"},{"市名": "秦皇岛","编码": "101091101"},{"市名": "沧州","编码": "101090701"},{"市名": "衡水","编码": "101090801"},{"市名": "邢台","编码": "101090901"},{"市名": "邯郸","编码": "101091001"},{"市名": "保定","编码": "101090201"},{"市名": "廊坊","编码": "101090601"}]},{"省": "河南","市": [{"市名": "郑州","编码": "101180101"},{"市名": "新乡","编码": "101180301"{"市名": "许昌","编码": "101180401" },{"市名": "平顶山","编码": "101180501" },{"市名": "信阳","编码": "101180601" },{"市名": "南阳","编码": "101180701" },{"市名": "开封","编码": "101180801" },{"市名": "洛阳","编码": "101180901" },{"市名": "商丘","编码": "101181001" },{"市名": "焦作","编码": "101181101" },{"市名": "鹤壁","编码": "101181201" },{"市名": "濮阳","编码": "101181301" },{"市名": "周口","编码": "101181401"{"市名": "漯河","编码": "101181501"},{"市名": "驻马店","编码": "101181601"},{"市名": "三门峡","编码": "101181701"},{"市名": "济源","编码": "101181801"},{"市名": "安阳","编码": "101180201"}]},{"省": "安徽","市": [{"市名": "合肥","编码": "101220101"},{"市名": "芜湖","编码": "101220301"},{"市名": "淮南","编码": "101220401"},{"市名": "马鞍山","编码": "101220501"},{"市名": "安庆",},{"市名": "宿州","编码": "101220701" },{"市名": "阜阳","编码": "101220801" },{"市名": "亳州","编码": "101220901" },{"市名": "黄山","编码": "101221001" },{"市名": "滁州","编码": "101221101" },{"市名": "淮北","编码": "101221201" },{"市名": "铜陵","编码": "101221301" },{"市名": "宣城","编码": "101221401" },{"市名": "六安","编码": "101221501" },{"市名": "巢湖","编码": "101221601" },{"市名": "池州",},{"市名": "蚌埠","编码": "101220201"}]},{"省": "浙江","市": [{"市名": "杭州","编码": "101210101"},{"市名": "舟山","编码": "101211101"},{"市名": "湖州","编码": "101210201"},{"市名": "嘉兴","编码": "101210301"},{"市名": "金华","编码": "101210901"},{"市名": "绍兴","编码": "101210501"},{"市名": "台州","编码": "101210601"},{"市名": "温州","编码": "101210701"},{"市名": "丽水","编码": "101210801"},{"市名": "衢州","编码": "101211001"},{"市名": "宁波","编码": "101210401"}]},{"省": "重庆","市": [{"市名": "重庆","编码": "101040100"},{"市名": "合川","编码": "101040300"},{"市名": "南川","编码": "101040400"},{"市名": "江津","编码": "101040500"},{"市名": "万盛","编码": "101040600"},{"市名": "渝北","编码": "101040700"},{"市名": "北碚","编码": "101040800"},"市名": "巴南","编码": "101040900" },{"市名": "长寿","编码": "101041000" },{"市名": "黔江","编码": "101041100" },{"市名": "万州天城","编码": "101041200" },{"市名": "万州龙宝","编码": "101041300" },{"市名": "涪陵","编码": "101041400" },{"市名": "开县","编码": "101041500" },{"市名": "城口","编码": "101041600" },{"市名": "云阳","编码": "101041700" },{"市名": "巫溪","编码": "101041800" },{"市名": "奉节","编码": "101041900" },"市名": "巫山","编码": "101042000" },{"市名": "潼南","编码": "101042100" },{"市名": "垫江","编码": "101042200" },{"市名": "梁平","编码": "101042300" },{"市名": "忠县","编码": "101042400" },{"市名": "石柱","编码": "101042500" },{"市名": "大足","编码": "101042600" },{"市名": "荣昌","编码": "101042700" },{"市名": "铜梁","编码": "101042800" },{"市名": "璧山","编码": "101042900" },{"市名": "丰都","编码": "101043000" },"市名": "武隆","编码": "101043100"},{"市名": "彭水","编码": "101043200"},{"市名": "綦江","编码": "101043300"},{"市名": "酉阳","编码": "101043400"},{"市名": "秀山","编码": "101043600"},{"市名": "沙坪坝","编码": "101043700"},{"市名": "永川","编码": "101040200"}]},{"省": "福建","市": [{"市名": "福州","编码": "101230101"},{"市名": "泉州","编码": "101230501"},{"市名": "漳州","编码": "101230601"{"市名": "龙岩","编码": "101230701"},{"市名": "晋江","编码": "101230509"},{"市名": "南平","编码": "101230901"},{"市名": "厦门","编码": "101230201"},{"市名": "宁德","编码": "101230301"},{"市名": "莆田","编码": "101230401"},{"市名": "三明","编码": "101230801"}]},{"省": "甘肃","市": [{"市名": "兰州","编码": "101160101"},{"市名": "平凉","编码": "101160301"},{"市名": "庆阳",},{"市名": "武威","编码": "101160501" },{"市名": "金昌","编码": "101160601" },{"市名": "嘉峪关","编码": "101161401" },{"市名": "酒泉","编码": "101160801" },{"市名": "天水","编码": "101160901" },{"市名": "武都","编码": "101161001" },{"市名": "临夏","编码": "101161101" },{"市名": "合作","编码": "101161201" },{"市名": "白银","编码": "101161301" },{"市名": "定西","编码": "101160201" },{"市名": "张掖",}]},{"省": "广东","市": [{"市名": "广州","编码": "101280101"},{"市名": "惠州","编码": "101280301"},{"市名": "梅州","编码": "101280401"},{"市名": "汕头","编码": "101280501"},{"市名": "深圳","编码": "101280601"},{"市名": "珠海","编码": "101280701"},{"市名": "佛山","编码": "101280800"},{"市名": "肇庆","编码": "101280901"},{"市名": "湛江","编码": "101281001"},{"编码": "101281101" },{"市名": "河源","编码": "101281201" },{"市名": "清远","编码": "101281301" },{"市名": "云浮","编码": "101281401" },{"市名": "潮州","编码": "101281501" },{"市名": "东莞","编码": "101281601" },{"市名": "中山","编码": "101281701" },{"市名": "阳江","编码": "101281801" },{"市名": "揭阳","编码": "101281901" },{"市名": "茂名","编码": "101282001" },{"市名": "汕尾","编码": "101282101" },{"编码": "101280201"}]},{"省": "广西","市": [{"市名": "南宁","编码": "101300101"},{"市名": "柳州","编码": "101300301"},{"市名": "来宾","编码": "101300401"},{"市名": "桂林","编码": "101300501"},{"市名": "梧州","编码": "101300601"},{"市名": "防城港","编码": "101301401"},{"市名": "贵港","编码": "101300801"},{"市名": "玉林","编码": "101300901"},{"市名": "百色","编码": "101301001"},{"市名": "钦州","编码": "101301101"},{"市名": "河池","编码": "101301201"},{"市名": "北海","编码": "101301301"},{"市名": "崇左","编码": "101300201"},{"市名": "贺州","编码": "101300701"}]},{"省": "贵州","市": [{"市名": "贵阳","编码": "101260101"},{"市名": "安顺","编码": "101260301"},{"市名": "都匀","编码": "101260401"},{"市名": "兴义","编码": "101260906"},{"市名": "铜仁","编码": "101260601"},{"市名": "毕节","编码": "101260701"},{"市名": "六盘水","编码": "101260801"},{"市名": "遵义","编码": "101260201"},{"市名": "凯里","编码": "101260501"}]},{"省": "云南","市": [{"市名": "昆明","编码": "101290101"},{"市名": "红河","编码": "101290301"},{"市名": "文山","编码": "101290601"},{"市名": "玉溪","编码": "101290701"},{"市名": "楚雄","编码": "101290801"},{"市名": "普洱","编码": "101290901"},{"市名": "昭通","编码": "101291001"},{"市名": "临沧","编码": "101291101"},{"市名": "怒江","编码": "101291201"},{"市名": "香格里拉","编码": "101291301"},{"市名": "丽江","编码": "101291401"},{"市名": "德宏","编码": "101291501"},{"市名": "景洪","编码": "101291601"},{"市名": "大理","编码": "101290201"},{"市名": "曲靖","编码": "101290401"},{"市名": "保山","编码": "101290501"}]},{"省": "内蒙古","市": [{"市名": "呼和浩特","编码": "101080101"},{"市名": "乌海","编码": "101080301"},{"市名": "集宁","编码": "101080401"},{"市名": "通辽","编码": "101080501"},{"市名": "阿拉善左旗","编码": "101081201"},{"市名": "鄂尔多斯","编码": "101080701"},{"市名": "临河","编码": "101080801"},{"市名": "锡林浩特","编码": "101080901"},{"市名": "呼伦贝尔","编码": "101081000"},{"市名": "乌兰浩特","编码": "101081101"},{"市名": "包头","编码": "101080201"},{"市名": "赤峰","编码": "101080601"}]},{"省": "江西","市": [{"市名": "南昌","编码": "101240101"},{"市名": "上饶","编码": "101240301"},{"市名": "抚州","编码": "101240401"},{"市名": "宜春","编码": "101240501"},{"市名": "鹰潭","编码": "101241101"},{"市名": "赣州","编码": "101240701"},{"市名": "景德镇","编码": "101240801"},{"市名": "萍乡","编码": "101240901"},"市名": "新余","编码": "101241001"},{"市名": "九江","编码": "101240201"},{"市名": "吉安","编码": "101240601"}]},{"省": "湖北","市": [{"市名": "武汉","编码": "101200101"},{"市名": "黄冈","编码": "101200501"},{"市名": "荆州","编码": "101200801"},{"市名": "宜昌","编码": "101200901"},{"市名": "恩施","编码": "101201001"},{"市名": "十堰","编码": "101201101"},{"市名": "神农架","编码": "101201201"{"市名": "随州","编码": "101201301"},{"市名": "荆门","编码": "101201401"},{"市名": "天门","编码": "101201501"},{"市名": "仙桃","编码": "101201601"},{"市名": "潜江","编码": "101201701"},{"市名": "襄樊","编码": "101200201"},{"市名": "鄂州","编码": "101200301"},{"市名": "孝感","编码": "101200401"},{"市名": "黄石","编码": "101200601"},{"市名": "咸宁","编码": "101200701"}]},{"省": "四川","市": [{"市名": "成都","编码": "101270101"},{"市名": "自贡","编码": "101270301"},{"市名": "绵阳","编码": "101270401"},{"市名": "南充","编码": "101270501"},{"市名": "达州","编码": "101270601"},{"市名": "遂宁","编码": "101270701"},{"市名": "广安","编码": "101270801"},{"市名": "巴中","编码": "101270901"},{"市名": "泸州","编码": "101271001"},{"市名": "宜宾","编码": "101271101"},{"市名": "内江","编码": "101271201"},{"市名": "资阳","编码": "101271301"},{"市名": "乐山","编码": "101271401"},{"市名": "眉山","编码": "101271501"},{"市名": "凉山","编码": "101271601"},{"市名": "雅安","编码": "101271701"},{"市名": "甘孜","编码": "101271801"},{"市名": "阿坝","编码": "101271901"},{"市名": "德阳","编码": "101272001"},{"市名": "广元","编码": "101272101"},{"市名": "攀枝花","编码": "101270201"}]},{"省": "宁夏","市": [{"市名": "银川","编码": "101170101"},{"市名": "中卫","编码": "101170501"},{"市名": "固原","编码": "101170401"},{"市名": "石嘴山","编码": "101170201"},{"市名": "吴忠","编码": "101170301"}]},{"省": "青海省","市": [{"市名": "西宁","编码": "101150101"},{"市名": "黄南","编码": "101150301"},{"市名": "海北","编码": "101150801"},{"市名": "果洛","编码": "101150501"},{"市名": "玉树","编码": "101150601"},{"市名": "海西","编码": "101150701"},{"市名": "海东","编码": "101150201"},{"市名": "海南","编码": "101150401"}]},{"省": "山东","市": [{"市名": "济南","编码": "101120101"},{"市名": "潍坊","编码": "101120601"},{"市名": "临沂","编码": "101120901"},{"市名": "菏泽","编码": "101121001"},{"市名": "滨州","编码": "101121101"},{"市名": "东营","编码": "101121201"{"市名": "威海","编码": "101121301" },{"市名": "枣庄","编码": "101121401" },{"市名": "日照","编码": "101121501" },{"市名": "莱芜","编码": "101121601" },{"市名": "聊城","编码": "101121701" },{"市名": "青岛","编码": "101120201" },{"市名": "淄博","编码": "101120301" },{"市名": "德州","编码": "101120401" },{"市名": "烟台","编码": "101120501" },{"市名": "济宁","编码": "101120701" },{"市名": "泰安","编码": "101120801"。

天气预报接口(微信)

天气预报接口(微信)

JSON返回示例:{"resultcode": "200","reason": "查询成功!","result": {"sk": { /*当前实况天气*/"temp": "21", /*当前温度*/"wind_direction": "西风", /*当前风向*/"wind_strength": "2级", /*当前风力*/"humidity": "4%", /*当前湿度*/"time": "14:25" /*更新时间*/},"today": {"city": "天津","date_y": "2014年03月21日","week": "星期五","temperature": "8℃~20℃", /*今日温度*/"weather": "晴转霾", /*今日天气*/"weather_id": { /*天气唯一标识*/"fa": "00", /*天气标识00:晴*/"fb": "53" /*天气标识53:霾如果fa不等于fb,说明是组合天气*/},"wind": "西南风微风","dressing_index": "较冷", /*穿衣指数*/"dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。

微信天气预报接口代码

微信天气预报接口代码

微信天气预报接口代码分享-微信公众平台接口开发(一)微信公众号开放平台,可以利用现在网络上的各种工具接口,为自己的微信公众平台增加亮点。

天气查询,快递查询,手机归属地查询,身份证查询,公交查询,火车查询,健康指数查询,实时翻译(语音),百度百科,百度问答,人品计算,笑话……等接口开发都已经成熟了。

今天我们先来看看最简单普遍的微信天气预报功能中国天气网天气预报接口(官方接口)请求地址: /data/101010100.html首先我们创建一个类。

保存为:weather.class.php 天气预报的处理类<?php// 天气类class getweather{public function getw($str){if ($code){$content =file_get_contents("/data/{$code}.html");if(!$content){file_put_contents('weather.txt', '没有接受到天气数据',FILE_APPEND);return '爱油,抱歉,暂时没有取到天气数据,请稍后再试';}$result=json_decode($content,true);$info=$result['weatherinfo'];//文本形式$strw="{$info['date_y']} {$info['week']} \n{$info['city']}的天气情况\n";$strw .= "今天:({$info['temp1']}){$info['weather1']}{$info['wind1']}{$info['fl1']}。

\n";$strw .= "明天:({$info['temp2']}){$info['weather2']}{$info['wind2']}{$info['fl2']}。

微信小程序开发五:案例实践

微信小程序开发五:案例实践
<!--index.wxml--> <view class="container">
<view class="header"> <view class="title">{{weather.results[0].currentCity}}</view> <view class="desc">{{weather.date}}</view>他部分。然后调用 wx.request 请求天气预报数据。 接下来把上述接口组合起来,组成给应用层的接口,相应代码如下所示。
function loadWeatherData(callback) { getLocation(function(success, latitude, longitude){ getWeather(latitude, longitude, function(weatherData){ callback(weatherData); }); });
博客园 用户登录 代码改变世界 密码登录 短信登录 忘记登录用户名 忘记密码 记住我 登录 第三方登录/注册 没有账户, 立即注册
微信小程序开发五:案例实践 5.1天 气 预 报
在这一节中,我们将开市名称查询天气,而 本节中使用坐标进行查询。 在小合法域名中。如图21-7所示。
由于项目只有一个页面,所以不需要底部tab。另外设置网络请求时间为10秒,并且启用调试模式。 逻辑层实现 首先在common.js中使用获取用户当前地理位置接口获取用户的坐标地址,坐标类型选择gcj02。 //获取当前位置坐标
function getLocation(callback) { wx.getLocation({ type: 'gcj02', success: function(res) { callback(true, titude, res.longitude); }, fail: function() { callback(false); } })

开源免费天气预报接口API以及全国所有地区代码

开源免费天气预报接口API以及全国所有地区代码

开源免费天气预报接口API以及全国所有地区代码国家气象局提供的天气预报接口接口地址:对于第三个接口,返回信息比较全面,也是以json格式提供,格式如下{"weatherinfo": {//基本信息;"city":"北京","city_en":"beijing","date_y":"2012年2月16日","date":"","week":"星期四","fchh":"11","cityid":"101010100",//摄氏温度"temp1":"2℃~-7℃","temp2":"1℃~-7℃","temp3":"4℃~-7℃","temp4":"7℃~-5℃","temp5":"5℃~-3℃","temp6":"5℃~-2℃",//华氏温度;"tempF1":"35.6℉~19.4℉","tempF2":"33.8℉~19.4℉","tempF3":"39.2℉~19.4℉","tempF4":"44.6℉~23℉","tempF5":"41℉~26.6℉","tempF6":"41℉~28.4℉",//天气描述;"weather1":"晴","weather2":"晴","weather3":"晴","weather4":"晴转多云","weather5":"多云","weather6":"多云转阴",//天气描述图片序号"img1":"0","img2":"99","img3":"0","img4":"99","img5":"0","img6":"99","img7":"0","img8":"1","img9":"1","img10":"99","img11":"1","img12":"2","img_single":"0",//图片名称;"img_title1":"晴","img_title2":"晴","img_title3":"晴","img_title4":"晴","img_title5":"晴","img_title6":"晴","img_title7":"晴","img_title8":"多云","img_title9":"多云","img_title10":"多云","img_title11":"多云","img_title12":"阴","img_title_single":"晴",//风速描述"wind1":"北风3-4级转微风","wind2":"微风","wind3":"微风","wind4":"微风","wind5":"微风","wind6":"微风",//风速级别描述"fx1":"北风","fx2":"微风","fl1":"3-4级转小于3级","fl2":"小于3级","fl3":"小于3级","fl4":"小于3级","fl5":"小于3级","fl6":"小于3级",//今天穿衣指数;"index":"冷","index_d":"天气冷,建议着棉衣、皮夹克加羊毛衫等冬季服装。

基于GO的天气预报接口调用代码实例

基于GO的天气预报接口调用代码实例

基于GO的天气预报接口调用代码实例代码描述:基于GO的天气预报接口调用代码实例代码平台:聚合数据package mainimport("io/ioutil""net/http""net/url""fmt""encoding/json")//----------------------------------// 全国天气预报调用示例代码-聚合数据// 在线接口文档:/docs/39//----------------------------------const APPKEY = "*******************"//您申请的APPKEY func main(){//1.根据城市名/id查询天气Request1()//2.天气种类及标识列表Request2()//3.根据IP查询天气Request3()//4.根据GPS坐标查询天气Request4()//5.城市天气三小时预报Request5()//6.支持城市列表Request6()}//1.根据城市名/id查询天气func Request1(){//请求地址juheURL :="/weather/index"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("cityname","") //城市名或城市ID,如:&quot;苏州&quot;,需要u tf8 urlencodeparam.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("format","") //未来6天预报(future)两种返回格式,1或2,默认1 param.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//2.天气种类及标识列表func Request2(){//请求地址juheURL :="/weather/uni"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("key",APPKEY) //应用APPKEY(应用详细页查询)param.Set("dtype","") //返回数据的格式,xml或json,默认json//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//3.根据IP查询天气func Request3(){//请求地址juheURL :="/weather/ip"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("ip","") //ip地址,如:58.215.185.154param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("format","") //未来6天预报(future)两种返回格式,1或2,默认1 param.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//4.根据GPS坐标查询天气func Request4(){//请求地址juheURL :="/weather/geo"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("lon","") //经度,如:116.39277param.Set("lat","") //纬度,如:39.933748param.Set("format","") //未来6天预报(future)两种返回格式,1或2,默认1 param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//5.城市天气三小时预报func Request5(){//请求地址juheURL :="/weather/forecast3h"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("cityname","") //城市名,如:&quot;苏州&quot;param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}//6.支持城市列表func Request6(){//请求地址juheURL :="/weather/citys"//初始化参数param:=url.Values{}//配置请求参数,方法内部已处理urlencode问题,中文参数可以直接传参param.Set("dtype","") //返回数据格式:json或xml,默认jsonparam.Set("key",APPKEY) //你申请的key//发送请求data,err:=Get(juheURL,param)if err!=nil{fmt.Errorf("请求失败,错误信息:\r\n%v",err)}else{var netReturn map[string]interface{}json.Unmarshal(data,&netReturn)if netReturn["error_code"].(float64)==0{fmt.Printf("接口返回result字段是:\r\n%v",netReturn["result "])}}}// get 网络请求func Get(apiURL string,params url.Values)(rs[]byte,err error){var Url *url.URLUrl,err=url.Parse(apiURL)if err!=nil{fmt.Printf("解析url错误:\r\n%v",err)return nil,err}//如果参数中有中文参数,这个方法会进行URLEncodeUrl.RawQuery=params.Encode()resp,err:=http.Get(Url.String())if err!=nil{fmt.Println("err:",err)return nil,err}defer resp.Body.Close()return ioutil.ReadAll(resp.Body)}// post 网络请求 ,params 是url.Values类型func Post(apiURL string, params url.Values)(rs[]byte,err error){ resp,err:=http.PostForm(apiURL, params)if err!=nil{return nil ,err}defer resp.Body.Close()return ioutil.ReadAll(resp.Body)}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

微信天气预报接口代码 This model paper was revised by the Standardization Office on December 10, 2020
微信天气预报接口代码分享-微信公众平台接口开发(一)
微信公众号开放平台,可以利用现在网络上的各种工具接口,为自己的微信公众平台增加亮点。

天气查询,快递查询,手机归属地查询,身份证查询,公交查询,火车查询,健康指数查询,实时翻译(语音),百度百科,百度问答,人品计算,笑话……等接口开发都已经成熟了。

今天我们先来看看最简单普遍的微信天气预报功能
中国天气网天气预报接口(官方接口)
请求地址:
首先我们创建一个类。

保存为:天气预报的处理类
<php
"今天:({$info['temp1']}){$info['weather1']}{$info['wind1']}{$info['fl1']}。

\n";
$strw .= "明天:
({$info['temp2']}){$info['weather2']}{$info['wind2']}{$info['fl2']}。

\n";
return $strw;
}else{
return '没有获取到该城市的天气,请确定输入了正确的城市名称,如\'福州天气\'';
}
}
/**
* 验证接受到的数据是否合法
* @param string $str 传入的接受到的数据
* @return mixed|boolean 有数据返回$code,没有数据返回false;
*
*/
private function check($str){
气查询(语音)
2.快递查询
3.手机归属地查询
4.身份证查询
5.公交查询
6.火车查询
7.健康指数查询
8.实时翻译(语音)
9.百度百科
10.百度问答
11.人品计算
12.笑话
13.糗事
14.谜语
15.解梦
16.成语接龙
17.成语字典
18.陪聊
19.机器人学习功能。

相关文档
最新文档