尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

HarmonyOS 5开发从入门到精通(十五):天气应用实战(上)

HarmonyOS 5开发从入门到精通(十五):天气应用实战(上)
📅 发布时间:2026/6/18 5:25:40

HarmonyOS 5开发从入门到精通(十五):天气应用实战(上)

本章将带领大家开发一个完整的天气应用,涵盖网络请求、JSON解析、UI展示等核心功能。通过本案例,你将掌握HarmonyOS应用开发的关键技能。

一、核心概念

1. 网络请求与数据模型

天气应用的核心是获取远程API数据并转换为本地数据模型。HarmonyOS提供了@ohos.net.http模块进行网络请求,配合JSON解析将服务器返回的数据转换为TypeScript对象,实现数据驱动UI。

2. 城市搜索与数据筛选

城市搜索功能通过监听输入框变化,实时过滤城市列表。结合ArkUI的响应式特性,当搜索关键词变化时,界面自动更新显示匹配结果。

二、关键API详解

1. 网络请求模块

import http from '@ohos.net.http'// 创建HTTP请求实例
const httpRequest = http.createHttp()// 发起GET请求
httpRequest.request(url, {method: http.RequestMethod.GET,connectTimeout: 10000,readTimeout: 10000
})

2. JSON解析

// 解析JSON字符串
const weatherData = JSON.parse(response.result)// 转换为JSON字符串
const jsonStr = JSON.stringify(weatherData)

3. 天气数据模型

class WeatherModel {city: string = ''temperature: number = 0condition: string = ''humidity: number = 0windSpeed: number = 0
}

4. 城市搜索过滤

// 过滤城市列表
const filteredCities = cityList.filter(city => city.name.includes(searchText)
)

5. 列表组件渲染

List() {ForEach(this.cityList, (city: CityInfo) => {ListItem() {Text(city.name)}})
}

6. 输入框组件

TextInput().placeholder('搜索城市').onChange((value: string) => {this.searchText = value})

7. 异步数据获取

async getWeatherData(city: string): Promise<WeatherModel> {try {const response = await httpRequest.request(url)return JSON.parse(response.result)} catch (error) {console.error('获取天气数据失败')}
}

8. 权限配置

{"requestPermissions": [{"name": "ohos.permission.INTERNET"}]
}

9. 状态管理

@State weatherData: WeatherModel = new WeatherModel()
@State cityList: CityInfo[] = []
@State searchText: string = ''

10. 页面生命周期

aboutToAppear() {this.loadWeatherData()
}onPageShow() {this.refreshData()
}

11. 错误处理

try {// 网络请求
} catch (error) {console.error('请求失败:', error)
}

12. 数据绑定

Text(this.weatherData.temperature + '°C').fontSize(48).fontColor(Color.White)

三、实战案例

完整代码实现

import http from '@ohos.net.http'
import { WeatherModel } from '../model/WeatherModel'@Entry
@Component
struct WeatherApp {@State weatherData: WeatherModel = new WeatherModel()@State cityList: CityInfo[] = []@State searchText: string = ''@State filteredCities: CityInfo[] = []private httpRequest = http.createHttp()aboutToAppear() {this.loadCities()this.loadWeatherData('北京')}// 加载城市列表private loadCities() {this.cityList = [{ name: '北京', code: '101010100' },{ name: '上海', code: '101020100' },{ name: '广州', code: '101280101' },{ name: '深圳', code: '101280601' },{ name: '杭州', code: '101210101' }]this.filteredCities = this.cityList}// 获取天气数据private async loadWeatherData(city: string) {try {const url = `https://api.example.com/weather?city=${city}`const response = await this.httpRequest.request(url, {method: http.RequestMethod.GET})if (response.responseCode === 200) {const data = JSON.parse(response.result)this.weatherData = {city: data.city,temperature: data.temp,condition: data.condition,humidity: data.humidity,windSpeed: data.windSpeed}}} catch (error) {console.error('获取天气数据失败:', error)}}// 搜索城市private searchCities() {if (this.searchText === '') {this.filteredCities = this.cityList} else {this.filteredCities = this.cityList.filter(city =>city.name.includes(this.searchText))}}build() {Column() {// 搜索框TextInput().placeholder('搜索城市').width('90%').height(40).backgroundColor(Color.White).margin({ top: 20 }).onChange((value: string) => {this.searchText = valuethis.searchCities()})// 城市列表List() {ForEach(this.filteredCities, (city: CityInfo) => {ListItem() {Text(city.name).fontSize(18).margin({ left: 20 })}.onClick(() => {this.loadWeatherData(city.name)})})}.width('100%').height('60%')// 天气信息展示Column() {Text(this.weatherData.city).fontSize(24).fontColor(Color.White)Text(this.weatherData.temperature + '°C').fontSize(48).fontColor(Color.White).margin({ top: 10 })Text(this.weatherData.condition).fontSize(16).fontColor(Color.White).margin({ top: 5 })Row() {Text('湿度: ' + this.weatherData.humidity + '%').fontSize(14).fontColor(Color.White)Text('风速: ' + this.weatherData.windSpeed + 'm/s').fontSize(14).fontColor(Color.White).margin({ left: 20 })}.margin({ top: 10 })}.width('100%').height('30%').backgroundColor('#4A90E2').padding(20)}.width('100%').height('100%').backgroundColor('#F5F5F5')}
}// 城市信息模型
interface CityInfo {name: stringcode: string
}

四、总结

✅ 关键知识点

  • 网络请求的异步处理与错误捕获
  • JSON数据的解析与模型转换
  • 城市搜索功能的实时过滤实现
  • 数据驱动UI的响应式更新机制

🔧 核心API列表

  • http.createHttp()- 创建HTTP请求实例
  • http.RequestMethod.GET- GET请求方法
  • JSON.parse()- JSON字符串解析
  • JSON.stringify()- 对象转JSON字符串
  • TextInput().onChange()- 输入框变化监听
  • ForEach()- 列表数据循环渲染
  • @State- 状态管理装饰器
  • async/await- 异步编程语法糖

💡 应用建议

  1. 网络请求建议使用try-catch包裹,处理网络异常
  2. 城市搜索可添加防抖优化,避免频繁触发过滤
  3. 天气数据可添加本地缓存,提升用户体验
  4. 建议使用真实的天气API(如和风天气、高德天气)替换示例中的模拟接口

通过本章学习,你已经掌握了天气应用的核心开发技能。下一章我们将继续完善应用,添加更多实用功能。

相关新闻

  • 3.框架设计
  • 【软件开发】如何做出好的项目
  • HarmonyOS 5开发从入门到精通(十三):待办事项应用实战(上)

最新新闻

  • Playwright自动化测试:从核心原理到实战应用的全方位指南
  • Claude Opus 4.7工程落地风险:不可控性如何摧毁AI生产信任
  • Django毕设项目: 基于 Django+Vue 的农业设备智能运维管理系统的设计与实现 基于 Django+Vue 的现代农业一体化管理系统(源码+文档,讲解、调试运行,定制等)
  • PowerPC 601缓存时序与总线仲裁机制深度解析
  • 一念成仙:看山不是山,看水不是水,为什么OPC创业的核心是商业模式,而非代码本身
  • 国内主流打包机厂家实测排行 适配电商物流多场景 - 起跑123

日新闻

  • 2026年不锈钢卷板厂家推荐排行榜:冷轧热轧/304/201不锈钢卷板,高颜值耐腐蚀源头厂家实力精选 - 企业推荐官【官方】
  • FLUX.1-dev FP8模型实战指南:24GB以下显卡高效部署方案
  • 2026佛山长途搬家价目表:跨省跨市搬家费用完整计算指南 - 从来都是英雄出少年

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号