当前位置:   article > 正文

HarmonyOS/OpenHarmony应用开发ets天气预报_deveco studio下载axios包

deveco studio下载axios包

使用工具:DevEco3.1.0.5,SDK9

文章目录

  • 使用ohpm下载三方库axios
  • 高德天气申请自己的key
  • 发送网络请求获取数据
  • 编写UI界面和程序


前言:

新版本的DevEco预览器可以使用联网功能了,这就很方便了。

文中布局不美观,大家不要介意

完整文件代码下载:src.rar - 蓝奏云


一、使用ohpm下载三方库axios

1.首先需要下载和配置ohpm

官网链接:ohpm使用指导-命令行工具-DevEco Studio使用指南-工具-HarmonyOS应用开发

2.查看OpenHarmony三方库中心仓

官网链接:OpenHarmony三方库中心仓

3.下载和安装第三方库axios

链接:OpenHarmony三方库中心仓

需要在项目名称目录下执行

ohpm install @ohos/axios

二、高德天气申请自己的key

1.注册账号,进入控制台,新建应用,添加Key

2.选择服务平台时需要选择Web服务

3.查看高德官方文档

链接:天气查询-基础 API 文档-开发指南-Web服务 API | 高德地图API (amap.com)

 4.生成自己的请求网址

例如:https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=<用户key>

三、发送网络请求获取数据

1.导入下载的第三方包

import axios from '@ohos/axios'

2.发请求,拿数据

  1. async getTodayData(){
  2. const data = await axios.get('自己申请的网址')
  3. console.log(data.data)
  4. }

 3.分析数据

四、编写UI界面和程序

1.纵向布局,分头部,中部,底部

头部代码:
  1. //头部
  2. Column({space:20}){
  3. Text('阿顾天气1.0')
  4. .fontSize(25).fontWeight(700).width('100%').textAlign(TextAlign.Center)
  5. Row(){
  6. Image(this.posiIco)
  7. .width(50).height(50).objectFit(ImageFit.Contain)
  8. //展示获取的城市,2.0版本添加点击事件,可以自主选择地区
  9. Text(this.address)
  10. .fontSize(30).fontColor(Color.Black)
  11. }
  12. }
  13. .width('100%').layoutWeight(2).justifyContent(FlexAlign.SpaceAround)
 中部代码:
  1. Column(){
  2. //分割线
  3. Divider().width('100%')
  4. //今日天气
  5. Column(){
  6. Text('今日天气:')
  7. .fontSize(20).fontWeight(800).width('100%')
  8. //空占位组件
  9. Blank()
  10. Column({space:10}){
  11. Text('天气情况:'+this.weatherToday)
  12. .fontSize(22).fontColor(Color.Brown)
  13. Image(this.weatherTodayPic)
  14. .width(80).height(50).objectFit(ImageFit.Contain)
  15. Text('气温:'+this.temToday+'°C')
  16. .fontSize(22).fontColor(Color.Brown)
  17. }.width('70%').border({width:1,color:Color.Pink}).margin({bottom:10})
  18. .borderRadius(30).backgroundColor("#ffbabac1").padding(10)
  19. }
  20. .width('100%').layoutWeight(1).padding(10)
  21. //分割线
  22. Divider().width('100%')
  23. //未来三日天气
  24. Column(){
  25. Text('未来三日天气:')
  26. .fontSize(20).fontWeight(800).width('100%')
  27. List({space:10}){
  28. ForEach(this.weaArr,(item:weatherTypes,index:Number)=>{
  29. //如果index==0,那么根据返回的数据,会渲染今天的天气,所以要排除今日的,渲染未来3日的
  30. if(index !== 0){
  31. ListItem(){
  32. Column({space:10}){
  33. Text(item.date)
  34. .fontSize(17)
  35. Text('白天天气:'+item.dayweather)
  36. .fontSize(20).fontColor(Color.Brown)
  37. Image(this.judeWeather(item))
  38. .width(80).height(50).objectFit(ImageFit.Contain)
  39. Text('气温:'+((item.daytemp*1.0+item.nighttemp*1.0)/2)+'°C')//取平均值做温度
  40. .fontSize(18).fontWeight(800)
  41. Row(){
  42. Text('风向:')
  43. .fontSize(15).fontColor(Color.Brown)
  44. Image(this.windImg)
  45. .width(50).height(40).objectFit(ImageFit.Contain)
  46. // @ts-ignore
  47. .rotate(this.judeWind(item))//图标旋转的角度
  48. }
  49. }.border({width:1,color:Color.Pink}).padding(10).borderRadius(15).backgroundColor("#ffbabac1")
  50. }.margin(20)
  51. }
  52. })
  53. }.listDirection(Axis.Horizontal)//将list方向改为横向
  54. }
  55. .width('100%').layoutWeight(2).padding(10)
  56. }
  57. .width('100%').layoutWeight(8)
底部代码:
  1. //底部信息区
  2. Row(){
  3. Text('信息:天气数据来自于高德天气API@2024.3.21')
  4. .fontSize(15).fontWeight(500).fontColor("#ffae2e2e").width('100%').textAlign(TextAlign.Center)
  5. }
  6. .width('100%').layoutWeight(1)

2.编写今日天气和未来3日天气的网络请求数据

  1. //获取今日天气数据
  2. async getTodayData(){
  3. const data = await axios.get(this.todayUrl)
  4. this.weaList = data.data.lives
  5. //赋值地址,默认北京东城区
  6. this.address = this.weaList[0].province+'市'+this.weaList[0].city
  7. //将获取的今日天气情况赋值
  8. this.weatherToday = this.weaList[0].weather
  9. //执行函数,判断今日获取的天气情况,动态赋值根据天气情况展示图片
  10. this.getWeatherTodayPic()
  11. //将获取的今日温度赋值
  12. this.temToday = this.weaList[0].temperature
  13. }
  14. //获取未来3日的天气数据
  15. async getLaterData(){
  16. const data = await axios.get(this.laterUrl)
  17. //将获取到的数据存入数组中
  18. this.weaArr = data.data.forecasts[0].casts
  19. }

 3.编写根据天气情况动态展示图片函数

  1. //动态判断今日天气情况,根据天气情况动态展示图片
  2. getWeatherTodayPic(){
  3. if(this.weatherToday.includes('雨')){
  4. this.weatherTodayPic = 'https://tse4-mm.cn.bing.net/th/id/OIP-C.xTBpOQF2baaiYA1stR_jDQHaHa?w=175&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7';
  5. }else if(this.weatherToday.includes('阴')){
  6. this.weatherTodayPic = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.T4a2oN9ggyuwxQnQBY5c7wHaFi?w=264&h=197&c=7&r=0&o=5&dpr=1.5&pid=1.7';
  7. }else if(this.weatherToday.includes('晴')){
  8. this.weatherTodayPic = 'https://tse1-mm.cn.bing.net/th/id/OIP-C.xwmwAj0dteZV-l6hutuGLAHaHa?w=198&h=199&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  9. }else if(this.weatherToday.includes('多云')){
  10. this.weatherTodayPic = 'https://tse4-mm.cn.bing.net/th/id/OIP-C.lFiZ9pHlWoMraLRTv4RMSAHaHa?w=169&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  11. }
  12. }
  13. //动态判断未来3日天气情况,展示未来三日的每一天的图片
  14. judeWeather(item){
  15. if(item.dayweather == '晴'){
  16. return this.fineImg
  17. }else if(item.dayweather.includes('云')){
  18. return this.cloudImg
  19. }else if(item.dayweather.includes('雨')){
  20. return this.rainImg
  21. }else if(item.dayweather.includes('阴')){
  22. return this.yinImg
  23. }
  24. return this.fineImg
  25. }

4.编写判断风向的函数

  1. //根据返回的数据结果,判断风向,根据东南西北,赋值角度,默认北方向
  2. judeWind(item){
  3. if(item.daywind == '东'){
  4. return this.wind = 90
  5. }else if(item.daywind == '东北'){
  6. return this.wind = 45
  7. }else if(item.daywind == '南'){
  8. return this.wind = 180
  9. }else if(item.daywind == '东南'){
  10. return this.wind = 135
  11. }else if(item.daywind == '西'){
  12. return this.wind = 270
  13. }else if(item.daywind == '西南'){
  14. return this.wind = 225
  15. }else if(item.daywind == '西北'){
  16. return this.wind = 315
  17. }
  18. return this.wind = 0
  19. }

总结代码

完整代码如下:

  1. import axios from '@ohos/axios'
  2. @Entry
  3. @Component
  4. struct Index {
  5. //请求今日数据的网络地址
  6. @State todayUrl:string = 'https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=ce1f551fc7d7227efd45ee58fd7887e7'
  7. //请求未来3日数据的网络地址
  8. @State laterUrl:string = 'https://restapi.amap.com/v3/weather/weatherInfo?city=110101&key=ce1f551fc7d7227efd45ee58fd7887e7&extensions=all'
  9. //数据显示
  10. @State message:string = ''
  11. //查询天气的地址
  12. @State address:string = ''
  13. //定位图片链接
  14. @State posiIco:string = 'https://tse3-mm.cn.bing.net/th/id/OIP-C.62J8FA41P_ftriSbFbw-HgHaHa?w=166&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  15. //================今日的天气变量=================//
  16. //接受天气数据的数组
  17. @State weaList: weatherType[] = []
  18. //今日天气
  19. @State weatherToday:string = ''
  20. //今日天气图片(网络获取)
  21. @State weatherTodayPic:string = ''
  22. //今日温度
  23. @State temToday:string = ''
  24. //================未来3日的天气变量=================//
  25. //接受未来3日天气数据的数组
  26. @State weaArr: weatherTypes[] = []
  27. //晴天展示的图像
  28. @State fineImg:string = 'https://tse1-mm.cn.bing.net/th/id/OIP-C.xwmwAj0dteZV-l6hutuGLAHaHa?w=198&h=199&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  29. //多云展示的图像
  30. @State cloudImg:string = 'https://tse4-mm.cn.bing.net/th/id/OIP-C.lFiZ9pHlWoMraLRTv4RMSAHaHa?w=169&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  31. //下雨展示的图像
  32. @State rainImg:string = 'https://tse4-mm.cn.bing.net/th/id/OIP-C.xTBpOQF2baaiYA1stR_jDQHaHa?w=175&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  33. //阴天展示的图像
  34. @State yinImg:string = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.T4a2oN9ggyuwxQnQBY5c7wHaFi?w=264&h=197&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  35. //风向图标
  36. @State windImg:string = 'https://img.ixintu.com/upload/jpg/20210523/79f0359886d38d97112c4b78b9107710_26936_800_800.jpg!con'
  37. //风向角度
  38. @State wind:number = 0
  39. aboutToAppear(){
  40. this.getTodayData()
  41. this.getLaterData()
  42. }
  43. //获取今日天气数据
  44. async getTodayData(){
  45. const data = await axios.get(this.todayUrl)
  46. this.weaList = data.data.lives
  47. //赋值地址,默认北京东城区
  48. this.address = this.weaList[0].province+'市'+this.weaList[0].city
  49. //将获取的今日天气情况赋值
  50. this.weatherToday = this.weaList[0].weather
  51. //执行函数,判断今日获取的天气情况,动态赋值根据天气情况展示图片
  52. this.getWeatherTodayPic()
  53. //将获取的今日温度赋值
  54. this.temToday = this.weaList[0].temperature
  55. }
  56. //获取未来3日的天气数据
  57. async getLaterData(){
  58. const data = await axios.get(this.laterUrl)
  59. //将获取到的数据存入数组中
  60. this.weaArr = data.data.forecasts[0].casts
  61. }
  62. //动态判断今日天气情况,根据天气情况动态展示图片
  63. getWeatherTodayPic(){
  64. if(this.weatherToday.includes('雨')){
  65. this.weatherTodayPic = 'https://tse4-mm.cn.bing.net/th/id/OIP-C.xTBpOQF2baaiYA1stR_jDQHaHa?w=175&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7';
  66. }else if(this.weatherToday.includes('阴')){
  67. this.weatherTodayPic = 'https://tse2-mm.cn.bing.net/th/id/OIP-C.T4a2oN9ggyuwxQnQBY5c7wHaFi?w=264&h=197&c=7&r=0&o=5&dpr=1.5&pid=1.7';
  68. }else if(this.weatherToday.includes('晴')){
  69. this.weatherTodayPic = 'https://tse1-mm.cn.bing.net/th/id/OIP-C.xwmwAj0dteZV-l6hutuGLAHaHa?w=198&h=199&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  70. }else if(this.weatherToday.includes('多云')){
  71. this.weatherTodayPic = 'https://tse4-mm.cn.bing.net/th/id/OIP-C.lFiZ9pHlWoMraLRTv4RMSAHaHa?w=169&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7'
  72. }
  73. }
  74. //动态判断未来3日天气情况,展示未来三日的每一天的图片
  75. judeWeather(item){
  76. if(item.dayweather == '晴'){
  77. return this.fineImg
  78. }else if(item.dayweather.includes('云')){
  79. return this.cloudImg
  80. }else if(item.dayweather.includes('雨')){
  81. return this.rainImg
  82. }else if(item.dayweather.includes('阴')){
  83. return this.yinImg
  84. }
  85. return this.fineImg
  86. }
  87. //根据返回的数据结果,判断风向,根据东南西北,赋值角度,默认北方向
  88. judeWind(item){
  89. if(item.daywind == '东'){
  90. return this.wind = 90
  91. }else if(item.daywind == '东北'){
  92. return this.wind = 45
  93. }else if(item.daywind == '南'){
  94. return this.wind = 180
  95. }else if(item.daywind == '东南'){
  96. return this.wind = 135
  97. }else if(item.daywind == '西'){
  98. return this.wind = 270
  99. }else if(item.daywind == '西南'){
  100. return this.wind = 225
  101. }else if(item.daywind == '西北'){
  102. return this.wind = 315
  103. }
  104. return this.wind = 0
  105. }
  106. build() {
  107. Column({space:20}){
  108. //头部
  109. Column({space:20}){
  110. Text('阿顾天气1.0')
  111. .fontSize(25).fontWeight(700).width('100%').textAlign(TextAlign.Center)
  112. Row(){
  113. Image(this.posiIco)
  114. .width(50).height(50).objectFit(ImageFit.Contain)
  115. //展示获取的城市,2.0版本添加点击事件,可以自主选择地区
  116. Text(this.address)
  117. .fontSize(30).fontColor(Color.Black)
  118. }
  119. }
  120. .width('100%').layoutWeight(2).justifyContent(FlexAlign.SpaceAround)
  121. Column(){
  122. //分割线
  123. Divider().width('100%')
  124. //今日天气
  125. Column(){
  126. Text('今日天气:')
  127. .fontSize(20).fontWeight(800).width('100%')
  128. //空占位组件
  129. Blank()
  130. Column({space:10}){
  131. Text('天气情况:'+this.weatherToday)
  132. .fontSize(22).fontColor(Color.Brown)
  133. Image(this.weatherTodayPic)
  134. .width(80).height(50).objectFit(ImageFit.Contain)
  135. Text('气温:'+this.temToday+'°C')
  136. .fontSize(22).fontColor(Color.Brown)
  137. }.width('70%').border({width:1,color:Color.Pink}).margin({bottom:10})
  138. .borderRadius(30).backgroundColor("#ffbabac1").padding(10)
  139. }
  140. .width('100%').layoutWeight(1).padding(10)
  141. //分割线
  142. Divider().width('100%')
  143. //未来三日天气
  144. Column(){
  145. Text('未来三日天气:')
  146. .fontSize(20).fontWeight(800).width('100%')
  147. List({space:10}){
  148. ForEach(this.weaArr,(item:weatherTypes,index:Number)=>{
  149. //如果index==0,那么根据返回的数据,会渲染今天的天气,所以要排除今日的,渲染未来3日的
  150. if(index !== 0){
  151. ListItem(){
  152. Column({space:10}){
  153. Text(item.date)
  154. .fontSize(17)
  155. Text('白天天气:'+item.dayweather)
  156. .fontSize(20).fontColor(Color.Brown)
  157. Image(this.judeWeather(item))
  158. .width(80).height(50).objectFit(ImageFit.Contain)
  159. Text('气温:'+((item.daytemp*1.0+item.nighttemp*1.0)/2)+'°C')//取平均值做温度
  160. .fontSize(18).fontWeight(800)
  161. Row(){
  162. Text('风向:')
  163. .fontSize(15).fontColor(Color.Brown)
  164. Image(this.windImg)
  165. .width(50).height(40).objectFit(ImageFit.Contain)
  166. // @ts-ignore
  167. .rotate(this.judeWind(item))//图标旋转的角度
  168. }
  169. }.border({width:1,color:Color.Pink}).padding(10).borderRadius(15).backgroundColor("#ffbabac1")
  170. }.margin(20)
  171. }
  172. })
  173. }.listDirection(Axis.Horizontal)//将list方向改为横向
  174. }
  175. .width('100%').layoutWeight(2).padding(10)
  176. }
  177. .width('100%').layoutWeight(8)
  178. //底部信息区
  179. Row(){
  180. Text('信息:天气数据来自于高德天气API@2024.3.21')
  181. .fontSize(15).fontWeight(500).fontColor("#ffae2e2e").width('100%').textAlign(TextAlign.Center)
  182. }
  183. .width('100%').layoutWeight(1)
  184. }
  185. .width('100%').height('100%')
  186. .backgroundImage('https://tse1-mm.cn.bing.net/th/id/OIP-C.DvKV8H07XleMDFfcVdtoMAAAAA?w=197&h=329&c=7&r=0&o=5&dpr=1.5&pid=1.7')
  187. .backgroundImageSize({width:'100%',height:'100%'})
  188. }
  189. }
  190. //==================模拟接口类型,便于数组.出来===================//
  191. //今日天气
  192. interface weatherType{
  193. province: string;
  194. city: string;
  195. adcode:string;
  196. weather: string;
  197. temperature: string;
  198. winddirection: string;
  199. windpower: string;
  200. humidity: string;
  201. reporttime: string;
  202. temperature_float: string;
  203. humidity_float: string;
  204. }
  205. //未来3日天气
  206. interface weatherTypes{
  207. date: string;
  208. week: string;
  209. dayweather: string;
  210. nightweather: string;
  211. daytemp: number;
  212. nighttemp: number;
  213. daywind:string;
  214. nightwind: string;
  215. daypower: string;
  216. nightpower: string;
  217. daytemp_float: string;
  218. nighttemp_float: string;
  219. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/772513
推荐阅读
相关标签
  

闽ICP备14008679号