抖音上做我女朋友网站,最火的主题wordpress,苏州建设工程交易中心网站,最权威的网站推广设计先来看一下效果 本项目界面搭建基于ArkUI中TS扩展的声明式开发范式#xff0c;
数据接口是和风#xff08;天气预报#xff09;#xff0c;
使用ArkUI自带的网络请求调用接口。
我想要实现的一个功能是#xff0c;查询当前城市的实时天气#xff0c;
目前已实现的功能…先来看一下效果 本项目界面搭建基于ArkUI中TS扩展的声明式开发范式
数据接口是和风天气预报
使用ArkUI自带的网络请求调用接口。
我想要实现的一个功能是查询当前城市的实时天气
目前已实现的功能有
默认查询北京的天气预报查看当前的天气查看未来七天的天气
通过本项目你能学到的知识有
网络请求条件渲染状态管理
先来看一下
目录结构
├── config.json
├── ets
│ └── MainAbility
│ ├── app.ets
│ ├── common
│ │ └── RealtimeWeather.ets
│ ├── data
│ │ ├── get_test.ets
│ │ └── get_week_test.ets
│ ├── model
│ │ ├── daily.ets
│ │ ├── now.ets
│ │ └── weatherModel.ets
│ └── pages
│ └── Main.ets
└── resources├── base│ ├── element│ │ ├── color.json│ │ └── string.json│ └── media│ └── icon.png└── rawfile接下来开始正文
我们先分析一下结构 我们可以分为三块
第一部分为实时天气信息栏 代码如下
// ts-nocheck/*** 该组件为实时天气预报组件** powered by 坚果* 2022/7/20*/Entry
Componentexport struct RealtimeWeather{State temp: string 9State text: string 坚果State isRequestSucceed: boolean truebuild(){Column() {Text($r(app.string.city)).fontSize(30)Row() {Text(this.temp).fontSize(100)Text(℃).fontSize(30).margin({ top: 10 })}.alignItems(VerticalAlign.Top).margin({ top: 5 })Text(this.text).fontSize(36).margin({ top: 5 })}.margin({ top: 50 })}}第二部分为
this.WeatherText(日期)this.WeatherText(天气)this.WeatherText(日出)this.WeatherText(日落)第三部分为
Scroll(){Column(){ForEach(this.future, (item: WeatherWeekData) {Row() {this.WeatherText(item.fxDate)this.WeatherText(item.textDay)this.WeatherText(item.sunrise)this.WeatherText(item.sunset)}.margin({left:10})}, item item.fxDate)}
}最后用Column包裹
完整的代码如下
Main.ets
// ts-nocheck/** Copyright (c) 2021 JianGuo Device Co., Ltd.* Licensed under the Apache License, Version 2.0 (the License);* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
import { WeatherModel, WeatherData, WeatherWeekData, } from ../model/weatherModel;import { RealtimeWeather } from ../common/RealtimeWeather
import { getWeekTest } from ../data/get_week_test
import { getTest } from ../data/get_testimport prompt from system.prompt;
import http from ohos.net.http;Entry
Component
struct Main {aboutToAppear() {this.getRequest()this.getWeekRequest()}State realtime: WeatherData getTest()State future: ArrayWeatherWeekData getWeekTest()State isRequestSucceed: boolean trueBuilder WeatherText(text: string) {Text(text).fontSize(14).layoutWeight(1).textAlign(TextAlign.Center).margin({ top: 10, bottom: 10 })}build() {Column() {if (this.isRequestSucceed) {// 当前天气RealtimeWeather({ temp: this.realtime.temp, text: this.realtime.text })Row() {this.WeatherText(日期)this.WeatherText(天气)this.WeatherText(日出)this.WeatherText(日落)}.margin({top:20})Scroll(){Column(){ForEach(this.future, (item: WeatherWeekData) {Row() {this.WeatherText(item.fxDate)this.WeatherText(item.textDay)this.WeatherText(item.sunrise)this.WeatherText(item.sunset)}.margin({left:10})}, item item.fxDate)}}Text(数据来自和风天气).fontSize(14).margin({ bottom: 30 })}}.width(100%).height(100%)}// 请求方式GET 获取一周天气预报getWeekRequest() {// 每一个httpRequest对应一个http请求任务不可复用let httpRequest http.createHttp()let url https://devapi.qweather.com/v7/weather/7d?location101010100key48fbadf80bbc43ce853ab9a92408373ehttpRequest.request(url, (err, data) {if (!err) {if (data.responseCode 200) {console.info(data.result data.result)// 解析数据var weatherModel: WeatherModel JSON.parse(data.result.toString())// 判断接口返回码0成功if (weatherModel.code 200) {// 设置数据this.future weatherModel.dailythis.isRequestSucceed true;ForEach(weatherModel.daily, (item: WeatherWeekData) {console.log(console.info(data.resultitem.fxDate item.fxDate))}, item item.date)console.info(data.result weatherModel.daily)} else {// 接口异常弹出提示prompt.showToast({ message: 数据请求失败 })}} else {// 请求失败弹出提示prompt.showToast({ message: 网络异常 })}} else {// 请求失败弹出提示prompt.showToast({ message: err.message })}})}// 请求方式GETgetRequest() {// 每一个httpRequest对应一个http请求任务不可复用let httpRequest http.createHttp()let url https://devapi.qweather.com/v7/weather/now?location101010100key48fbadf80bbc43ce853ab9a92408373ehttpRequest.request(url, (err, data) {if (!err) {if (data.responseCode 200) {console.info(data.result data.result)// 解析数据//this.content data.result;// 解析数据var weatherModel: WeatherModel JSON.parse(data.result.toString())// 判断接口返回码0成功if (weatherModel.code 200) {// 设置数据this.realtime weatherModel.nowthis.isRequestSucceed true;console.info(data.resultthis.content weatherModel.now)} else {// 接口异常弹出提示prompt.showToast({ message: 数据请求失败 })}} else {// 请求失败弹出提示prompt.showToast({ message: 网络异常 })}} else {// 请求失败弹出提示prompt.showToast({ message: err.message })}})}
}里面用到了网络请求
网络请求的步骤
1、声明网络请求权限 在entry下的config.json中module字段下配置权限
reqPermissions: [{name: ohos.permission.INTERNET}
]2、支持http明文请求 默认支持https如果要支持http在entry下的config.json中deviceConfig字段下配置
default: {network: {cleartextTraffic: true}
}3、创建HttpRequest
// 导入模块
import http from ohos.net.http;
// 创建HttpRequest对象
let httpRequest http.createHttp();4、发起请求 GET请求默认为GET请求 // 请求方式GETgetRequest() {// 每一个httpRequest对应一个http请求任务不可复用let httpRequest http.createHttp()let url https://devapi.qweather.com/v7/weather/now?location101010100key48fbadf80bbc43ce853ab9a92408373ehttpRequest.request(url, (err, data) {if (!err) {if (data.responseCode 200) {console.info(data.result data.result)// 解析数据//this.content data.result;// 解析数据var weatherModel: WeatherModel JSON.parse(data.result.toString())// 判断接口返回码0成功if (weatherModel.code 200) {// 设置数据this.realtime weatherModel.nowthis.isRequestSucceed true;console.info(data.resultthis.content weatherModel.now)} else {// 接口异常弹出提示prompt.showToast({ message: 数据请求失败 })}} else {// 请求失败弹出提示prompt.showToast({ message: 网络异常 })}} else {// 请求失败弹出提示prompt.showToast({ message: err.message })}})}5、解析数据简单示例 1.网络请求到的json字符串
export function getTest() {return [{obsTime: 2022-07-20T09:2408:00,temp: 28,feelsLike: 29,icon: 101,text: 多云,wind360: 225,windDir: 西南风,windScale: 3,windSpeed: 17,humidity: 71,precip: 0.0,pressure: 1000,vis: 8,cloud: 91,dew: 21},]
}2.创建相应的对象
export class WeatherWeekData {fxDate: string //sunrise: string //sunset: string //moonrise: string //moonset: string //moonPhase: string //moonPhaseIcon: string //tempMax: string //tempMin: string //iconDay: string //textDay: stringtextNight: string //wind360Day: string //windDirDay: string //windScaleDay: string //windSpeedDay: string //wind360Night: string //windDirNight: string //dew: string //windScaleNight: string // windSpeedNight: string //humidity: string //precip: string //pressure: string //vis: string //cloud: string //uvIndex: string //}实况天气
目前支持全国4000个市县区和海外15万个城市实时天气数据包括实时温度、体感温度、风力风向、相对湿度、大气压强、降水量、能见度、露点温度、云量等数据。
请求URL
// 北京实况天气 https://devapi.qweather.com/v7/weather/now?location101010100key你的KEY请求参数
请求参数包括必选和可选参数如不填写可选参数将使用其默认值参数之间使用进行分隔。
key
用户认证key请参考如何获取你的KEY。例如 key123456789ABC
location
需要查询地区的LocationID或以英文逗号分隔的经度,纬度坐标十进制最多支持小数点后两位LocationID可通过城市搜索服务获取。例如 location101010100 或 location116.41,39.92
返回数据格式
// 北京实况天气 // https://devapi.qweather.com/v7/weather/now?location101010100key你的KEY{code: 200,updateTime: 2020-06-30T22:0008:00,fxLink: http://hfx.link/2ax1,now: {obsTime: 2020-06-30T21:4008: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: [QWeather,NMC,ECMWF],license: [commercial license]}
}// 请求方式GETgetRequest() {// 每一个httpRequest对应一个http请求任务不可复用let httpRequest http.createHttp()let url https://devapi.qweather.com/v7/weather/now?location101010100key48fbadf80bbc43ce853ab9a92408373ehttpRequest.request(url, (err, data) {if (!err) {if (data.responseCode 200) {console.info(data.result data.result)// 解析数据//this.content data.result;// 解析数据var weatherModel: WeatherModel JSON.parse(data.result.toString())// 判断接口返回码0成功if (weatherModel.code 200) {// 设置数据this.realtime weatherModel.nowthis.isRequestSucceed true;console.info(data.resultthis.content weatherModel.now)} else {// 接口异常弹出提示prompt.showToast({ message: 数据请求失败 })}} else {// 请求失败弹出提示prompt.showToast({ message: 网络异常 })}} else {// 请求失败弹出提示prompt.showToast({ message: err.message })}})}七天天气预报
接口
// 北京7天预报 // https://devapi.qweather.com/v7/weather/7d?location101010100key你的KEY返回数据
// 北京3天预报
// 商业版 https://api.qweather.com/v7/weather/3d?location101010100key你的KEY
// 开发版 https://devapi.qweather.com/v7/weather/3d?location101010100key你的KEY{code: 200,updateTime: 2021-11-15T16:3508: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]}
}代码
// 请求方式GET 获取一周天气预报getWeekRequest() {// 每一个httpRequest对应一个http请求任务不可复用let httpRequest http.createHttp()let url https://devapi.qweather.com/v7/weather/7d?location101010100key48fbadf80bbc43ce853ab9a92408373ehttpRequest.request(url, (err, data) {if (!err) {if (data.responseCode 200) {console.info(data.result data.result)// 解析数据var weatherModel: WeatherModel JSON.parse(data.result.toString())// 判断接口返回码0成功if (weatherModel.code 200) {// 设置数据this.future weatherModel.dailythis.isRequestSucceed true;ForEach(weatherModel.daily, (item: WeatherWeekData) {console.log(console.info(data.resultitem.fxDate item.fxDate))}, item item.date)console.info(data.result weatherModel.daily)} else {// 接口异常弹出提示prompt.showToast({ message: 数据请求失败 })}} else {// 请求失败弹出提示prompt.showToast({ message: 网络异常 })}} else {// 请求失败弹出提示prompt.showToast({ message: err.message })}})}城市搜索
调用接口Get方式
请求URL
# 搜索关键字beij
// https://geoapi.qweather.com/v2/city/lookup?locationbeijkey你的KEYlocation
需要查询地区的名称支持文字、以英文逗号分隔的经度,纬度坐标十进制最多支持小数点后两位、LocationID或Adcode仅限中国城市。例如 location北京 或 location116.41,39.92 模糊搜索当location传递的为文字时支持模糊搜索即用户可以只输入城市名称一部分进行搜索最少一个汉字或2个字符结果将按照相关性和Rank值进行排列便于开发或用户进行选择他们需要查看哪个城市的天气。例如locationbei将返回与bei相关性最强的若干结果包括黎巴嫩的贝鲁特和中国的北京市 重名当location传递的为文字时可能会出现重名的城市例如陕西省西安市、吉林省辽源市下辖的西安区和黑龙江省牡丹江市下辖的西安区此时会根据Rank值排序返回所有结果。在这种情况下可以通过adm参数的方式进一步确定需要查询的城市或地区例如location西安adm黑龙江 名词解释
Rank值
Rank值是表明一个城市或地区排名的数字基于多种因素综合计算而来例如人口、面积、GDP、搜索热度等。取值范围为1-10在定位搜索服务中返回的结果除了关键字的相关性以外也会参考该城市的Rank值。数值越大代表该城市或地区的人口越多、面积更大或更加热门。例如陕西省西安市的Rank值就要比黑龙江省牡丹江市西安区更高当使用“西安”作为关键字定位的时候西安市的排名要高于西安区。
LocationID
LocationID或locid是城市、地区或POI点的ID一般由数字或字母数字组成是一个地点的唯一标识。LocationID可以通过定位搜索服务获取中国地区、热门海外城市、一些POI点的LocationID还可以通过城市列表下载。
如果大家还没有掌握鸿蒙现在想要在最短的时间里吃透它我这边特意整理了《鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程》以及《鸿蒙开发学习手册》共计890页希望对大家有所帮助https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3
鸿蒙语法ArkTS、TypeScript、ArkUI等…视频教程https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3 OpenHarmony APP开发教程步骤https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3 《鸿蒙开发学习手册》
如何快速入门https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3
1.基本概念 2.构建第一个ArkTS应用 3.……
开发基础知识https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3
1.应用基础知识 2.配置文件 3.应用数据管理 4.应用安全管理 5.应用隐私保护 6.三方应用调用管控机制 7.资源分类与访问 8.学习ArkTS语言 9.……
基于ArkTS 开发https://docs.qq.com/doc/DZVVBYlhuRkZQZlB3
1.Ability开发 2.UI开发 3.公共事件与通知 4.窗口管理 5.媒体 6.安全 7.网络与链接 8.电话服务 9.数据管理 10.后台任务(Background Task)管理 11.设备管理 12.设备使用信息统计 13.DFX 14.国际化开发 15.折叠屏系列 16.……
鸿蒙生态应用开发白皮书V2.0PDFhttps://docs.qq.com/doc/DZVVkRGRUd3pHSnFG
文章转载自: http://www.morning.hlfnh.cn.gov.cn.hlfnh.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.ljtwp.cn.gov.cn.ljtwp.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.npqps.cn.gov.cn.npqps.cn http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn http://www.morning.gqddl.cn.gov.cn.gqddl.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.xwzsq.cn.gov.cn.xwzsq.cn http://www.morning.bcngs.cn.gov.cn.bcngs.cn http://www.morning.kpgft.cn.gov.cn.kpgft.cn http://www.morning.lqws.cn.gov.cn.lqws.cn http://www.morning.nfccq.cn.gov.cn.nfccq.cn http://www.morning.grpfj.cn.gov.cn.grpfj.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.fwblh.cn.gov.cn.fwblh.cn http://www.morning.wjzzh.cn.gov.cn.wjzzh.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.xqgfy.cn.gov.cn.xqgfy.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.bwqr.cn.gov.cn.bwqr.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.glnxd.cn.gov.cn.glnxd.cn http://www.morning.zmtrk.cn.gov.cn.zmtrk.cn http://www.morning.fpqq.cn.gov.cn.fpqq.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.btns.cn.gov.cn.btns.cn http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.fwnyz.cn.gov.cn.fwnyz.cn http://www.morning.epeij.cn.gov.cn.epeij.cn http://www.morning.llgpk.cn.gov.cn.llgpk.cn http://www.morning.hkchp.cn.gov.cn.hkchp.cn http://www.morning.kcypc.cn.gov.cn.kcypc.cn http://www.morning.snbry.cn.gov.cn.snbry.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.zlfxp.cn.gov.cn.zlfxp.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.lztrt.cn.gov.cn.lztrt.cn http://www.morning.ckfyp.cn.gov.cn.ckfyp.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.mhwtq.cn.gov.cn.mhwtq.cn http://www.morning.knlgk.cn.gov.cn.knlgk.cn http://www.morning.yrqb.cn.gov.cn.yrqb.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.qnkqk.cn.gov.cn.qnkqk.cn http://www.morning.saletj.com.gov.cn.saletj.com http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.ktnt.cn.gov.cn.ktnt.cn http://www.morning.qwbtr.cn.gov.cn.qwbtr.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.lwygd.cn.gov.cn.lwygd.cn http://www.morning.wgzzj.cn.gov.cn.wgzzj.cn http://www.morning.ybyln.cn.gov.cn.ybyln.cn http://www.morning.nlywq.cn.gov.cn.nlywq.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.rgzc.cn.gov.cn.rgzc.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn http://www.morning.mgbcf.cn.gov.cn.mgbcf.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.nspzy.cn.gov.cn.nspzy.cn http://www.morning.pjjkz.cn.gov.cn.pjjkz.cn