3.19 HarmonyOS 网络请求工具类
1.3.72.3.8 uniapp用不了echarts3.3.114.ArkTs 网络请求注意事项5.3.18 记账本的bug修复
6.3.19 HarmonyOS 网络请求工具类
7.5.3 居家养老web端控制台8.5.21结组任务9.5.20结组作业10.5.17结组作业11.5.16结组作业12.5.15结组作业13.5.14结组作业14.5.27 spark先导15.5.29 matlab16.01梦断代码阅读笔记之一17.02梦断代码阅读笔记之二18.03大道至简阅读笔记之一19.05人月神话阅读笔记之一20.06人月神话阅读笔记之二21.阳大壮AI助手22.2.1923.小论文24.2.2625.架构漫谈26.3.7网络请求部分的代码实在是太麻烦了,所以把他封装成一个工具类使用起来方便些
今天先封装两个,一个是post把集合传给后端,一个是get查询全部
import http from '@ohos.net.http';
class AxiosUtil {
private httpRequest = http.createHttp();
constructor() {
// 用于订阅HTTP响应头
this.httpRequest.on('headersReceive', (header) => {
console.info('header: ' + JSON.stringify(header));
});
}
public async getAllUtil(url: string): Promise<any> {
return new Promise((resolve, reject) => {
this.httpRequest.request(
url,
{
method: http.RequestMethod.GET,
header: {
'Content-Type': 'application/json'
},
usingCache: true,
priority: 1,
connectTimeout: 60000,
readTimeout: 60000,
usingProtocol: http.HttpProtocol.HTTP1_1,
},
(err, data) => {
if (!err) {
try {
const res = JSON.parse(data.result as string);
resolve(res); // 解析成功后 resolve 出去
} catch (parseError) {
reject(parseError); // JSON 解析失败时 reject 出去
}
} else {
console.info('error:' + JSON.stringify(err));
this.httpRequest.off('headersReceive');
this.httpRequest.destroy();
reject(err); // 发生错误时 reject 出去
}
},
);
});
}
public async postUtil(postclass: object, url: string): Promise<any> {
const requestBody = JSON.stringify(postclass);
return new Promise((resolve, reject) => {
this.httpRequest.request(
url,
{
method: http.RequestMethod.POST,
header: {
'Content-Type': 'application/json'
},
extraData: requestBody,
expectDataType: http.HttpDataType.STRING,
usingCache: true,
priority: 1,
connectTimeout: 60000,
readTimeout: 60000,
usingProtocol: http.HttpProtocol.HTTP1_1,
},
(err, data) => {
if (!err) {
console.info('Result:' + JSON.stringify(data.result));
console.info('code:' + JSON.stringify(data.responseCode));
console.info('header:' + JSON.stringify(data.header));
console.info('cookies:' + JSON.stringify(data.cookies));
resolve(
data.result
);
} else {
console.info('error:' + JSON.stringify(err));
this.httpRequest.off('headersReceive');
this.httpRequest.destroy();
reject(err);
}
},
);
});
}
}
export default AxiosUtil;
然后就可以在别的界面直接调用咯
import AxiosUtil from '../common/util/axiosUtil';
class CourseDetails {
coursename: string;
teacher: string;
classroom: string;
constructor(coursename: string, teacher: string, classroom: string) {
this.coursename = coursename;
this.teacher = teacher;
this.classroom = classroom;
}
}
@Entry
@Component
struct AxiosPage {
courseDetails: CourseDetails = new CourseDetails('jack5', '王建', '206');
@State Return:string = 'error'
@State students:Object[] = null
axiosInstance:AxiosUtil = new AxiosUtil();
url : string = 'http://localhost:8080/newcourse'
url2 : string = 'http://localhost:8080/book'
async get() {
// 在适当的地方调用 postUtil 方法
this.Return = await this.axiosInstance.postUtil(this.courseDetails,this.url);
}
async get2() {
this.students = await this.axiosInstance.getAllUtil(this.url2)
// 在适当的地方调用 postUtil 方法
}
build() {
Row() {
Column() {
List(){
ForEach(this.students,item=>{
ListItem(){
Row(){
Text(item.bookName)
}
}
})
}
Text(this.Return)
Button('发送')
.onClick(() => this.get())
Button('发送2')
.onClick(() => this.get2())
}
.width('100%')
}
.height('100%')
}
}
本文作者:菜鸟de博客
本文链接:https://www.cnblogs.com/zeyangshuaige/p/18083975
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步