当前位置:   article > 正文

07鸿蒙【网络连接】_鸿蒙websocket

鸿蒙websocket


网络连接

  • Http数据请求
  • 第三方库axios

HTTP数据请求:通过HTTP发起一个数据请求。
WebSocket连接:使用WebSocket建立服务器与客户端的双向连接。
Socket连接: 通过Socket进行数据传输。

源码https://gitee.com/cheng_yong_xu/hongmeng-os

Http数据请求

在这里插入图片描述

在这里插入图片描述

访问http://localhost:3000/shops
在这里插入图片描述
看到上面,即使服务器开启,没有问题

在这里插入图片描述

在这里插入图片描述
我们需要使用Promise
在这里插入图片描述

第三方库axios

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
下面是配置环境变量
在这里插入图片描述
在这里插入图片描述
直接在命令行就可以运行了
在这里插入图片描述

在这里插入图片描述
OpenHarmony三方库中心仓https://ohpm.openharmony.cn/#/cn/home

在这里插入图片描述

在这里插入图片描述

// 安装axios
ohpm install @ohos/axios
  • 1
  • 2

在这里插入图片描述

// entry/src/main/ets/model/ShopModel.ts
import http from '@ohos.net.http';
import { Axis } from '@ohos.multimodalInput.mouseEvent';
import ShopInfo from '../viewmodel/ShopInfo';
import axios from '@ohos/axios'

class ShopModel {
  baseURL: string = 'http://localhost:3000'
  pageNo: number = 1

  /**
   * 基于axios实现异步查询商铺
   * @returns
   */

  getShopListByAxios(): Promise<ShopInfo[]> {
    return new Promise((resolve, reject) => {
      axios.get(
        `${this.baseURL}/shops`,
        {
          params: { pageNo: this.pageNo, pageSize: 3 }
        }
      )
        .then(resp => {
          if(resp.status === 200) {
            // 查询成功
            console.log('testTag',  '查询商铺成功!', JSON.stringify(resp.data))
            resolve(resp.data)
          } else {
            console.log('testTag',  '查询商铺信息失败!error:', JSON.stringify(resp))
            reject('查询商铺失败')
          }
        })
        .catch(error => {
          console.log('testTag',  '查询商铺信息失败!error:', JSON.stringify(error))
          reject('查询商铺失败')
        })
    })
  }


  /**
   * 基于ohos的http模块实现异步查询商铺
   * @returns
   */
  getShopListByHttp(): Promise<ShopInfo[]> {
    return new Promise((resolve, reject) => {
      // 1.创建http请求对象
      let httpRequest = http.createHttp()
      // 2.发送请求
      httpRequest.request(
        `${this.baseURL}/shops?pageNo=${this.pageNo}&pageSize=3`,
        {
          method: http.RequestMethod.GET
        }
      )
        .then(resp => {
          if(resp.responseCode === 200) {
            // 查询成功
            console.log('testTag',  '查询商铺成功!', resp.result)
            resolve(JSON.parse(resp.result.toString()))
          }else {
            console.log('testTag',  '查询商铺信息失败!error:', JSON.stringify(resp))
           reject('查询商铺失败')
          }
        })
        .catch(error => {
          console.log('testTag',  '查询商铺信息失败!error:', JSON.stringify(error))
          reject('查询商铺失败')
        })
    })
  }


  /**
   * 基于axios实现同步查询商铺
   * @returns
   */
  async getShopListByAxiosAsync(): Promise<ShopInfo[]> {
    // 1.发送请求\
    let resp = await axios.get(
      `${this.baseURL}/shops`,
      {
        params: { pageNo: this.pageNo, pageSize: 3 }
      }
    )
    // 2.处理响应
    if (resp.status === 200) {
      // 查询成功
      console.log('testTag', '查询商铺成功!', JSON.stringify(resp.data))
      return resp.data;
    }
    // 查询失败
    console.log('testTag', '查询商铺信息失败!error:', JSON.stringify(resp))

  }

  /**
   * 基于ohos的http模块实现同步查询商铺
   * @returns
   */

  async getShopListByHttpAsync(): Promise<ShopInfo[]> {
    // 1.创建http的请求对象
    let httpRequest = http.createHttp()
    // 2.发送请求
    let resp = await httpRequest.request(
      `${this.baseURL}/shops?pageNo=${this.pageNo}&pageSize=3`,
      {
        method: http.RequestMethod.GET
      }
    )
    // 3.处理响应
    if (resp.responseCode === 200) {
      // 查询成功
      console.log('testTag', '查询商铺成功!', resp.result)
      return JSON.parse(resp.result.toString());
    }
    console.log('testTag', '查询商铺信息失败!error:', JSON.stringify(resp))

  }

}

const shopModel = new ShopModel();
export default shopModel as ShopModel;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/963979
推荐阅读
相关标签
  

闽ICP备14008679号