IT评测·应用市场-qidao123.com
标题:
【中工开发者】HarmonyOS应用开发中怎样发送网络哀求
[打印本页]
作者:
怀念夏天
时间:
2024-12-29 17:36
标题:
【中工开发者】HarmonyOS应用开发中怎样发送网络哀求
【中工开发者】HarmonyOS应用开发中怎样发送网络哀求
无论在开发Wed应用,还是开发手机端应用,都离不开网络。而在网络开发中,发送HTTP哀求是必不可少的一部分。同样,在HarmonyOS应用开发中,无论是从第三方的API中获取数据,还是从后端服务器中获取数据,HTTP哀求是不可制止的。
下面是对在HarmonyOS应用开发中所需要涉及的HTTP相关内容的先容以及怎样实现的先容。
HTTP相关概述
HTTP采用B/S(Browser/Server)架构举行通信,同样也是一个应用层协议。它基于运输层TCP协议传输数据,并采用了轻便的哀求-相应方式举行交互,即:客户端根据自身需求发送相应的哀求至服务器,而服务器只能根据接收到的客户端哀求发送相应数据
HTTP主要包括:消息布局,哀求方法,状态码等
消息布局
HTTP 消息是客户端和服务器之间通信的底子,它们由一系列的文本行构成,遵照特定的格式和布局。主要分为哀求消息和相应消息。
哀求消息
哀求消息主要包罗:哀求行(request line)、哀求头(header)和哀求体构成
哀求行
哀求方法
:如GET,POST,PUT,DELETE等
URI
:哀求的资源路径
HTTP版本
:如 HTTP/1.1 或 HTTP/2
GET /index.html HTTP/1.1
复制代码
哀求头
包罗了客户端环境信息、哀求体的大小(假如有)、客户端支持的压缩类型等。
常见的哀求头包括Host、User-Agent、Accept、Accept-Encoding、Content-Length等。
哀求体
在某些类型的HTTP哀求(如 POST 和 PUT)中,哀求体包罗要发送给服务器的数据。
相应消息
HTTP 相应主要由三部分构成,分别是:状态行、相应头和相应体。
状态行
HTTP 版本
:与哀求消息中的版本相匹配。
状态码
:三位数,表示哀求的处理结果,如 200 表示乐成,404 表示未找到资源。
状态信息
:状态码的简短说明。
HTTP/1.1 200 OK
复制代码
相应头
包罗了服务器环境信息、相应体的大小、服务器支持的压缩类型等。
常见的相应头包括Content-Type、Content-Length、Server、Set-Cookie等。
相应体
包罗服务器返回的数据,如哀求的网页内容、图片、JSON数据等。
{
"message": "success感谢又拍云(upyun.com)提供CDN赞助",
"status": 200,
"date": "20241209",
"time": "2024-12-09 10:36:23",
"cityInfo": {
"city": "上海市",
"citykey": "101020100",
"parent": "上海",
"updateTime": "08:12"
},
"data": {
"shidu": "57%",
"pm25": 18.0,
"pm10": 30.0,
"quality": "优",
"wendu": "3.3",
"ganmao": "各类人群可自由活动",
"forecast": [
{
"date": "09",
"high": "高温 11℃",
"low": "低温 5℃",
"ymd": "2024-12-09",
"week": "星期一",
"sunrise": "06:41",
"sunset": "16:51",
"aqi": 31,
"fx": "东北风",
"fl": "2级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "10",
"high": "高温 12℃",
"low": "低温 8℃",
"ymd": "2024-12-10",
"week": "星期二",
"sunrise": "06:41",
"sunset": "16:51",
"aqi": 33,
"fx": "东北风",
"fl": "2级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "11",
"high": "高温 10℃",
"low": "低温 8℃",
"ymd": "2024-12-11",
"week": "星期三",
"sunrise": "06:42",
"sunset": "16:52",
"aqi": 32,
"fx": "北风",
"fl": "3级",
"type": "小雨",
"notice": "雨虽小,注意保暖别感冒"
},
{
"date": "12",
"high": "高温 10℃",
"low": "低温 6℃",
"ymd": "2024-12-12",
"week": "星期四",
"sunrise": "06:43",
"sunset": "16:52",
"aqi": 50,
"fx": "北风",
"fl": "3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "13",
"high": "高温 10℃",
"low": "低温 4℃",
"ymd": "2024-12-13",
"week": "星期五",
"sunrise": "06:44",
"sunset": "16:52",
"aqi": 53,
"fx": "西北风",
"fl": "3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "14",
"high": "高温 8℃",
"low": "低温 3℃",
"ymd": "2024-12-14",
"week": "星期六",
"sunrise": "06:44",
"sunset": "16:52",
"aqi": 66,
"fx": "西北风",
"fl": "3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "15",
"high": "高温 8℃",
"low": "低温 3℃",
"ymd": "2024-12-15",
"week": "星期日",
"sunrise": "06:45",
"sunset": "16:53",
"aqi": 59,
"fx": "西北风",
"fl": "3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "16",
"high": "高温 9℃",
"low": "低温 1℃",
"ymd": "2024-12-16",
"week": "星期一",
"sunrise": "06:45",
"sunset": "16:53",
"aqi": 62,
"fx": "西北风",
"fl": "2级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "17",
"high": "高温 10℃",
"low": "低温 3℃",
"ymd": "2024-12-17",
"week": "星期二",
"sunrise": "06:46",
"sunset": "16:54",
"aqi": 63,
"fx": "北风",
"fl": "2级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "18",
"high": "高温 11℃",
"low": "低温 5℃",
"ymd": "2024-12-18",
"week": "星期三",
"sunrise": "06:47",
"sunset": "16:54",
"aqi": 53,
"fx": "东北风",
"fl": "2级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "19",
"high": "高温 11℃",
"low": "低温 6℃",
"ymd": "2024-12-19",
"week": "星期四",
"sunrise": "06:47",
"sunset": "16:54",
"aqi": 45,
"fx": "东北风",
"fl": "2级",
"type": "阴",
"notice": "不要被阴云遮挡住好心情"
},
{
"date": "20",
"high": "高温 8℃",
"low": "低温 3℃",
"ymd": "2024-12-20",
"week": "星期五",
"sunrise": "06:48",
"sunset": "16:55",
"aqi": 58,
"fx": "西北风",
"fl": "3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "21",
"high": "高温 5℃",
"low": "低温 1℃",
"ymd": "2024-12-21",
"week": "星期六",
"sunrise": "06:48",
"sunset": "16:55",
"aqi": 66,
"fx": "西北风",
"fl": "3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
},
{
"date": "22",
"high": "高温 6℃",
"low": "低温 0℃",
"ymd": "2024-12-22",
"week": "星期日",
"sunrise": "06:49",
"sunset": "16:56",
"aqi": 55,
"fx": "西北风",
"fl": "3级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
},
{
"date": "23",
"high": "高温 8℃",
"low": "低温 3℃",
"ymd": "2024-12-23",
"week": "星期一",
"sunrise": "06:49",
"sunset": "16:56",
"aqi": 59,
"fx": "西北风",
"fl": "1级",
"type": "多云",
"notice": "阴晴之间,谨防紫外线侵扰"
}
],
"yesterday": {
"date": "08",
"high": "高温 10℃",
"low": "低温 5℃",
"ymd": "2024-12-08",
"week": "星期日",
"sunrise": "06:40",
"sunset": "16:51",
"aqi": 28,
"fx": "北风",
"fl": "3级",
"type": "晴",
"notice": "愿你拥有比阳光明媚的心情"
}
}
}
复制代码
在HarmonyOS应用开发中实现发送HTTTP哀求
1.获取权限
首先需要在module.json5设置文件中添加ohos.permission.INTERNET的权限
"requestPermissions": [
{
"name":'ohos.permission.INTERNET'
}
]
复制代码
2.哀求封装
将arkts中发送HTTP哀求的一些重复语句封装为一个工具类
1.导入
import { http } from '@kit.NetworkKit'
复制代码
2.创建哀求对象
let httpRequest = http.createHttp();
复制代码
3.订阅HTTP Response Header 变乱(可选)
根据需要,通过on方法订阅http相应头变乱,此接口会比request哀求先返回;
httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
复制代码
4
.
设置哀求
let requestOptions = { // 请求配置选项
method: http.RequestMethod.GET,
header: {
'Content-Type': 'application/json',
},
connectTimeout: 3000, // 连接超时时间
readTimeout: 3000, // 读取数据超时时间
}
复制代码
5.发送HTTP哀求
let res = httpRequest.request(url, requestOptions);
复制代码
6.烧毁哀求对象
httpRequest.destroy();
复制代码
完整代码如下:
import { http } from '@kit.NetworkKit'
;export function requestUrl( url:string,callback:(result)=>void ){ let httpRequest = http.createHttp();
//创建哀求对象 let requestOptions = { // 请求配置选项
method: http.RequestMethod.GET,
header: {
'Content-Type': 'application/json',
},
connectTimeout: 3000, // 连接超时时间
readTimeout: 3000, // 读取数据超时时间
}
// 订阅HTTP Response Header 变乱 httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
// 发起HTTP网络哀求 let res = httpRequest.request(url, requestOptions);
// 异步处理结果 res.then((data) => { callback(data.result) // 返回数据后回调 httpRequest.destroy();
}).catch((err) => { httpRequest.destroy();
// 烧毁 });}
复制代码
3.调用封装工具类
1.导入工具类
import { requestUrl } from '../http/HttpUtil';
复制代码
2.调用方法
url: string = 'http://t.weather.itboy.net/api/weather/city/101020100'
requestUrl(url, (result:string) => {
this.resultInfo = JSON.parse(result.toString())
})
复制代码
通过上述的简短先容,可以实现鸿蒙应用与后端的网络通信,满意差别业务场景的需求。假如想要更加高效。可以利用第三方库axios来实现网络哀求。
上述代码详情可以查看这里
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/)
Powered by Discuz! X3.4