当前位置: 首页 > news >正文

遵义网站建设公司价格湘潭seo磐石网络

遵义网站建设公司价格,湘潭seo磐石网络,公司网站开发费用,wordpress如何删除已安装主题文章目录 基本使用封装参考 基本使用 鸿蒙应用发起HTTP请求的基本使用#xff0c;如下#xff1a; 导入http模块创建httpRequest对象发起http请求#xff0c;并处理响应结果 第一、导入http模块#xff1a; import http from ohos.net.http第二、创建httpRequest对象如下 导入http模块创建httpRequest对象发起http请求并处理响应结果 第一、导入http模块 import http from ohos.net.http第二、创建httpRequest对象注意的是每一个httpRequest对象对应一个http请求任务不可复用。 const httpRequest http.createHttp()第三、发起请求比如POST请求 httpRequest.request(// 请求url地址url,{// 请求方式method: http.RequestMethod.POST,// 请求的额外数据。extraData: {param1: value1,param2: value2,},// 可选默认为60sconnectTimeout: 60000,// 可选默认为60sreadTimeout: 60000,// 开发者根据自身业务需要添加header字段header: {Content-Type: application/json}}).then((data) { if (data.responseCode http.ResponseCode.OK) {// 处理响应结果// data.result为服务器返回的业务数据console.info(Result: data.result);console.info(code: data.responseCode);} }).catch((err) {console.info(error: JSON.stringify(err)); });最后需要声明网络权限在module.josn5文件中声明 {module : {requestPermissions:[{name: ohos.permission.INTERNET}]} }上面就是网络请求的简单使用接下来通过Promise来封装一个网络请求库统一管理请求参数、响应数据、日志的输出等对外屏蔽了细节使用者只需定义业务数据的实体类以及调用即可。 封装 以**玩Android**开放接口为测试用例 定义业务数据的实体类通过泛型来接收不同的数据类型 export class ResponseResultT {errorCode: number;errorMsg: string;data?: T | Object | string; }把各种请求方式用枚举声明RequestMethod export enum RequestMethod {OPTIONS,GET,HEAD,POST ,PUT,DELETE,TRACE,CONNECT }其实在http模块中已经有对应的枚举之所以再用一个新枚举来声明是简化使用同时也是将http模块相关细节屏蔽掉不对外开放这样可以灵活替换网络库。 定义一个HttpUtils类实现 const TAG HttpUtils const BASE_URL https://www.wanandroid.com export class HttpUtils{public static readonly SUCCESS_CODE: number 0public static readonly READ_TIME_OUT 60 * 1000public static readonly CONNECT_TIME_OUT 60 * 1000private baseUrl: string constructor(baseUrl: string) {this.baseUrl baseUrl}private methodName(method: RequestMethod): http.RequestMethod {switch (method){case RequestMethod.OPTIONS:{return http.RequestMethod.OPTIONS}case RequestMethod.GET:{return http.RequestMethod.GET}case RequestMethod.HEAD:{return http.RequestMethod.HEAD}case RequestMethod.POST:{return http.RequestMethod.POST}case RequestMethod.PUT:{return http.RequestMethod.PUT}case RequestMethod.DELETE:{return http.RequestMethod.DELETE}case RequestMethod.TRACE:{return http.RequestMethod.TRACE}case RequestMethod.CONNECT:{return http.RequestMethod.CONNECT}}}requestT(path: string, reqMethod: RequestMethod, parameter: Mapstring, Object null): PromiseT | null {// 注意的是每一个httpRequest对象对应一个http请求任务不可复用。const httpRequest http.createHttp()const method this.methodName(reqMethod)let extraData {}let url ${this.baseUrl}/${path}if (parameter ! null) {switch (reqMethod) {case RequestMethod.POST: {extraData Object.fromEntries(parameter)break;}case RequestMethod.GET: {const urlParams Object.keys(parameter).map(key ${key}${parameter[key]}).join()if (url.includes(?)) {url ${url}${urlParams}} else {url ${url}?${urlParams}}break;}}}LogUtils.debug(TAG, Request)LogUtils.debug(TAG, url: url)LogUtils.debug(TAG, method: method.toString())if (reqMethod RequestMethod.POST)LogUtils.debug(TAG, extraData: JSON.stringify(parameter, null, 2))return new Promise((resolve, reject) {httpRequest.request(url,{method,readTimeout: HttpUtils.READ_TIME_OUT,connectTimeout: HttpUtils.CONNECT_TIME_OUT,header: {Content-Type: application/json},extraData}).then((value) {LogUtils.debug(TAG, Response)LogUtils.debug(TAG, url: url)LogUtils.debug(TAG, method: method.toString())LogUtils.debug(TAG, header: JSON.stringify(value.header, null, 2))LogUtils.debug(TAG, responseCode: value.responseCode)LogUtils.debug(TAG, resultType: value.resultType)if (value.responseCode http.ResponseCode.OK) {let result: ResponseResultT JSON.parse(value.result.toString())LogUtils.debug(TAG, body: JSON.stringify(result, null, 2))if (result.errorCode HttpUtils.SUCCESS_CODE) {resolve(result.data as T)} else {reject(result.errorMsg)}} else {reject(请求失败)}}).catch((reason) {reject(reason)})})}getT(path: string, parameter: Mapstring, Object null): PromiseT | null {return this.requestT(path, RequestMethod.GET, parameter)}postT(path: string, parameter: Mapstring, Object null): PromiseT | null {return this.requestT(path, RequestMethod.POST, parameter)}deleteT(path: string, parameter: Mapstring, Object null): PromiseT | null {return this.requestT(path, RequestMethod.DELETE, parameter)}putT(path: string, parameter: Mapstring, Object null): PromiseT | null {return this.requestT(path, RequestMethod.PUT, parameter)}} const YiNet new HttpUtils(BASE_URL) export default YiNet使用发起网络请求 aboutToAppear() {let map new Mapstring,string()map[cid] 294YiNet.getArticleList(project/list/1/json,map).then((data){this.data JSON.stringify(data, null, 2)})let map2 new Mapstring,string()map2[username] 123map2[password] 123456YiNet.postUser(user/login,map2).then((data){this.data JSON.stringify(data, null, 2)}).catch((err){Prompt.showToast({message:err})})}日志输出 参考 https://developer.huawei.com/consumer/cn/training/course/slightMooc/C101667364948559963?ha_linkereyJ0cyI6MTcwMjE3NzI3OTYyMywiaWQiOiI4MmM3ZTI1MmFmMDJlMDZiODBmOGU1ZDM5ZTI5YmMyOCJ9https://www.wanandroid.com/blog/show/2
http://www.sadfv.cn/news/164220/

相关文章:

  • 网站制作策划方案wordpress标题后乱码
  • 淘宝运营培训电商seo推广
  • ps下一页哪里搜索引擎优化好
  • 安卓手机网站开发国外木屋建设网站
  • 公司网站是用什么软件做flash里面如何做网站链接
  • 门禁考勤网站建设建设网站教程视频视频视频
  • 企业网站源码怎么获取卓智网络科技有限公司
  • 莱州做网站的公司erp软件是干嘛的
  • 当地信息网站建设资质淮安市住房和城乡建设局网站首页
  • 什么是做网站金属行业网站模板下载
  • 手机端网站需要多少钱wordpress菜单默认对游客不显示
  • 江苏建设服务信息网站做网站租服务器
  • 潮州网络推广公司东营网站搜索引擎优化
  • 设计说明书模板seo报价单
  • 制作外贸网站模板网站制作过程流程
  • 涪陵网站设计统计站老站长推荐app视频
  • 营销型网站的缺点廊坊网站建设模板
  • 做网站有名的公司百度云搜索引擎入口官方
  • 高端品牌网站建设服务网站服务器平台
  • 做网站栏目是什么意思做的网站如何发布会
  • 遂宁模板建站公司工程承包商赚钱吗
  • 用asp.net 做网站电子商务实验网站建设实训过程
  • 网站title keywords现在花钱做那个网站好呀
  • 珠海科技网站建设电子商务和网络营销哪个好
  • 网站域名在哪备案网站开发包括哪些
  • 做ppt时网站怎么设计直播带货平台
  • 网站域名空间5个G的多少钱h5做的公司网站
  • 建设银行网站201308网站设计的设计方案
  • 哪个网站做视频赚钱南通制作公司网站
  • 网站支付怎么做的灰色词排名接单