移动软件开发实验2记录

一、实验目标

1、掌握服务器域名配置和临时服务器部署;2、掌握 wx.request 接口的用法。

二、实验步骤

列出实验的关键步骤、代码解析、截图。

1.首先做好准备工作,注册和风天气的账号,选择webAPI,在控制台项目管理中找到密钥key,记下来。

 

 

2.还要在小程序开发平台配置服务域名,根据和风文档的解释,配置request域名

 

 

3.然后就可以写代码了,微信开发者工具,创建一个小程序,开始写代码。

在这之前要创建images文件夹,然后将表示天气的图标导入。

关键代码:index.wxml,用组件来展示需要的天气信息

<!--index.wxml-->
<view class="container">
<!--区域1-->
<picker mode='region' bindchange='regionChange'>
  <view>{{region}}</view>
</picker>
<!--区域2-->
<text>{{now.temp}}℃ {{now.text}}</text>
<!--区域3-->
<image src='/images/weather_icon_s1_bw/{{now.icon}}.png' mode='widthFix'></image>
<!--区域4-->
<view class='detail'>
  <view class='bar'>
    <view class='box'>湿度</view>
    <view class='box'>气压</view>
    <view class='box'>能见度</view>
  </view>
  <view class='bar'>
    <view class='box'>{{now.humidity}} %</view>
    <view class='box'>{{now.pressure}} hPa</view>
    <view class='box'>{{now.vis}} km</view>
  </view>
<view class='bar'>
    <view class='box'>风向</view>
    <view class='box'>风速</view>
    <view class='box'>风力</view>
  </view>
  <view class='bar'>
    <view class='box'>{{now.windDir}}</view>
    <view class='box'>{{now.windSpeed}} km/h</view>
    <view class='box'>{{now.windScale}} 级</view>
  </view>
</view>
</view>

index.wxss,页面布局。

/**index.wxss**/
​
.container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-around;
}
text{
  font-size: 80rpx;
  color: #3c5f81;
}
image{
  width: 220rpx;
}
.detail{
  width: 100%;
  display: flex;
  flex-direction: column;
}
.bar{
  display: flex;
  flex-direction: row;
  margin: 20rpx 0;
}
.box{
  width: 33.3%;
  text-align: center;
}
​

index.js 里面最重要的两个方法就是getWeather()和getlocation()

 data: {
    region:['山东省','青岛市','黄岛区'],//给所有的变量一个初始数据
    locationID:101120202,
    now:{
      temp:1,
      text:'未知',
      icon:'999',
      humidity:2,
      pressure:3,
      vis:4,
      windDir:5,
      windSpeed:6,
      windScale:7
    }
  },

  regionChange:function(e){
    this.setData({
      region:e.detail.value
    });
    this.getlocation().then(result =>{//为了解决异步问题
      this.getWeather();
    });
  },
getWeather:function(e){
  var that=this;
  wx.request({
    url:'https://devapi.qweather.com/v7/weather/now?',//请求服务器域名
    data:{
    location:that.data.locationID,//因为在请求地址的时候,如果直接用region[1],它得到的可能会是汉字,而不是编码或者经纬度,所以需要创建一个变量来转换一下
    key:'ec16b77770cd4ab1869952e5cc766d00'//自己的key
    },
    success:function(res){
      console.log(res.data);
      that.setData({
      now:res.data.now//将请求得到的数据给now
    });
    }
  })
},
getlocation:function(){//请求根据汉字来请求地址编号
  var that=this;
  return new Promise(resolve =>{
  wx.request({
    url:'https://geoapi.qweather.com/v2/city/lookup?',
    data:{
    location:that.data.region[2],
    adm:that.data.region[1],
    key:'ec16b77770cd4ab1869952e5cc766d00'
    },
    success:function(res) {
      console.log(res.data);
      that.data.locationID=res.data.location[0].id//请求得到地址编号给到locationID
      return resolve();
      },
    })
  });
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlocation().then(result =>{
      this.getWeather();//为解决异步问题,要先请求地址得到地点编号,然后根据地址得到天气
    });
  },

4.最后得到的页面

 

三、程序运行结果

在初始页面上可以点击地名,更改地址:

 

 

选择自己想要查看的地址:

 

四、问题总结与体会

描述实验过程中所遇到的问题,以及是如何解决的。有哪些收获和体会,对于课程的安排有哪些建议。

遇到的问题:

1.报code:400的错,经过查询,url的请求参数location需要为编码或者经纬度,但如果直接使用region[1]得到的是汉字地址,就会报错。所以我们需要一个新的变量locationID和方法getlocation()来做一个转换。

 

如图,我们需要得到的是locationID,而不能直接用region[1]来表示。

  

2.拼写错误,有时候报code:400的错误,如果能确保URL和它的参数没错的话,那么很可能是拼写错误。

3.渲染层网络层错误,这个错误并不会直接影响结果,所以不修改也没关系。

在网上查询发现它是因为异步问题引起的。如果要修改的话,可以像上面那样, this.getlocation().then(result =>{this.getWeather()});

 

4.要微信开发工具要切换成游客模式。

5.在为变量赋名字的时候可以先将请求的url复制到游览器看一下,因为好像这些名字是会变的。

 

体会:

这次因为我自己的拼写错误,一直报code:400的错误,后面找了很久,才发现是拼写错误,所以让我更加深刻的意识到,敲代码一定要仔细。

另外本次实验也让我更加了解了如何去使用request域名以及对微信小程序的开发也更了解一点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值