0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

鴻蒙OS開(kāi)發(fā)實(shí)例:【窺探網(wǎng)絡(luò)請(qǐng)求】

jf_46214456 ? 2024-04-01 16:11 ? 次閱讀

HarmonyOS 平臺(tái)中使用網(wǎng)絡(luò)請(qǐng)求,需要引入 "@ohos.net.http", 并且需要在 module.json5 文件中申請(qǐng)網(wǎng)絡(luò)權(quán)限, 即 “ohos.permission.INTERNET”

本篇文章將嘗試使用 @ohos.net.http 來(lái)實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求

場(chǎng)景設(shè)定

  1. WeiBo UniDemo HuaWei : 請(qǐng)求順序
  2. WeiBo1 UniDemo2 HuaWei3 : 異步/同步請(qǐng)求時(shí),序號(hào)表示請(qǐng)求回來(lái)的順序
  3. “開(kāi)始網(wǎng)絡(luò)請(qǐng)求-異步” : 開(kāi)始異步請(qǐng)求
  4. “開(kāi)始網(wǎng)絡(luò)請(qǐng)求-同步” : 開(kāi)始同步請(qǐng)求
  5. “開(kāi)始網(wǎng)絡(luò)請(qǐng)求-自定義方法裝飾器” : 采用自定義方法裝飾器進(jìn)行傳參,進(jìn)而完成網(wǎng)絡(luò)請(qǐng)求

官方網(wǎng)絡(luò)請(qǐng)求案例

注意:

每次請(qǐng)求都必須新創(chuàng)建一個(gè)HTTP請(qǐng)求實(shí)例,即只要發(fā)起請(qǐng)求,必須調(diào)用createHttp方法

更多鴻蒙開(kāi)發(fā)應(yīng)用知識(shí)已更新qr23.cn/AKFP8k參考前往。

搜狗高速瀏覽器截圖20240326151547.png

關(guān)于 @ohos.net.http 有三個(gè)request方法

可+mau123789獲取鴻蒙文檔
  1. request(url: string, callback: AsyncCallback): void;
    1.1 如下“官方指南代碼縮減版”使用到了這個(gè)方法
  2. request(url: string, options: HttpRequestOptions, callback: AsyncCallback): void;
    2.1 如下“官方指南代碼” 使用了這個(gè)方法
  3. request(url: string, options?: HttpRequestOptions): Promise;
    3.1 將在后續(xù)實(shí)踐代碼中使用到
// 引入包名
import http from '@ohos.net.http';

// 每一個(gè)httpRequest對(duì)應(yīng)一個(gè)HTTP請(qǐng)求任務(wù),不可復(fù)用
let httpRequest = http.createHttp();
// 用于訂閱HTTP響應(yīng)頭,此接口會(huì)比request請(qǐng)求先返回??梢愿鶕?jù)業(yè)務(wù)需要訂閱此消息
// 從API 8開(kāi)始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
httpRequest.on('headersReceive', (header) = > {
    console.info('header: ' + JSON.stringify(header));
});
httpRequest.request(
    // 填寫(xiě)HTTP請(qǐng)求的URL地址,可以帶參數(shù)也可以不帶參數(shù)。URL地址需要開(kāi)發(fā)者自定義。請(qǐng)求的參數(shù)可以在extraData中指定
    "EXAMPLE_URL",
    {
        method: http.RequestMethod.POST, // 可選,默認(rèn)為http.RequestMethod.GET
        // 開(kāi)發(fā)者根據(jù)自身業(yè)務(wù)需要添加header字段
        header: {
            'Content-Type': 'application/json'
        },
        // 當(dāng)使用POST請(qǐng)求時(shí)此字段用于傳遞內(nèi)容
        extraData: {
            "data": "data to send",
        },
        expectDataType: http.HttpDataType.STRING, // 可選,指定返回?cái)?shù)據(jù)的類(lèi)型
        usingCache: true, // 可選,默認(rèn)為true
        priority: 1, // 可選,默認(rèn)為1
        connectTimeout: 60000, // 可選,默認(rèn)為60000ms
        readTimeout: 60000, // 可選,默認(rèn)為60000ms
        usingProtocol: http.HttpProtocol.HTTP1_1, // 可選,協(xié)議類(lèi)型默認(rèn)值由系統(tǒng)自動(dòng)指定
    }, (err, data) = > {
        if (!err) {
            // data.result為HTTP響應(yīng)內(nèi)容,可根據(jù)業(yè)務(wù)需要進(jìn)行解析
            console.info('Result:' + JSON.stringify(data.result));
            console.info('code:' + JSON.stringify(data.responseCode));
            // data.header為HTTP響應(yīng)頭,可根據(jù)業(yè)務(wù)需要進(jìn)行解析
            console.info('header:' + JSON.stringify(data.header));
            console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
        } else {
            console.info('error:' + JSON.stringify(err));
            // 取消訂閱HTTP響應(yīng)頭事件
            httpRequest.off('headersReceive');
            // 當(dāng)該請(qǐng)求使用完畢時(shí),調(diào)用destroy方法主動(dòng)銷(xiāo)毀
            httpRequest.destroy();
        }
    }
);
// 引入包名
import http from '@ohos.net.http';

// 每一個(gè)httpRequest對(duì)應(yīng)一個(gè)HTTP請(qǐng)求任務(wù),不可復(fù)用
let httpRequest = http.createHttp();

httpRequest.request(
    // 填寫(xiě)HTTP請(qǐng)求的URL地址,可以帶參數(shù)也可以不帶參數(shù)。URL地址需要開(kāi)發(fā)者自定義。請(qǐng)求的參數(shù)可以在extraData中指定
    "EXAMPLE_URL",
    (err, data) = > {
        if (!err) {
            // data.result為HTTP響應(yīng)內(nèi)容,可根據(jù)業(yè)務(wù)需要進(jìn)行解析
            console.info('Result:' + JSON.stringify(data.result));
            console.info('code:' + JSON.stringify(data.responseCode));
            // data.header為HTTP響應(yīng)頭,可根據(jù)業(yè)務(wù)需要進(jìn)行解析
            console.info('header:' + JSON.stringify(data.header));
            console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
        } else {
            console.info('error:' + JSON.stringify(err));
            // 取消訂閱HTTP響應(yīng)頭事件
            httpRequest.off('headersReceive');
            // 當(dāng)該請(qǐng)求使用完畢時(shí),調(diào)用destroy方法主動(dòng)銷(xiāo)毀
            httpRequest.destroy();
        }
    }
);

場(chǎng)景布局

基礎(chǔ)頁(yè)面組件代碼

考慮到實(shí)際的場(chǎng)景會(huì)用到網(wǎng)絡(luò)請(qǐng)求加載,因此這里將發(fā)揮 [@BuilderParam] 裝飾器作用,先定義基礎(chǔ)頁(yè)面

組件中定義了 @Prop netLoad:boolean 變量來(lái)控制是否展示加載動(dòng)畫(huà)

@Component
export struct BasePage {
  @Prop netLoad: boolean

  //指向一個(gè)組件  
  @BuilderParam aB0: () = > {}

  build(){
     Stack(){
       
       //為組件占位
       this.aB0()

       if (this.netLoad) {
         LoadingProgress()
           .width(px2vp(150))
           .height(px2vp(150))
           .color(Color.Blue)
       }

     }.hitTestBehavior(HitTestMode.None)
  }

}

主頁(yè)面布局代碼

import { BasePage } from './BasePage'

@Entry
@Component
struct NetIndex {
  @State netLoad: number = 0
  @State msg: string = ''

  build() {
      Stack(){
        BasePage({netLoad: this.netLoad != 0}) {
          Column( {space: 20} ){

            Row({space: 20}){
              Text('WeiBo').fontColor(Color.Black)
              Text('UniDemo').fontColor(Color.Black)
              Text('HuaWei').fontColor(Color.Black)
            }

            Row({space: 20}){
              Text('WeiBo' + this.weiboIndex)
              Text('UniDemo' + this.uniIndex)
              Text('HuaWei' + this.huaweiIndex)
            }

            Button('開(kāi)始網(wǎng)絡(luò)請(qǐng)求 - 異步').fontSize(20).onClick( () = > {
                ...
            })

            Button('開(kāi)始網(wǎng)絡(luò)請(qǐng)求 - 同步').fontSize(20).onClick( () = > {
                ...
            })

            Button('開(kāi)始網(wǎng)絡(luò)請(qǐng)求-自定義方法裝飾器').fontSize(20).onClick( () = > {
              ...
            })

            Scroll() {
              Text(this.msg).width('100%')
            }
            .scrollable(ScrollDirection.Vertical)
          }
          .width('100%')
          .height('100%')
          .padding({top: px2vp(120)})
        }

      }
  }

}

簡(jiǎn)單裝封裝網(wǎng)絡(luò)請(qǐng)求

函數(shù)傳參,直接調(diào)用封裝方法

WeiBo為數(shù)據(jù)結(jié)構(gòu)體,暫時(shí)不用關(guān)心,后續(xù)會(huì)貼出完整代碼,這里僅僅是演示網(wǎng)絡(luò)請(qǐng)求用法

//引用封裝好的HNet網(wǎng)絡(luò)工具類(lèi)
import HNet from './util/HNet'

@State msg: string = ''
    
getWeiBoData(){
   HNet.get< WeiBo >({
  url: 'https://m.weibo.cn/api/feed/trendtop?containerid=102803_ctg1_4188_-_ctg1_4188',
}).then( (r) = > {

  this.msg = ''

  if(r.code == 0 && r.result){
    r.result.data.statuses.forEach((value: WeiBoItem) = > {
      this.msg = this.msg.concat(value.created_at + ' ' + value.id + 'n')
    })
  } else {
    this.msg = r.code + ' ' + r.msg
  }
  console.log('順序-weibo-' + (new Date().getTime() - starTime))

  this.netLoad--

})    
   
}

自定義方法裝飾器,完成傳參調(diào)用

網(wǎng)絡(luò)請(qǐng)求樣例

NetController.getWeiBo< WeiBo >().then( r = > {
   ......
})    
復(fù)制

按照業(yè)務(wù)定義傳參

import { Get, NetResponse } from './util/HNet'

export default class BizNetController {

  @Get('https://m.weibo.cn/api/feed/trendtop?containerid=102803_ctg1_4188_-_ctg1_4188')
  static getWeiBo< WeiBo >(): Promise< NetResponse< WeiBo >>{ return }

}
復(fù)制

封裝的網(wǎng)絡(luò)請(qǐng)求代碼

import http from '@ohos.net.http';

//自定義網(wǎng)絡(luò)請(qǐng)求參數(shù)對(duì)象    
class NetParams{
  url: string
  extraData?: JSON
}

//自定義數(shù)據(jù)公共結(jié)構(gòu)體    
export class NetResponse< T > {
  result: T
  code: number
  msg: string
}

//網(wǎng)絡(luò)封裝工具類(lèi)    
class HNet {

  //POST 請(qǐng)求方法  
  static post< T >(options: NetParams): Promise< NetResponse< T >>{
    return this.request(options, http.RequestMethod.POST)
  }

  //GET 請(qǐng)求方法  
  static get< T >(options: NetParams): Promise< NetResponse< T >>{
    return this.request(options, http.RequestMethod.GET)
  }

    
  private static request< T >(options: NetParams, method: http.RequestMethod): Promise< NetResponse< T >>{

    let r = http.createHttp()

    return r.request(options.url, {
      method: method,
      extraData: options.extraData != null ? JSON.stringify(options.extraData) : null
    }).then( (response: http.HttpResponse) = > {

      let netResponse = new NetResponse< T >()

      let dataType = typeof response.result

      if(dataType === 'string'){
         console.log('結(jié)果為字符串類(lèi)型')
      }

      if(response.responseCode == 200){
        netResponse.code = 0
        netResponse.msg = 'success'
        netResponse.result = JSON.parse(response.result as string)

      } else {
        //出錯(cuò)
        netResponse.code = -1
        netResponse.msg = 'error'

      }

      return netResponse

    }).catch( reject = > {
      console.log('結(jié)果發(fā)生錯(cuò)誤')

      let netResponse = new NetResponse< T >()
      netResponse.code = reject.code
      netResponse.msg  = reject.message
      return netResponse

    }).finally( () = > {
       //網(wǎng)絡(luò)請(qǐng)求完成后,需要進(jìn)行銷(xiāo)毀
       r.destroy()
    })

  }

}

export default HNet 
    
//用于裝飾器傳參
export function Get(targetUrl: string) : MethodDecorator {

  return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) = > {
        //替換方法
        descriptor.value = () = > {
           let options = new NetParams()
           options.url = targetUrl
            return HNet.get(options)
        }
  }

}

完整代碼

代碼結(jié)構(gòu)

net/BasePage.ets

net/NetRequest.ets

net/util/HNet.ts

net/viewmodel/WeiBoModel.ts

net/BizNetController.ets

詳細(xì)代碼

@Component
export struct BasePage {
  @Prop netLoad: boolean

  @BuilderParam aB0: () = > {}

  build(){
     Stack(){

       this.aB0()

       if (this.netLoad) {
         LoadingProgress()
           .width(px2vp(150))
           .height(px2vp(150))
           .color(Color.Blue)
       }

     }.hitTestBehavior(HitTestMode.None)
  }

}
import HNet from './util/HNet'
import NetController from './BizNetController'
import { WeiBo, WeiBoItem } from './viewmodel/WeiBoModel'
import { BasePage } from './BasePage'

@Entry
@Component
struct NetIndex {
  @State netLoad: number = 0
  @State msg: string = ''

  @State weiboColor: Color = Color.Black
  @State uniColor: Color = Color.Black
  @State huaweiColor: Color = Color.Black

  @State weiboIndex: number = 1
  @State uniIndex: number = 2
  @State huaweiIndex: number = 3

  private  TEST_Target_URL: string[] = [
    'https://m.weibo.cn/api/feed/trendtop?containerid=102803_ctg1_4188_-_ctg1_4188',
    'https://unidemo.dcloud.net.cn/api/news',
    'https://developer.huawei.com/config/cn/head.json',
  ]

  build() {
      Stack(){
        BasePage({netLoad: this.netLoad != 0}) {
          Column( {space: 20} ){

            Row({space: 20}){
              Text('WeiBo').fontColor(Color.Black)
              Text('UniDemo').fontColor(Color.Black)
              Text('HuaWei').fontColor(Color.Black)
            }

            Row({space: 20}){
              Text('WeiBo' + this.weiboIndex).fontColor(this.weiboColor)
              Text('UniDemo' + this.uniIndex).fontColor(this.uniColor)
              Text('HuaWei' + this.huaweiIndex).fontColor(this.huaweiColor)
            }

            Button('開(kāi)始網(wǎng)絡(luò)請(qǐng)求 - 異步').fontSize(20).onClick( () = > {
                this.weiboColor = Color.Black
                this.uniColor = Color.Black
                this.huaweiColor = Color.Black
                this.weiboIndex = 1
                this.uniIndex = 2
                this.huaweiIndex = 3
                this.asyncGetData()
            })

            Button('開(kāi)始網(wǎng)絡(luò)請(qǐng)求 - 同步').fontSize(20).onClick( () = > {
                this.weiboColor = Color.Black
                this.uniColor = Color.Black
                this.huaweiColor = Color.Black

                this.weiboIndex = 1
                this.uniIndex = 2
                this.huaweiIndex = 3
                this.syncGetData()
            })

            Button('開(kāi)始網(wǎng)絡(luò)請(qǐng)求-自定義方法裝飾器').fontSize(20).onClick( () = > {
              this.getWeiBoListByController()
            })

            Scroll() {
              Text(this.msg).width('100%')
            }
            .scrollable(ScrollDirection.Vertical)
          }
          .width('100%')
          .height('100%')
          .padding({top: px2vp(120)})
        }

      }
  }

  asyncGetData(){

    this.netLoad = 3;

    this.TEST_Target_URL.forEach( (value) = > {

      HNet.get({
        url: value,
      }).then( (r) = > {

        this.msg = JSON.stringify(r)

        if(value.indexOf('weibo') != -1){
          this.weiboColor = Color.Green
          this.weiboIndex = 3 - this.netLoad + 1
        } else if(value.indexOf('unidemo') != -1){
          this.uniColor = Color.Green
          this.uniIndex = 3 - this.netLoad + 1
        } else if(value.indexOf('huawei') != -1){
          this.huaweiColor = Color.Green
          this.huaweiIndex = 3 - this.netLoad + 1
        }

        this.netLoad--

      })
    })

  }

  async syncGetData() {

    let starTime
    let url
    this.netLoad = 3;

    starTime = new Date().getTime()
    url = this.TEST_Target_URL[0]

    starTime = new Date().getTime()
    if(url.indexOf('weibo') != -1){
      console.log('順序-請(qǐng)求-weibo')
    } else if(url.indexOf('unidemo') != -1){
      console.log('順序-請(qǐng)求-unidemo')
    } else if(url.indexOf('huawei') != -1){
      console.log('順序-請(qǐng)求-huawei')
    }

    await HNet.get< WeiBo >({
      url: url,
    }).then( (r) = > {

      this.msg = ''

      if(r.code == 0 && r.result){
        r.result.data.statuses.forEach((value: WeiBoItem) = > {
          this.msg = this.msg.concat(value.created_at + ' ' + value.id + 'n')
        })
      } else {
        this.msg = r.code + ' ' + r.msg
      }

      if(url.indexOf('weibo') != -1){
        this.weiboColor = Color.Green
        this.weiboIndex = 3 - this.netLoad + 1
        console.log('順序-返回-weibo-' + (new Date().getTime() - starTime))
      } else if(url.indexOf('unidemo') != -1){
        this.uniColor = Color.Green
        this.uniIndex = 3 - this.netLoad + 1
        console.log('順序-返回-unidemo-' + (new Date().getTime() - starTime))
      } else if(url.indexOf('huawei') != -1){
        this.huaweiColor = Color.Green
        this.huaweiIndex = 3 - this.netLoad + 1
        console.log('順序-返回-huawei-' + (new Date().getTime() - starTime))
      }

      this.netLoad--

    })

    starTime = new Date().getTime()
    url = this.TEST_Target_URL[1]

    starTime = new Date().getTime()
    if(url.indexOf('weibo') != -1){
      console.log('順序-請(qǐng)求-weibo')
    } else if(url.indexOf('unidemo') != -1){
      console.log('順序-請(qǐng)求-unidemo')
    } else if(url.indexOf('huawei') != -1){
      console.log('順序-請(qǐng)求-huawei')
    }

    await HNet.get({
      url: url,
    }).then( (r) = > {

      this.msg = JSON.stringify(r)

      if(url.indexOf('weibo') != -1){
        this.weiboColor = Color.Green
        this.weiboIndex = 3 - this.netLoad + 1
        console.log('順序-返回-weibo-' + (new Date().getTime() - starTime))
      } else if(url.indexOf('unidemo') != -1){
        this.uniColor = Color.Green
        this.uniIndex = 3 - this.netLoad + 1
        console.log('順序-返回-unidemo-' + (new Date().getTime() - starTime))
      } else if(url.indexOf('huawei') != -1){
        this.huaweiColor = Color.Green
        this.huaweiIndex = 3 - this.netLoad + 1
        console.log('順序-返回-huawei-' + (new Date().getTime() - starTime))
      }

      this.netLoad--

    })

    starTime = new Date().getTime()
    url = this.TEST_Target_URL[2]

    starTime = new Date().getTime()
    if(url.indexOf('weibo') != -1){
      console.log('順序-請(qǐng)求-weibo')
    } else if(url.indexOf('unidemo') != -1){
      console.log('順序-請(qǐng)求-unidemo')
    } else if(url.indexOf('huawei') != -1){
      console.log('順序-請(qǐng)求-huawei')
    }

    await HNet.get({
      url: url,
    }).then( (r) = > {

      this.msg = JSON.stringify(r)

      if(url.indexOf('weibo') != -1){
        this.weiboColor = Color.Green
        this.weiboIndex = 3 - this.netLoad + 1
        console.log('順序-返回-weibo-' + (new Date().getTime() - starTime))
      } else if(url.indexOf('unidemo') != -1){
        this.uniColor = Color.Green
        this.uniIndex = 3 - this.netLoad + 1
        console.log('順序-返回-unidemo-' + (new Date().getTime() - starTime))
      } else if(url.indexOf('huawei') != -1){
        this.huaweiColor = Color.Green
        this.huaweiIndex = 3 - this.netLoad + 1
        console.log('順序-返回-huawei-' + (new Date().getTime() - starTime))
      }

      this.netLoad--

    })

  }

  getHuaWeiSomeDataByNet(){

    this.netLoad = 1

    let starTime = new Date().getTime()

    console.log('順序-huawei-請(qǐng)求' + starTime)

    HNet.get({
      url: 'https://developer.huawei.com/config/cn/head.json',
    }).then( (r) = > {

      this.msg = JSON.stringify(r, null, 't')

      this.netLoad--

      console.log('順序-huawei-' + (new Date().getTime() - starTime))

    })

  }

  getWeiBoListByHNet(){

    this.netLoad = 1

    let starTime = new Date().getTime()

    console.log('順序-weibo-請(qǐng)求' + starTime)

    HNet.get< WeiBo >({
      url: 'https://m.weibo.cn/api/feed/trendtop?containerid=102803_ctg1_4188_-_ctg1_4188',
    }).then( (r) = > {

      this.msg = ''

      if(r.code == 0 && r.result){
        r.result.data.statuses.forEach((value: WeiBoItem) = > {
          this.msg = this.msg.concat(value.created_at + ' ' + value.id + 'n')
        })
      } else {
        this.msg = r.code + ' ' + r.msg
      }
      console.log('順序-weibo-' + (new Date().getTime() - starTime))

      this.netLoad--

    })
  }

  getWeiBoListByController(){

    this.netLoad = 1

    NetController.getWeiBo< WeiBo >().then( r = > {

      this.msg = ''

      if(r.code == 0 && r.result){
        r.result.data.statuses.forEach((value: WeiBoItem) = > {
          this.msg = this.msg.concat(value.created_at + ' ' + value.id + 'n' + value.source + 'n')
        })
      } else {
        this.msg = r.code + ' ' + r.msg
      }

      this.netLoad--

    })
  }

}
import { Get, NetResponse } from './util/HNet'

export default class BizNetController {

  @Get('https://m.weibo.cn/api/feed/trendtop?containerid=102803_ctg1_4188_-_ctg1_4188')
  static getWeiBo< WeiBo >(): Promise< NetResponse< WeiBo >>{ return }

}
import http from '@ohos.net.http';

class NetParams{
  url: string
  extraData?: JSON
}

export class NetResponse< T > {
  result: T
  code: number
  msg: string
}

class HNet {

  static post< T >(options: NetParams): Promise< NetResponse< T >>{
    return this.request(options, http.RequestMethod.POST)
  }

  static get< T >(options: NetParams): Promise< NetResponse< T >>{
    return this.request(options, http.RequestMethod.GET)
  }

  private static request< T >(options: NetParams, method: http.RequestMethod): Promise< NetResponse< T >>{

    let r = http.createHttp()

    return r.request(options.url, {
      method: method,
      extraData: options.extraData != null ? JSON.stringify(options.extraData) : null
    }).then( (response: http.HttpResponse) = > {

      let netResponse = new NetResponse< T >()

      let dataType = typeof response.result

      if(dataType === 'string'){
         console.log('結(jié)果為字符串類(lèi)型')
      }

      if(response.responseCode == 200){
        netResponse.code = 0
        netResponse.msg = 'success'
        netResponse.result = JSON.parse(response.result as string)

      } else {
        //出錯(cuò)
        netResponse.code = -1
        netResponse.msg = 'error'

      }

      return netResponse

    }).catch( reject = > {
      console.log('結(jié)果發(fā)生錯(cuò)誤')

      let netResponse = new NetResponse< T >()
      netResponse.code = reject.code
      netResponse.msg  = reject.message
      return netResponse

    }).finally( () = > {
       r.destroy()
    })

  }

}

export default HNet

export function Get(targetUrl: string) : MethodDecorator {

  return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) = > {
        //替換方法
        descriptor.value = () = > {
           let options = new NetParams()
           options.url = targetUrl
            return HNet.get(options)
        }
  }

}
export class WeiBo{
  ok: number
  http_code: number
  data: WeiBoDataObj
}

export class WeiBoDataObj{
  total_number: number
  interval: number
  remind_text: string
  page: number
  statuses: Array< WeiBoItem >
}

export class WeiBoItem{
  created_at: string
  id: string
  source: string
  textLength: number
}
聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 移動(dòng)開(kāi)發(fā)

    關(guān)注

    0

    文章

    52

    瀏覽量

    10019
  • 鴻蒙
    +關(guān)注

    關(guān)注

    57

    文章

    2457

    瀏覽量

    43452
  • HarmonyOS
    +關(guān)注

    關(guān)注

    79

    文章

    2001

    瀏覽量

    31418
  • OpenHarmony
    +關(guān)注

    關(guān)注

    25

    文章

    3785

    瀏覽量

    17340
  • 鴻蒙OS
    +關(guān)注

    關(guān)注

    0

    文章

    190

    瀏覽量

    4734
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    鴻蒙開(kāi)發(fā)實(shí)戰(zhàn):網(wǎng)絡(luò)請(qǐng)求庫(kù)【axios】

    [Axios]?,是一個(gè)基于 promise 的網(wǎng)絡(luò)請(qǐng)求庫(kù),可以運(yùn)行 node.js 和瀏覽器中。本庫(kù)基于[Axios]原庫(kù)v1.3.4版本進(jìn)行適配,使其可以運(yùn)行在 OpenHarmony,并沿用其現(xiàn)有用法和特性。
    的頭像 發(fā)表于 03-25 16:47 ?4225次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開(kāi)發(fā)</b>實(shí)戰(zhàn):<b class='flag-5'>網(wǎng)絡(luò)</b><b class='flag-5'>請(qǐng)求</b>庫(kù)【axios】

    鴻蒙OS開(kāi)發(fā)實(shí)例:【工具類(lèi)封裝-http請(qǐng)求

    ;@ohos.promptAction';** **封裝HTTP接口請(qǐng)求類(lèi),提供格式化的響應(yīng)信息輸出功能。 使用 DevEco Studio 3.1.1 Release 及以上版本,API 版本為 api 9 及以上
    的頭像 發(fā)表于 03-27 22:32 ?1618次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開(kāi)發(fā)</b><b class='flag-5'>實(shí)例</b>:【工具類(lèi)封裝-http<b class='flag-5'>請(qǐng)求</b>】

    鴻蒙OS應(yīng)用程序開(kāi)發(fā)

    這份學(xué)習(xí)文檔主要是帶領(lǐng)大家在鴻蒙OS上學(xué)習(xí)開(kāi)發(fā)一個(gè)應(yīng)用程序,主要知識(shí)點(diǎn)如下:1、U-Boot引導(dǎo)文件燒寫(xiě)方式;2、內(nèi)核鏡像燒寫(xiě)方式;3、鏡像運(yùn)行。
    發(fā)表于 09-11 14:39

    鴻蒙JS開(kāi)發(fā)接口請(qǐng)求loading怎么解決?

    鴻蒙JS開(kāi)發(fā)接口請(qǐng)求loading?
    發(fā)表于 05-10 10:24

    鴻蒙應(yīng)用開(kāi)發(fā)請(qǐng)求不到數(shù)據(jù)是為什么?

    鴻蒙應(yīng)用開(kāi)發(fā)請(qǐng)求不到數(shù)據(jù)
    發(fā)表于 06-15 11:04

    鴻蒙 OS 應(yīng)用開(kāi)發(fā)初體驗(yàn)

    的操作系統(tǒng)平臺(tái)和開(kāi)發(fā)框架。HarmonyOS 的目標(biāo)是實(shí)現(xiàn)跨設(shè)備的無(wú)縫協(xié)同和高性能。 DevEco Studio 對(duì)標(biāo) Android Studio,開(kāi)發(fā)鴻蒙 OS 應(yīng)用的 IDE。
    發(fā)表于 11-02 19:38

    嵌入式系統(tǒng)設(shè)計(jì)與實(shí)例開(kāi)發(fā)—ARM與uC/OS-Ⅱ

    嵌入式系統(tǒng)設(shè)計(jì)與實(shí)例開(kāi)發(fā) ——ARM與uC/OS-Ⅱ
    發(fā)表于 11-08 17:32 ?0次下載

    華為鴻蒙OS 2.0帶來(lái)哪些智慧體驗(yàn)?

    華為已經(jīng)定于12月16日在北京發(fā)布鴻蒙OS 2.0手機(jī)開(kāi)發(fā)者Beta版本。這不僅是手機(jī)鴻蒙OS的首次亮相,同時(shí)也意味著手機(jī)
    的頭像 發(fā)表于 12-15 15:10 ?2172次閱讀

    鴻蒙OS 2.0手機(jī)開(kāi)發(fā)者Beta版發(fā)布會(huì)在京舉辦

    三個(gè)月前,鴻蒙OS 2.0正式在華為開(kāi)發(fā)者大會(huì)2020亮相。12月16日,鴻蒙OS 2.0手機(jī)開(kāi)發(fā)
    的頭像 發(fā)表于 12-16 09:29 ?1.9w次閱讀

    華為發(fā)布鴻蒙OS Beta版

    昨天華為發(fā)布鴻蒙OS Beta版了?鴻蒙系統(tǒng)一直在按照既有步伐前進(jìn),現(xiàn)在華為發(fā)布鴻蒙OS Beta版,而且一些生態(tài)
    的頭像 發(fā)表于 12-17 08:41 ?3004次閱讀

    鴻蒙OS與Lite OS的區(qū)別是什么

    鴻蒙OS鴻蒙OS面向未來(lái)、面向全場(chǎng)景、分布式。在單設(shè)備系統(tǒng)能力基礎(chǔ)上,鴻蒙OS提出了基于同一套系
    的頭像 發(fā)表于 12-24 12:40 ?5224次閱讀

    鴻蒙os怎么升級(jí)

    6月2日,華為正式發(fā)布了鴻蒙armonyOS 2系統(tǒng),那么鴻蒙os如何升級(jí)?現(xiàn)將鴻蒙os升級(jí)方式告知如下。
    的頭像 發(fā)表于 06-08 16:26 ?2951次閱讀

    華為開(kāi)發(fā)者大會(huì)2021鴻蒙os在哪場(chǎng)

    華為開(kāi)發(fā)者大會(huì)2021將在10月22日-24日舉辦,地點(diǎn)為東莞松山湖,鴻蒙os 3.0或?qū)⑴c我們見(jiàn)面,那么華為開(kāi)發(fā)者大會(huì)2021鴻蒙
    的頭像 發(fā)表于 10-22 15:24 ?2027次閱讀

    鴻蒙OS開(kāi)發(fā)實(shí)戰(zhàn):【網(wǎng)絡(luò)管理HTTP數(shù)據(jù)請(qǐng)求

    應(yīng)用通過(guò)HTTP發(fā)起一個(gè)數(shù)據(jù)請(qǐng)求,支持常見(jiàn)的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。
    的頭像 發(fā)表于 04-01 16:31 ?904次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開(kāi)發(fā)</b>實(shí)戰(zhàn):【<b class='flag-5'>網(wǎng)絡(luò)</b>管理HTTP數(shù)據(jù)<b class='flag-5'>請(qǐng)求</b>】

    鴻蒙OS開(kāi)發(fā)實(shí)例:【HarmonyHttpClient】網(wǎng)絡(luò)框架

    鴻蒙上使用的Http網(wǎng)絡(luò)框架,里面包含純Java實(shí)現(xiàn)的HttpNet,類(lèi)似okhttp使用,支持同步和異步兩種請(qǐng)求方式;還有鴻蒙版retrofit,和Android版Retrofit相
    的頭像 發(fā)表于 04-12 16:58 ?1004次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開(kāi)發(fā)</b><b class='flag-5'>實(shí)例</b>:【HarmonyHttpClient】<b class='flag-5'>網(wǎng)絡(luò)</b>框架

    電子發(fā)燒友

    中國(guó)電子工程師最喜歡的網(wǎng)站

    • 2931785位工程師會(huì)員交流學(xué)習(xí)
    • 獲取您個(gè)性化的科技前沿技術(shù)信息
    • 參加活動(dòng)獲取豐厚的禮品