【HarmonyOS开发】ArkTs使用Http封装

1、鸿蒙中如何进行网络请求

1.1 三方库请求

1.2 鸿蒙原生请求

2、ArkTs请求模块@ohos.net.http

本模块提供HTTP数据请求能力。应用可以通过HTTP发起一个数据请求,支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。

3、@ohos.net.http请求流程

  1. http.createHttp(创建请求实例任务);

  2. request(请求);

  3. destroy(中断请求);

  4. on(订阅HTTP Response Header 事件);

  5. off(取消订阅HTTP Response Header 事件);

  6. once(订阅HTTP Response Header 事件,但是只触发一次);

4、预览效果

5、封装@ohos.net.http

5.1 函数式

5.1.1 封装

TypeScript 复制代码
import http from '@ohos.net.http';

export interface httpOptions {
  timeOut?: number;
  ContentType?: string;
  header?: Object;
  state?: string;
  callBack?: Function;
}

const HTTP_READ_TIMEOUT = 60000;
const HTTP_CONNECT_TIMEOUT = 60000;
const CONTENT_TYPE = 'application/json'

export function httpRequest(
    url: string,
    method: http.RequestMethod = http.RequestMethod.GET,
    params?: string | Object | ArrayBuffer,
    options?: httpOptions
): Promise<ResponseResult> {
  const request = http.createHttp();
  // 处理状态
  if(options?.state) {
    switch (options.state) {
      case 'on':
        request.on('headersReceive', (header) => options.callBack(header));
        break;
      case 'once':
        request.on('headersReceive', (header) => options.callBack(header));
        break;
      case 'off':
        request.off('headersReceive');
        break;
      case 'destroy':
        request.destroy();
        break;
      default:
        break;
    }
    return;
  }

  // 处理请求
  const responseResult = request.request(url, {
    // 超时时间
    readTimeout: options?.timeOut || HTTP_READ_TIMEOUT,
    connectTimeout: options?.timeOut || HTTP_CONNECT_TIMEOUT,
    method,
    extraData: params || {},
    header: options?.header || {
      'Content-Type': options?.ContentType || CONTENT_TYPE
    },
  });
  let serverData: ResponseResult = new ResponseResult();
  // Processes the data and returns.
  return responseResult.then((data: http.HttpResponse) => {
    if (data.responseCode === http.ResponseCode.OK) {
      // Obtains the returned data.
      let result = `${data.result}`;
      let resultJson: ResponseResult = JSON.parse(result);

      serverData.data = resultJson;
      serverData.code = 'success';
      serverData.msg = resultJson?.msg;
    } else {
      serverData.msg = `error info & ${data.responseCode}`;
    }
    return serverData;
  }).catch((err) => {
    serverData.msg = `${err}`;
    return serverData;
  })
}

export class ResponseResult {
  /**
   * Code returned by the network request: success, fail.
   */
  code: string;

  /**
   * Message returned by the network request.
   */
  msg: string | Resource;

  /**
   * Data returned by the network request.
   */
  data: string | Object | ArrayBuffer;

  constructor() {
    this.code = '';
    this.msg = '';
    this.data = '';
  }
}

export default httpRequest;

5.1.2 使用

TypeScript 复制代码
import httpRequest from '../../utils/requestHttp';

interface resultType {
  resultcode: string;
  reason: string;
  result: resultValType | null;
  error_code: string;
}

interface resultValType {
  city: string;
  realtime: realtimeType;
  future: Object;
}

interface realtimeType {
  temperature: string;
  humidity: string;
  info: string;
  wid: string;
  direct: string;
  power: string;
  aqi: string;
}

@Extend(Text) function textStyle() {
  .fontColor(Color.White)
  .margin({
    left: 12
  })
}

@Entry
@Component
struct httpPages {
  @State html: resultValType = {
    city: '',
    realtime: {
      temperature: '',
      humidity: '',
      info: '',
      wid: '',
      direct: '',
      power: '',
      aqi: '',
    },
    future: undefined
  };
  @State url: string = "http://apis.juhe.cn/simpleWeather/query?key=397c9db4cb0621ad0313123dab416668&city=西安";

  @Styles weatherStyle() {
    .width('100%')
    .padding(6)
    .backgroundColor(Color.Green)
    .borderRadius(8)
  }

  build() {
    Column({space: 10}) {
      Button("请求数据")
        .onClick(() => {
          this.httpRequest();
        })
      Column() {
        Text(this.html.city || '--').textStyle().fontWeight(FontWeight.Bold)
      }.weatherStyle().alignItems(HorizontalAlign.Start)
      Column() {
        Text(this.html.realtime.temperature || '--').textStyle()
      }.weatherStyle().alignItems(HorizontalAlign.Start)
      Column() {
        Text(this.html.realtime.humidity || '--').textStyle()
      }.weatherStyle().alignItems(HorizontalAlign.Start)
      Column() {
        Text(this.html.realtime.info || '--').textStyle()
      }.weatherStyle().alignItems(HorizontalAlign.Start)
      Column() {
        Text(this.html.realtime.wid || '--').textStyle()
      }.weatherStyle().alignItems(HorizontalAlign.Start)
      Column() {
        Text(this.html.realtime.direct || '--').textStyle()
      }.weatherStyle().alignItems(HorizontalAlign.Start)
      Column() {
        Text(this.html.realtime.power || '--').textStyle()
      }.weatherStyle().alignItems(HorizontalAlign.Start)
      Column() {
        Text(this.html.realtime.aqi || '--').textStyle()
      }.weatherStyle().alignItems(HorizontalAlign.Start)
    }
    .width('100%')
    .height('100%')
    .padding(10)
  }

  private httpRequest() {
    httpRequest(this.url).then(res => {
      const data: resultType = res.data as resultType;
      this.html = data.result;
      console.info('网络结果:'+JSON.stringify(data));
    });
  }
}

5.2 泛型式

5.2.1 封装

TypeScript 复制代码
import http from '@ohos.net.http';

// 1、创建RequestOption.ets 配置类
export interface RequestOptions {
  url?: string;
  method?: RequestMethod; // default is GET
  queryParams ?: Record<string, string>;
  extraData?: string | Object | ArrayBuffer;
  header?: Object; // default is 'content-type': 'application/json'
}

export enum RequestMethod {
  OPTIONS = "OPTIONS",
  GET = "GET",
  HEAD = "HEAD",
  POST = "POST",
  PUT = "PUT",
  DELETE = "DELETE",
  TRACE = "TRACE",
  CONNECT = "CONNECT"
}

/**
 * Http请求器
 */
export class HttpCore {
  /**
   * 发送请求
   * @param requestOption
   * @returns Promise
   */
  request<T>(requestOption: RequestOptions): Promise<T> {
    return new Promise<T>((resolve, reject) => {
      this.sendRequest(requestOption)
        .then((response) => {
          if (typeof response.result !== 'string') {
            reject(new Error('Invalid data type'));

          } else {
            let bean: T = JSON.parse(response.result);
            if (bean) {
              resolve(bean);
            } else {
              reject(new Error('Invalid data type,JSON to T failed'));
            }

          }
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  private sendRequest(requestOption: RequestOptions): Promise<http.HttpResponse> {
    // 每一个httpRequest对应一个HTTP请求任务,不可复用
    let httpRequest = http.createHttp();

    let resolveFunction, rejectFunction;
    const resultPromise = new Promise<http.HttpResponse>((resolve, reject) => {
      resolveFunction = resolve;
      rejectFunction = reject;
    });

    if (!this.isValidUrl(requestOption.url)) {
      return Promise.reject(new Error('url格式不合法.'));
    }

    let promise = httpRequest.request(this.appendQueryParams(requestOption.url, requestOption.queryParams), {
      method: requestOption.method,
      header: requestOption.header,
      extraData: requestOption.extraData, // 当使用POST请求时此字段用于传递内容
      expectDataType: http.HttpDataType.STRING // 可选,指定返回数据的类型
    });

    promise.then((response) => {
      console.info('Result:' + response.result);
      console.info('code:' + response.responseCode);
      console.info('header:' + JSON.stringify(response.header));

      if (http.ResponseCode.OK !== response.responseCode) {
        throw new Error('http responseCode !=200');
      }
      resolveFunction(response);

    }).catch((err) => {
      rejectFunction(err);
    }).finally(() => {
      // 当该请求使用完毕时,调用destroy方法主动销毁。
      httpRequest.destroy();
    })
    return resultPromise;
  }


  private appendQueryParams(url: string, queryParams: Record<string, string>): string {
    // todo 使用将参数拼接到url上
    return url;
  }

  private isValidUrl(url: string): boolean {
    //todo 实现URL格式判断
    return true;
  }
}

// 实例化请求器
const httpCore = new HttpCore();


export class HttpManager {
  private static mInstance: HttpManager;

  // 防止实例化
  private constructor() {
  }

  static getInstance(): HttpManager {
    if (!HttpManager.mInstance) {
      HttpManager.mInstance = new HttpManager();
    }
    return HttpManager.mInstance;
  }

  request<T>(option: RequestOptions): Promise<T> {
    return httpCore.request(option);
  }
}

export default HttpManager;

5.2.2 使用

TypeScript 复制代码
import httpManager, { RequestMethod } from '../../utils/requestManager';

interface TestBean {
  userId: number,
  id: number,
  title: string,
  completed: boolean
}

private handleClick() {
    httpManager.getInstance()
      .request<TestBean>({
        method: RequestMethod.GET,
        url: 'https://jsonplaceholder.typicode.com/todos/1' //公开的API
      })
      .then((result) => {
        this.text = JSON.stringify(result);
        console.info(JSON.stringify(result));
      })
      .catch((err) => {
        console.error(JSON.stringify(err));
      });
  }
相关推荐
火狮7 分钟前
鸿蒙Next API 12开发,使用@ohos/axios进行HTTP请求
http·华为·harmonyos
m0_7482382743 分钟前
WebClient HTTP 请求问题处理模板(泛型响应、忽略 SSL 证书等)
网络协议·http·ssl
我曾经是个程序员1 小时前
鸿蒙学习记录之http网络请求
服务器·学习·http
SuperHeroWu72 小时前
【HarmonyOS】HarmonyOS和React Native混合开发 (一)之环境安装
react native·harmonyos·鸿蒙·开发环境·环境安装·rn·混合开发
轻口味2 小时前
【每日学点鸿蒙知识】AVCodec、SmartPerf工具、web组件加载、监听键盘的显示隐藏、Asset Store Kit
前端·华为·harmonyos
无处安放的波澜2 小时前
【HarmonyOS 5.0】第十二篇-ArkUI公共属性(一)
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
李洋-蛟龙腾飞公司3 小时前
HarmonyOS Next 应用元服务开发-分布式数据对象迁移数据文件资产迁移
分布式·华为·harmonyos
大土豆的bug记录3 小时前
鸿蒙历史搜索功能:tag标签根据文字宽度自动换行 展示更多
华为·harmonyos
轻口味4 小时前
【每日学点鸿蒙知识】Charles抓包、lock文件处理、WebView组件、NFC相关、CallMethod失败等
华为·harmonyos
凯子坚持 c4 小时前
编程新选择:深入了解仓颉语言的优雅与高效
华为