# HarmonyOS_http_request获取远程数据 **Repository Path**: ren3016/harmonyos_http_request ## Basic Information - **Project Name**: HarmonyOS_http_request获取远程数据 - **Description**: http.createHttp().request获取远程数据 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-12-06 - **Last Updated**: 2023-12-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: harmony ## README # HarmonyOS_http_request获取远程数据 #### 介绍 httpReq = http.createHttp() httpReq.request获取远程数据 #### 开发平台 Win11 + DevEco Stuido #### 开发步骤 1. 在 DevEco Studio 中 File->New_Create Project,选择 Application->Empty Ability, Next, Compile SDK:3.1.0(API 9), Model: stage 2. 在默认的 Index.ets 文件第一行引入资源: import call from '@ohos.telephony.call'; ``` import http from '@ohos.net.http'; import { http_func } from '../common/http_func'; //(自定义函数) ``` 3. 新建 ets\common\http_func.ets 并写入函数: ``` export function http_func(getJson, add_char:string = '') { var str: string = ''; // console.log(typeof getJson === 'object' ? 'object' : 'other') if (typeof getJson === 'object') { Object.entries(getJson).forEach(([key, value]) => { str += `${add_char}${key}: ${JSON.stringify(value)}\n\n` // console.log(`${add_char}${key}: ${JSON.stringify(value)}`); if (typeof value === 'object') { str += http_func(value, ' ') } }); } else { str += '不是 object: '+getJson + "\n" console.info('不是 object: '+getJson); } return str; } ``` 4. 修改 entry\src\main\resources\base\element\string.json 文件中的 EntryAbility_label 的 value 为 HttpReq。 同时修改和 base 同级的 zh_CN、en_US 下的 string.json 文件中的 EntryAbility_label 的 value 为 HttpReq 5. 使用 USB 链接手机 并打开手机 USB 调试 6. 在 File 中选择 Project Structure -> Signing Configs 选择 Support HarmonyOS 和 Automatically generate signature (需要打开浏览器登录华为帐号并授权) 7. 链接手机成功后, 点击右上角工具栏的真机调试。 8. 手机上自动添加了名为 HttpReq 的应用 #### 主页代码 ``` import http from '@ohos.net.http'; import { http_func } from '../common/http_func' @Entry @Component struct Index { @State strJson: string = '' @State strJson2: string = '' aboutToAppear() { let httpReq = http.createHttp() httpReq.request('https://tool.quhuichang.cn/random_json/', {}, (err, data) => { // console.log(JSON.stringify(data.result)) let getJson = JSON.parse(`${data.result}`) this.strJson = http_func(getJson) } ) httpReq.request('https://tool.quhuichang.cn/random_json/json.php', {}, (err2, data2) => { console.log(JSON.stringify(data2.result)) let getJson2 = JSON.parse(`${data2.result}`) this.strJson2 = http_func(getJson2) } ) } build() { Column() { List() { ListItem() { Column() { Text(this.strJson).width('90%').margin({ top: 25 }) Divider().color('#dddddd').height(30).strokeWidth(10) Text(this.strJson2).width('90%').margin({ bottom: 50 }) } } } } .height('100%') } } ``` #### string.json 配置文件修改 1. entry\src\main\resources\base\element\string.json (默认配置文件) 2. entry\src\main\resources\zh_CN\element\string.json (中文配置文件) 3. entry\src\main\resources\en_US\element\string.json (英文配置文件) ``` { "string": [ { "name": "module_desc", "value": "module description" }, { "name": "EntryAbility_desc", "value": "http_request获取远程数据" }, { "name": "EntryAbility_label", "value": "HttpReq" } ] } ``` #### 相关权限 1. 需要在文件中添加授权:entry\src\main\module.json5 ``` "requestPermissions": [{ "name": "ohos.permission.INTERNET" }] ``` #### 克隆安装 git clone https://gitee.com/ren3016/harmonyos_http_request.git #### 使用说明 打开应用,可以看到获取远程JSON数据后解析出来的值。 #### 约束与限制 1. 本示例仅支持标准系统上运行,支持设备:华为手机或运行在DevEco Studio上的华为手机设备模拟器。 2. 本示例为Stage模型,支持API Version 9。 3. 本示例需要使用DevEco Studio 3.1 Release版本进行编译运行。 #### 页面效果 ![输入图片说明](entry/src/main/resources/rawfile/Sample_image.jpg)