当前位置:   article > 正文

鸿蒙应用开发之数据请求_鸿蒙js开发模式下请求接口封装

鸿蒙js开发模式下请求接口封装

背景

本文以个人的鸿蒙开发项目为例,功能为高校查询,使用的API接口官网为:

我要查 - 专业API数据接口|IP地址查询|手机号码归属地查询|身份证号码查询|邮政编码查询|行政区划代码查询|院校代码/高等学校查询|POS商户编号|天气查询|快递查询https://www.woyaocha.net/

官方API 参考为:

数据请求-网络管理-接口-手机、平板、智慧屏和智能穿戴开发-JS API参考-HarmonyOS应用开发https://developer.harmonyos.com/cn/docs/documentation/doc-references/js-apis-network-data-request-0000000000626077

前期准备

首先需要配置网络访问权限

在mian目录下的config.json文件中的module中添加网络权限

  1. "module": {
  2. ...
  3. "reqPermissions": [
  4. {
  5. "reason": "",
  6. "name": "ohos.permission.INTERNET"
  7. },
  8. }

 实现

本文采用的是鸿蒙js环境中的fetch接口(如果项目的SDK版本为API Version 6或以上推荐使用http接口)

导入模块

在js文件中导入模块

import fetch from '@system.fetch';

鸿蒙数据请求默认支持https协议,如使用接口的协议为http,需要在config.json里增加network标签,属性标识 "cleartextTraffic": true。即:

  1. {
  2. "deviceConfig": {
  3. "default": {
  4. "network": {
  5. "cleartextTraffic": true
  6. }
  7. ...
  8. }
  9. }
  10. }

创建变量以接收返回值

  1. export default {
  2. data: {
  3. School:[],
  4. },
  5. ...
  6. }

编写数据请求函数,使用fetch.fecth发起数据请求。

由于本文使用的API接口需要sha256加密,所以推荐使用组件为

Gitee 极速下载/js-sha256https://gitee.com/mirrors/js-sha256?_from=gitee_search在js的开头使用一下代码以导入sha256插件。

var sha256=require('js-sha256').sha256;

根据插件要求对数据进行加密(如使用API接口文档中没有要求请忽略此步骤)。

var Key=sha256("appid=10000###&module=getUniversityInfo&school="+SchoolName+"&appkey=094b8337af541d9e6cdd5daff1111111");   

以上appid和appkey均为不可用状态,详情请查看文章开头的API参考文档。

参数

参数名类型必填说明

url

string

发起网络请求的URL地址。

callback

AsyncCallback<HttpResponse>

回调函数。

数据请求实现代码

推荐将请求到的数据转成JSON格式,代码为:

this.responseData=JSON.parse(response.data);

 数据请求代码:

  1. fetch.fetch({
  2. url: "https://cha.ebaitian.cn/api/json?type=get&appid=10000###&module=getUniversityInfo&school="+SchoolName+"&sign="+that.Key,
  3. responseType:"json",
  4. success:function(response){
  5. that.responseData=JSON.parse(response.data);
  6. var addItem={
  7. school_img:"/common/images/School/BSchool.png",
  8. school_name:that.responseData.universityInfo.uname,
  9. address:that.responseData.universityInfo.area,
  10. piece:[
  11. {
  12. id:that.responseData.universityInfo.levels
  13. },
  14. {
  15. id:that.responseData.universityInfo.nature
  16. },
  17. {
  18. id:that.responseData.universityInfo.genre
  19. }
  20. ]
  21. };
  22. console.info("查询成功");
  23. },
  24. fail(){
  25. console.info("异常")
  26. }
  27. });

将接收到的数据插入到定义好的变量中:

this.School.unshift(addItem);

网络请求完整代码:

  1. InitSelect(SchoolName){
  2. var Key=sha256("appid=10000###&module=getUniversityInfo&school="+SchoolName+"&appkey=094b8337af541d9e6cdd5daff1111111");
  3. console.log(that.Key);
  4. fetch.fetch({
  5. url: "https://cha.ebaitian.cn/api/json?type=get&appid=10000###&module=getUniversityInfo&school="+SchoolName+"&sign="+Key,
  6. responseType:"json",
  7. success:function(response){
  8. that.responseData=JSON.parse(response.data);
  9. var addItem={
  10. school_img:"/common/images/School/BSchool.png",
  11. school_name:that.responseData.universityInfo.uname,
  12. address:that.responseData.universityInfo.area,
  13. piece:[
  14. {
  15. id:that.responseData.universityInfo.levels
  16. },
  17. {
  18. id:that.responseData.universityInfo.nature
  19. },
  20. {
  21. id:that.responseData.universityInfo.genre
  22. }
  23. ]
  24. };
  25. this.School.unshift(addItem);
  26. console.info("查询成功");
  27. },
  28. fail(){
  29. console.info("异常")
  30. }
  31. });
  32. },

实现自动查询

在data中创建select_name数组。

select_name:["浙江大学","北京科技大学","清华大学","北京大学"],

 在生命周期函数onInit中实现将select_name数组中的院校名称依次传入InitSelect函数。

  1. onInit(){
  2. var that=this;
  3. that.select_name.forEach(element => {
  4. that.InitSelect(element);
  5. });
  6. },
  7. ...

以上代码即可实现鸿蒙js开发中的数据请求

前端部分代码:

  1. <div class="container">
  2. <div class="main">
  3. <div class="content">
  4. <list>
  5. <list-item class="school_div" for="School">
  6. <div class="SchoolContent">
  7. <image class="School_img" src="{{$item.school_img}}"></image>
  8. <div class="content_school" >
  9. <text class="School_Name">{{$item.school_name}}</text>
  10. <text class="address">{{$item.address}}</text>
  11. <div class="piece_div">
  12. <div class="piece" for="$item.piece">
  13. <text>{{$item.id}}</text>
  14. </div>
  15. </div>
  16. </div>
  17. </div>
  18. <div class="Number_divider_div">
  19. <divider class="Number_divider"></divider>
  20. </div>
  21. </list-item>
  22. </list>
  23. </div>
  24. </div>
  25. </div>

实现效果

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

闽ICP备14008679号