zl程序教程

您现在的位置是:首页 >  Javascript

当前栏目

鸿蒙HarmonyOS三方件开发指南(3)-AsyncHttpHarmony组件

2023-04-18 14:51:26 时间

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

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

1. AsyncHttpHarmony功能介绍

1.1. 组件介绍

在做HarmonyOS开发过程中,用java原生的HttpsURLConnection实现网络请求很难高效的达到预期效果。我们需要高效的处理数据解析,更加快捷的实现UI线程更新,这里基于方网络框架AsyncHttpClient二次封装,更加高效实现网络请求及数据处理。同时HarmonyOS为我们提供了TaskDispatcher类派发同步任务,达到更新UI的效果。

1.2. TV模拟器上运行效果

请求前:


点击get请求之后:


2. AsyncHttpHarmony使用方法

2.1. 为应用添加httplibrary-debug.har包依赖

在应用模块中调用HAR,常用的添加依赖的方式包括如下两种。

Ø 方式一:依赖本地HAR

第一步:将httplibrary-debug.har复制到entrylibs目录下即可(由于build.gradle中已经依赖libs目录下的*.har,因此不需要再做修改)。


查看工程目录中build.gradle下的*.har是否存在:


第二步:需要添加外部依赖实现Header类的引入,引入方式如下图,引入完之后同步下即可可以使用。


2.2. 为应用添加网络权限,config.json文件部分代码如下:

  1. "reqPermissions": [ 
  2.  
  3.  
  4. "name""ohos.permission.INTERNET"
  5.  
  6. "reason"""
  7.  
  8. "usedScene": { 
  9.  
  10. "ability": [ 
  11.  
  12. "com.example.ohosdemo.MainAbility"
  13.  
  14. "com.example.ohosdemo.slice.MainAbilitySlice" 
  15.  
  16. ], 
  17.  
  18. "when""always" 
  19.  
  20.  
  21. }, 

 以上操作无误之后就可以进行编码了!

3. AsyncHttpHarmony开发实现

3.1. 主页面的布局文件

定义一个Text文本用来显示请求返回的数据,一个text实现请求点击事件

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <DirectionalLayout 
  4.  
  5.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  6.  
  7.     ohos:height="match_parent" 
  8.  
  9.     ohos:width="match_parent" 
  10.  
  11.     ohos:orientation="vertical"
  12.  
  13.  
  14.  
  15.  
  16.     <Text 
  17.  
  18.         ohos:id="$+id:tvResult" 
  19.  
  20.         ohos:height="match_content" 
  21.  
  22.         ohos:width="match_content" 
  23.  
  24.         ohos:background_element="$graphic:background_ability_main" 
  25.  
  26.         ohos:layout_alignment="horizontal_center" 
  27.  
  28.         ohos:text="数据显示" 
  29.  
  30.         ohos:text_size="50" 
  31.  
  32.         ohos:top_margin="180vp" 
  33.  
  34.     /> 
  35.  
  36.     <Text 
  37.  
  38.         ohos:id="$+id:tvRequest" 
  39.  
  40.         ohos:height="match_content" 
  41.  
  42.         ohos:width="match_content" 
  43.  
  44.         ohos:background_element="$graphic:background_ability_main" 
  45.  
  46.         ohos:layout_alignment="horizontal_center" 
  47.  
  48.         ohos:text="get请求" 
  49.  
  50.         ohos:text_size="50" 
  51.  
  52.         ohos:top_margin="80vp" 
  53.  
  54.         /> 
  55.  
  56.  
  57.  
  58.  
  59. </DirectionalLayout> 

 3.2. MainAbilitySlice代码如下

核心代码是initListener,其中声明了一个 AsyncHttpClient对象,设置请求参数,调用get方法获取ulr返回结果,然后通过TaskDispatcher类派发同步任务达到更新UI的效果,代码如下:

  1. package com.huawei.asynchttpharmonyos.slice; 
  2.  
  3.  
  4.  
  5.  
  6. import com.example.httplibrary.utils.AsyncHttpClient; 
  7.  
  8. import com.example.httplibrary.utils.JsonHttpResponseHandler; 
  9.  
  10. import com.example.httplibrary.utils.RequestParams; 
  11.  
  12. import com.huawei.asynchttpharmonyos.ResourceTable; 
  13.  
  14. import cz.msebera.android.httpclient.Header; 
  15.  
  16. import ohos.aafwk.ability.AbilitySlice; 
  17.  
  18. import ohos.aafwk.content.Intent; 
  19.  
  20. import ohos.agp.components.Component; 
  21.  
  22. import ohos.agp.components.Text; 
  23.  
  24. import ohos.hiviewdfx.HiLog; 
  25.  
  26. import ohos.hiviewdfx.HiLogLabel; 
  27.  
  28.  
  29.  
  30.  
  31. public class MainAbilitySlice extends AbilitySlice { 
  32.  
  33.  
  34.  
  35.  
  36.     private Text tvRequest,tvResult; 
  37.  
  38.  
  39.  
  40.  
  41.     private static final String TAG = "MainAbilitySlice"
  42.  
  43.  
  44.  
  45.  
  46.     private static final HiLogLabel label=new HiLogLabel(HiLog.DEBUG,0x00100,"async-http"); 
  47.  
  48.     @Override 
  49.  
  50.     public void onStart(Intent intent) { 
  51.  
  52.         super.onStart(intent); 
  53.  
  54.         super.setUIContent(ResourceTable.Layout_ability_main); 
  55.  
  56.         initView(); 
  57.  
  58.         initListener(); 
  59.  
  60.     } 
  61.  
  62.  
  63.  
  64.  
  65.     private void initView() { 
  66.  
  67.         tvResult = (Text) findComponentById(ResourceTable.Id_tvResult); 
  68.  
  69.         tvRequest = (Text) findComponentById(ResourceTable.Id_tvRequest); 
  70.  
  71.     } 
  72.  
  73.  
  74.  
  75.  
  76.     private void initListener() { 
  77.  
  78.         tvRequest.setClickedListener(new Component.ClickedListener() { 
  79.  
  80.             @Override 
  81.  
  82.             public void onClick(Component component) { 
  83.  
  84.                 String url="https://apis.juhe.cn/simpleWeather/query"
  85.  
  86.                 String key="32becf485f7f174d4385957b62f28f61"
  87.                  //这里获取AsyncHttpClient实例, 这个类提供了get post delete put 请求对外的接口方法 
  88.  
  89.                     AsyncHttpClient client=new AsyncHttpClient(); 
  90.   
  91.                  //这里是我们包装参数的实体类 
  92.  
  93.                     RequestParams params=new RequestParams(); 
  94.  
  95.                     params.put("city","西安"); 
  96.  
  97.                     params.put("key",key); 
  98.                  /这里是实现get请求的方,JsonHttpResponseHandler会重写请求成功的onSuccess和onFailure两个方法,两个方法内部做具体业务逻辑 
  99.  
  100.                     client.get(url,params, new JsonHttpResponseHandler() { 
  101.  
  102.                         @Override 
  103.  
  104.                         public void onSuccess(int statusCode, Header[] headers, String responseString) { 
  105.  
  106.                             super.onSuccess(statusCode, headers, responseString); 
  107.  
  108.                             HiLog.error(label,"zel-onSuccess:"+responseString,responseString); 
  109.  
  110.                             // 通知主线程更新UI 
  111.  
  112.                              getUITaskDispatcher().asyncDispatch(new Runnable() { 
  113.  
  114.                                 @Override 
  115.  
  116.                                 public void run() { 
  117.                                  // 这里具体业务Text文本显示请求数据 
  118.  
  119.                                     tvResult.setText(responseString); 
  120.  
  121.                                 } 
  122.  
  123.                             }); 
  124.  
  125.                         } 
  126.  
  127.  
  128.  
  129.  
  130.                         @Override 
  131.  
  132.                         public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { 
  133.  
  134.                             super.onFailure(statusCode, headers, responseString, throwable); 
  135.  
  136.                             HiLog.error(label,"zel-onFailure:"+responseString,responseString); 
  137.  
  138.                         } 
  139.  
  140.                     }); 
  141.  
  142.  
  143.  
  144.  
  145.             } 
  146.  
  147.         }); 
  148.  
  149.     } 
  150.  
  151.  
  152.  
  153.  
  154.     @Override 
  155.  
  156.     public void onActive() { 
  157.  
  158.         super.onActive(); 
  159.  
  160.     } 
  161.  
  162.  
  163.  
  164.  
  165.     @Override 
  166.  
  167.     public void onForeground(Intent intent) { 
  168.  
  169.         super.onForeground(intent); 
  170.  
  171.     } 
  172.  

项目源代码地址:https://github.com/isoftstone-dev/Http-Async-HarmonyOS

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

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