当前位置: 首页 > 科技观察

从微信小程序到鸿蒙JS开发[03]-fetch获取数据&简单天气预报

时间:2023-03-14 12:04:51 科技观察

更多内容请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto。com/#zz在微信小程序中,如果需要向远程服务器请求数据,可以使用wx.request接口。那么在鸿蒙js的开发中,请求远程服务器需要进行以下步骤:1.在config.json中配置网络权限和可信域名。网络权限的配置在module.reqPermissions中,配置了以下三种权限。工具有提示,比较友好。"module":{"reqPermissions":[{"name":"ohos.permission.GET_NETWORK_INFO"},{"name":"ohos.permission.SET_NETWORK_INFO"},{"name":"ohos.permission.INTERNET"}],...信任域名的配置在deviceConfig中,默认为空对象,需要配置如下。“deviceConfig”:{“default”:{“network”:{“usesCleartext”:true,“securityConfig”:{“domainSettings”:{“cleartextPermitted”:true,“domains”:[{“subdomains”:true,”name":"apis.juhe.cn"},{"subdomains":true,"name":"api.seniverse.com"},{"subdomains":true,"name":"v.juhe.cn"},{"subdomains":true,"name":"api.tianapi.com"}]}}}}},domains数组中subdomains为是否信任子域,name为域名,不需要填写协议。如果没有配置请求的服务器域名,则无法请求成功,工具也不会报错。一定要记得在这里配置服务器域名。2.在js文件中引入fetch模块。鸿蒙js请求远程服务器的模块是fetch,需要在js文件最前面引入。importfetchfrom'@system.fetch';这里也有提示。3.调用fetch.fetch发送请求。看一下fetch模块的封装。请求参数、响应类型、回调函数都可以在对象中定义,与wx.request()基本相同。exportdefaultclassFetch{/***通过网络获取数据。*@paramoptions*/staticfetch(options:{/***ResourceURL.*/url:string;/***请求参数,可以是string类型或JSONobject.*/data?:string|object;/***Requestheader,其中包含请求的所有属性。*/header?:Object;/***可用的请求方法:OPTIONS、GET、HEAD、POST、PUT、DELETE和TRACE。默认值为GET。*/method?:string;/***返回类型可以是文本,也可以是JSON。默认情况下,返回类型是根据内容确定的-键入服务器返回的标头。*/responseType?:string;/***在成功获取网络数据时调用。*/success?:(data:IFetch)=>void;/***在获取网络数据失败时调用。*/fail?:(data:any,code:number)=>void;/***Calledwhentheexecutioniscompleted.*/complete?:()=>void;}):void;}比如我在页面初始化执行的onInit()方法中请求聚合数据的天气预报接口,可以这样写:onInit(){//loadweatherforecastfetch.fetch({url:'http://apis.juhe.cn/simpleWeather/query?city=%E5%8D%97%E4%BA%AC&key=xxxxxxxxx',responseType:'json',success:res=>{...}});}4.处理返回的数据,需要调用JSON.parse()。鸿蒙js开发目前调试不方便。虽然有console.log()、console.info()等方法打印log,但是实际运行的时候发现并没有打印log。所以我只能在视图中划出一小块区域进行调试。这里可以看到,虽然已经设置了responseType为json,但是使用'时还是会报错,红线。调用JSON.parse()转成json类型,问题解决。onInit(){//加载天气预报fetch.fetch({url:'http://apis.juhe.cn/simpleWeather/query?city=%E5%8D%97%E4%BA%AC&key=e4b4e30c713b6e2a24f4a851258c8457',responseType:'json',success:res=>{console.info(JSON.stringify(res.data));//不打印logletdata=JSON.parse(res.data);//这个必须加。nowWeather=data.result.realtime;letdailyWeather=data.result.future;for(letiindailyWeather){dailyWeather[i].date=dailyWeather[i].date.substr(5,5);}this.dailyWeather=dailyWeather;}});附上这部分天气预报的代码:{{nowWeather.info}}{{nowWeather.temperature}}?C风向:{{nowWeather.direct}}{{nowWeather.power}}空气质量:{{nowWeather.aqi}}

{{$item.date}}{{$item.weather}}{{$item.temperature}}
/*weather*/.weather{background-image:url('./common/weatherbg.jpg');background-size:contain;}.weathertext{color:#fdfdfd;}.now{width:100%;height:260px;margin-top:30px;display:flex;align-items:center;justify-content:space-around;}.now>text{font-size:60px;}.nowPhe{margin-left:20px;}.nowOther{margin-right:20px;display:flex;flex-direction:column;height:220px;justify-content:space-around;}.daily{margin-top:30px;display:flex;flex-direction:column;}.dailyItem{margin:030px030px;height:120px;border-bottom:1pxsolid#bbbbbb;display:flex;justify-content:space-between;align-items:center;}更多内容请访问:与华为共建的鸿蒙技术社区https://harmonyos.51cto.com/#zz【编者推荐】CDN:什么是边缘CDN和虚拟CDN(vCDN)?终于有人说清楚了datacenter什么都不是你一定用过的4种最慢的动态编程语言2021年将爆发勒索软件攻击为什么一些高级开发人员不喜欢Python