自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從微信小程序到鴻蒙JS開發(fā)【03】-fetch獲取數(shù)據(jù)&簡(jiǎn)單天氣預(yù)報(bào)

開發(fā)
文章由鴻蒙社區(qū)產(chǎn)出,想要了解更多內(nèi)容請(qǐng)前往:51CTO和華為官方戰(zhàn)略合作共建的鴻蒙技術(shù)社區(qū)https://harmonyos.51cto.com/#zz

[[381065]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

在微信小程序中,若需要向遠(yuǎn)程服務(wù)器請(qǐng)求數(shù)據(jù),使用wx.request接口即可。


那么在鴻蒙js開發(fā)中,請(qǐng)求遠(yuǎn)程服務(wù)器需要如下幾步:

1、在config.json配置網(wǎng)絡(luò)權(quán)限和信任域名。

網(wǎng)絡(luò)權(quán)限的配置是在module.reqPermissions中,配置以下三個(gè)權(quán)限。工具有提示,還是比較友好的。

  1.   "module": { 
  2.     "reqPermissions": [ 
  3.       { 
  4.         "name""ohos.permission.GET_NETWORK_INFO" 
  5.       }, 
  6.       { 
  7.         "name""ohos.permission.SET_NETWORK_INFO" 
  8.       }, 
  9.       { 
  10.         "name""ohos.permission.INTERNET" 
  11.       } 
  12.     ], 
  13. ...... 

 信任域名的配置是在deviceConfig中,默認(rèn)是一個(gè)空對(duì)象,需配置成如下形式。

  1. "deviceConfig": { 
  2.   "default": { 
  3.     "network": { 
  4.       "usesCleartext"true
  5.       "securityConfig": { 
  6.         "domainSettings": { 
  7.           "cleartextPermitted"true
  8.           "domains": [ 
  9.             { 
  10.               "subdomains"true
  11.               "name""apis.juhe.cn" 
  12.             }, 
  13.             { 
  14.               "subdomains"true
  15.               "name""api.seniverse.com" 
  16.             }, 
  17.             { 
  18.               "subdomains"true
  19.               "name""v.juhe.cn" 
  20.             }, 
  21.             { 
  22.               "subdomains"true
  23.               "name""api.tianapi.com" 
  24.             } 
  25.           ] 
  26.         } 
  27.       } 
  28.     } 
  29.   } 
  30. }, 

 在domains數(shù)組中,subdomains為是否信任下級(jí)域名,name為域名,無(wú)需填寫協(xié)議。如果請(qǐng)求的服務(wù)器域名未配置,是無(wú)法請(qǐng)求成功的,且工具不會(huì)報(bào)錯(cuò)。這里一定記得配置服務(wù)器域名。

2、在js文件中引入fetch模塊。

鴻蒙js請(qǐng)求遠(yuǎn)程服務(wù)器的模塊為fetch,在js文件的最上方需引入該模塊。

  1. import fetch from '@system.fetch'

這里也是有提示的。


3、調(diào)用fetch.fetch發(fā)送請(qǐng)求。

來(lái)看一下fetch模塊的封裝,請(qǐng)求的參數(shù),響應(yīng)的類型,回調(diào)函數(shù)都可在對(duì)象中定義,和wx.request()基本一致。

  1. export default class Fetch { 
  2.   /** 
  3.    * Obtains data through the network. 
  4.    * @param options 
  5.    */ 
  6.   static fetch(options: { 
  7.     /** 
  8.      * Resource URL. 
  9.      */ 
  10.     url: string; 
  11.  
  12.     /** 
  13.      * Request parameter, which can be of the string type or a JSON object. 
  14.      */ 
  15.     data?: string | object; 
  16.  
  17.     /** 
  18.      * Request header, which accommodates all attributes of the request. 
  19.      */ 
  20.     header?: Object; 
  21.  
  22.     /** 
  23.      * Request methods available: OPTIONS, GET, HEAD, POST, PUT, DELETE and TRACE. The default value is GET. 
  24.      */ 
  25.     method?: string; 
  26.  
  27.     /** 
  28.      * The return type can be text, or JSON. By default, the return type is determined based on Content-Type in the header returned by the server. 
  29.      */ 
  30.     responseType?: string; 
  31.  
  32.     /** 
  33.      * Called when the network data is obtained successfully. 
  34.      */ 
  35.     success?: (data: IFetch) => void; 
  36.  
  37.     /** 
  38.      * Called when the network data fails to be obtained. 
  39.      */ 
  40.     fail?: (data: any, code: number) => void; 
  41.  
  42.     /** 
  43.      * Called when the execution is completed. 
  44.      */ 
  45.     complete?: () => void; 
  46.   }): void; 

 比如我在頁(yè)面初始化執(zhí)行的方法onInit()中請(qǐng)求聚合數(shù)據(jù)的天氣預(yù)報(bào)接口,就可以這樣寫:

  1. onInit() { 
  2.       // 加載天氣預(yù)報(bào) 
  3.       fetch.fetch({ 
  4.           url: 'http://apis.juhe.cn/simpleWeather/query?city=%E5%8D%97%E4%BA%AC&key=xxxxxxxxx'
  5.           responseType: 'json'
  6.           success: res => { 
  7.               ...... 
  8.           } 
  9.       }); 
  10.   } 

 4、處理返回?cái)?shù)據(jù)需調(diào)用JSON.parse()。

鴻蒙js開發(fā)目前調(diào)試功能尚不方便,雖有console.log(), console.info()等方法用于打印日志,但實(shí)際運(yùn)行時(shí)并未找到日志的打印。所以我只能在視圖中劃出一小塊區(qū)域用于調(diào)試。

這里看到雖然responseType已設(shè)置為json,但用' . '取其中屬性時(shí)仍會(huì)紅線報(bào)錯(cuò),且頁(yè)面中可以看出并未取到值,可以猜測(cè)此時(shí)的res.data仍為string類型,需調(diào)用JSON.parse()將其轉(zhuǎn)為json類型,隨后問(wèn)題解決。


  1. onInit() { 
  2.        // 加載天氣預(yù)報(bào) 
  3.        fetch.fetch({ 
  4.            url: 'http://apis.juhe.cn/simpleWeather/query?city=%E5%8D%97%E4%BA%AC&key=e4b4e30c713b6e2a24f4a851258c8457'
  5.            responseType: 'json'
  6.            success: res => { 
  7.                console.info(JSON.stringify(res.data)); //并未打印日志 
  8.                let data = JSON.parse(res.data); //必須要加上 
  9.                this.nowWeather = data.result.realtime; 
  10.                let dailyWeather = data.result.future; 
  11.                for(let i in dailyWeather) { 
  12.                    dailyWeather[i].date = dailyWeather[i].date.substr(5, 5); 
  13.                } 
  14.                this.dailyWeather = dailyWeather; 
  15.            } 
  16.        }); 

 

附上天氣預(yù)報(bào)這一部分的代碼:

  1. <!-- 天氣 --> 
  2.     <div class="weather"
  3.         <div class="now" if="{{ nowWeather }}"
  4.             <text class="nowPhe"
  5.                 {{ nowWeather.info }} 
  6.             </text> 
  7.             <text> 
  8.                 {{ nowWeather.temperature }}˚C 
  9.             </text> 
  10.             <div class="nowOther"
  11.                 <text> 
  12.                     風(fēng)力風(fēng)向: {{ nowWeather.direct }} {{ nowWeather.power }} 
  13.                 </text> 
  14.                 <text> 
  15.                     空氣質(zhì)量: {{ nowWeather.aqi }} 
  16.                 </text> 
  17.             </div> 
  18.         </div> 
  19.         <div class="daily" if="{{ dailyWeather }}"
  20.             <block for="{{ dailyWeather }}"
  21.                 <div class="dailyItem"
  22.                     <text> 
  23.                         {{ $item.date }} 
  24.                     </text> 
  25.                     <text> 
  26.                         {{ $item.weather }} 
  27.                     </text> 
  28.                     <text> 
  29.                         {{ $item.temperature }} 
  30.                     </text> 
  31.                 </div> 
  32.             </block> 
  33.         </div> 
  34.     </div>         
  35.     <!-- 天氣end --> 

  1. /*天氣*/ 
  2. .weather { 
  3.     background-image: url('./common/weatherbg.jpg'); 
  4.     background-size: contain; 
  5. .weather text { 
  6.     color: #fdfdfd; 
  7. .now { 
  8.     width: 100%; 
  9.     height: 260px; 
  10.     margin-top: 30px; 
  11.     display: flex; 
  12.     align-items: center; 
  13.     justify-content: space-around; 
  14. .now>text { 
  15.     font-size: 60px; 
  16. .nowPhe { 
  17.     margin-left: 20px; 
  18. .nowOther { 
  19.     margin-right: 20px; 
  20.     display: flex; 
  21.     flex-direction: column
  22.     height: 220px; 
  23.     justify-content: space-around; 
  24. .daily{ 
  25.     margin-top: 30px; 
  26.     display: flex; 
  27.     flex-direction: column
  28. .dailyItem{ 
  29.     margin: 0 30px 0 30px; 
  30.     height: 120px; 
  31.     border-bottom: 1px solid #bbbbbb; 
  32.     display: flex; 
  33.     justify-content: space-between
  34.     align-items: center; 

 想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com/#zz

【編輯推薦】

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2013-04-10 17:59:50

微信公眾平臺(tái)接口開發(fā)

2021-02-23 12:25:26

鴻蒙HarmonyOS應(yīng)用開發(fā)

2016-03-14 10:29:38

天氣預(yù)報(bào)各類工具源碼

2021-03-02 09:29:29

鴻蒙HarmonyOS應(yīng)用開發(fā)

2017-08-01 10:10:32

人工智能智能天氣預(yù)報(bào)

2021-02-20 09:52:02

鴻蒙HarmonyOS應(yīng)用開發(fā)

2013-03-26 13:20:27

Android天氣預(yù)報(bào)

2021-02-22 14:56:55

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 10:01:19

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-23 12:23:57

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-21 11:09:18

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-05 09:46:16

鴻蒙HarmonyOSjs開發(fā)

2009-07-07 09:25:08

Linux開發(fā)FOSS開發(fā)項(xiàng)目

2018-01-29 11:25:37

LinuxASCII 字符天氣預(yù)報(bào)

2020-02-11 20:00:29

開源開源工具天氣預(yù)報(bào)

2021-02-23 09:52:42

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-04 13:49:41

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-02-25 15:13:08

鴻蒙HarmonyOS應(yīng)用開發(fā)

2010-08-13 10:56:58

FlexWebservice

2021-02-24 09:36:03

鴻蒙CSS應(yīng)用開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)