Harmony网络请求工具类

使用的网络请求框架是axios

1、安装axios

ohpm install @ohos/axios

2、封装

import axios, { FormData } from "@ohos/axios"
import fs from '@ohos.file.fs'
import ArrayList from '@ohos.util.ArrayList'

/**
 * 网络请求工具类
 */
class HttpManager {

  baseUrl:string

  /**
   * get请求
   * @param url
   * @param param
   * @returns
   */
  get(url: string, param: object) {
    this.init()
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params: param
      }).then(function (response) {
        if (response.data == 200) {
          resolve(response.data)
        } else {
          reject(response.data)
        }
      }).catch(function (error) {
        reject(error.data)
      })
    })
  }

  /**
   * post请求
   * @param url
   * @param params
   * @returns
   */
  post(url: string, params: object) {
    this.in

你可能感兴趣的:(鸿蒙,Harmony网络请求工具类,Harmony,网络请求)