HarmonyOS鸿蒙Next中使用http接口报'createHttp' of undefined找不到什么原因

HarmonyOS鸿蒙Next中使用http接口报’createHttp’ of undefined找不到什么原因

// @ts-nocheck
import app from '@system.app';
import video from '@system.video';
import request from '@system.request';
import http from '@ohos.net.http';
import router from '@system.router';

export default {
    data: {
        title: "测试",
        url: 'https://haierh5.123bingo.cn/ceshi.mp4',
    },
    onInit() {
        console.info('dsadad')
        // video.src ='/video/ceshi.mp4';
        // video.play();
        let url="https://zzrv5.github.io/test/zzr.html";
        this.getHttp()

    },
    getHttp(){
        // 每一个httpRequest对应一个http请求任务,不可复用
        let httpRequest = http.createHttp();
        // 用于订阅http响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
        // 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback). 8+
        httpRequest.on('headersReceive', (header) => {
            console.info('header: ' + JSON.stringify(header));
        });
        httpRequest.request(
        // 填写http请求的url地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
            "EXAMPLE_URL",
            {
                method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json'
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: {
                    "data": "data to send",
                },
                connectTimeout: 60000, // 可选,默认为60s
                readTimeout: 60000, // 可选,默认为60s
            }, (err, data) => {
            if (!err) {
                // data.result为http响应内容,可根据业务需要进行解析
                console.info('Result:' + data.result);
                console.info('code:' + data.responseCode);
                // data.header为http响应头,可根据业务需要进行解析
                console.info('header:' + JSON.stringify(data.header));
                console.info('cookies:' + data.cookies); // 8+
            } else {
                console.info('error:' + JSON.stringify(err));
                // 当该请求使用完毕时,调用destroy方法主动销毁。
                httpRequest.destroy();
            }
        }
        );
    },
}

更多关于HarmonyOS鸿蒙Next中使用http接口报'createHttp' of undefined找不到什么原因的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

提供一下DevEco Studio和SDK的版本!

更多关于HarmonyOS鸿蒙Next中使用http接口报'createHttp' of undefined找不到什么原因的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中使用createHttp接口报错'createHttp' of undefined,通常是因为API未正确导入或未在ohos.net.http模块中找到。鸿蒙Next的http模块提供了createHttp方法,用于创建HTTP请求实例。如果报错,可能是以下原因:

  • 模块未正确导入:确保在代码中正确导入了ohos.net.http模块。例如:

    import http from '[@ohos](/user/ohos).net.http';
    
  • API版本不匹配:检查你的开发环境与鸿蒙Next的API版本是否匹配。不同版本的SDK可能存在API差异。

  • 设备或模拟器未支持:确保你的设备或模拟器支持该API。某些API可能仅在特定设备或系统版本上可用。

  • 权限未配置:使用HTTP请求需要在config.json中配置网络权限。例如:

    {
      "module": {
        "requestPermissions": [
          {
            "name": "ohos.permission.INTERNET"
          }
        ]
      }
    }
    
  • 代码调用错误:确保在调用createHttp时,方法名拼写正确且调用方式符合规范。例如:

    let httpRequest = http.createHttp();
    

如果上述步骤无误,建议检查开发环境配置及设备/模拟器状态。

在HarmonyOS鸿蒙Next中,如果遇到'createHttp' of undefined的错误,通常是因为未正确导入或初始化HTTP模块。请确保以下几点:

  1. 在代码中正确导入@ohos.net.http模块。

  2. 使用http.createHttp()方法前,确保http对象已正确初始化。

  3. 检查开发环境是否支持该API,确保SDK版本与API兼容。

  4. 如果问题依旧,尝试清理项目并重新构建,或查阅官方文档确认API使用方式。

回到顶部