# OhosHttp **Repository Path**: RampageCat2016/OhosHttp ## Basic Information - **Project Name**: OhosHttp - **Description**: OhosHttp是对HarmonyOS Next原生网络请求框架的封装,结合okhttp的特性开发的网络请求库。 - **Primary Language**: TypeScript - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-10-09 - **Last Updated**: 2025-01-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # OhosHttp ## 介绍 OhosHttp是对HarmonyOS Next原生网络请求框架的封装,结合okhttp的特性开发的网络请求库。目前实现的功能特性如下所示: 1.支持get、post、head、options、download、upload 2.支持任务调度,用户可以自定义任务调度管理器。默认任务管理器:当有相同的请求时,后面发起的请求会被忽略,但如果后面的请求setExpressed(true),则该请求可以直接发起访问,且不会因为执行队列满员进入等待队列。 3.支持设置tag,并可以调用Cancel(tag)取消请求,或CancelAll()取消全部请求 4.支持定义拦截器,可以灵活添加拦截器 ![introduce.gif](./library/introduce.gif) ## 安装教程 ``` ohpm install @lynyko/ohoshttp ``` ## 使用说明 使用前可以先设置host,同时可以配置连接超时和读取超时,可以添加拦截器,请求库自带日志拦截器、重连拦截器、重定向拦截器、请求方法纠错拦截器(当误用get和post方法时,拦截器会修正请求方法) ``` NetConfig.getInstance().config('https://www.wanandroid.com/', (net) => { net.setConnectTimeout(30_000) // 默认30s net.setReadTimeout(30_000) // 默认30s net.addInterceptor(new LogInterector()) net.addInterceptor(new RetryInterector()) net.addInterceptor(new CacheInterector(CacheMode.CACHE_NET)) net.addInterceptor(new RequestMethodInterector()) net.addInterceptor(new RedirectInterector()) }) ``` ### Get请求方法 ``` Get>(`https://www.wanandroid.com/article/list/${this.i}/json`, (requestBody) => { }).then((result) => { if(result.errorCode === 0){ this.text = JSON.stringify(result.data) } }) 如果请求url以http开发,框架直接使用该url,否则与host拼接组成新的url ``` ### Post请求方法 ``` Post('user/login', (requestBody) => { requestBody.setParam('username', 'jfeiowf') requestBody.setParam('password', 'abcdefg') }).then((result) => { this.text = JSON.stringify(result) }) ``` ### Head请求方法 ``` Head('https://www.wanandroid.com/banner/json', (requestBody) => { }).then((result) => { this.text = JSON.stringify(result) }).catch((error) => { console.info('error', JSON.stringify(error)) }) ``` ### Options请求方法 ``` Options('https://www.wanandroid.com/', (requestBody) => { }).then((result) => { this.text = JSON.stringify(result) }).catch((error) => { console.info('error', JSON.stringify(error)) }) ``` ### Download请求方法 ``` Download('https://q5.itc.cn/images01/20240409/f7181bdfda4b4deeabcfb2ba03b0aa1a.jpeg', path, (requestBody) => { requestBody.setDownloadCallback((process) => { console.info('process', JSON.stringify(process)) }) }).then(() => { this.path = `file://${path}` }).catch((err) => { console.error(JSON.stringify(err)) }) 可以通过setDownloadCallback监听下载进度,当下载完成时会调用then来通知开发人员已经下载完成 ``` ### upload请求方法 ``` let filePath = getContext(this).cacheDir + "/hello.txt"; let fd = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); fs.truncateSync(fd.fd); fs.writeSync(fd.fd, "hello, world!"); Upload('', (requestBody) => { requestBody.addFile('files', filePath.replace(getContext(this).cacheDir, 'internal://cache')) requestBody.setUploadCallback((process) => { console.info('process', JSON.stringify(process)) }) }).then((process) => { console.info('finish', JSON.stringify(process)) }) fs.fsyncSync(fd.fd); fs.closeSync(fd); 可以通过setUploadCallback监听传进度,当上传完成时会调用then来通知开发人员已经上传完成 ``` ### 并发 ``` Promise.all([ Post('user/login', (requestBody) => { requestBody.setParam('username', 'jfeiowf2') requestBody.setParam('password', 'abcdefg') }), Post('user/login', (requestBody) => { requestBody.setParam('username', 'jfeiowf') requestBody.setParam('password', 'abcdefg') })]) .then((results) => { this.text = ` result1:${JSON.stringify(results[0])}\n result2:${JSON.stringify(results[1])}\n ` }) ```