DEV Community

Cover image for HarmonyOS development: Switch to rcp-based network request
程序员一鸣
程序员一鸣

Posted on

HarmonyOS development: Switch to rcp-based network request

Foreword 

this article is based on Api12 

before the open source a for network requests based on http encapsulation, please refer to the previous article 《 HarmonyOS development: a network request library based on http open source ", but with the upgrade of Api, another Api form has been recommend for network communication, that is, based on Remote Communication Kit (Far Field Communication Service)  , can only sigh, the Api changes really fast, a little unable to keep up with the official rhythm. 

Image description

In order to facilitate the maintenance of Api in the future, what should I do? Can only be based on Remote Communication Kit (Far Field Communication Service) Let's start another one. After all, the evolution in the future is based on this. 

The content of this article is mainly to modify the library based on http encapsulation to the current Remote Communication Kit (Far-field communication service), nothing more than the way of communication has changed, and everything else is not bad. 

The contents of this paper are as follows: 

1. Brief introduction of Remote Communication Kit (far field Communication service)

2. Quick use of http library

3. Differences between http library and net Library

4. Summary of http Library

i. Brief introduction of Remote Communication Kit (far field Communication service) 

the Remote Communication Kit (far-field Communication service) is a NAPI package provided by Huawei that initiates HTTP data requests. Through the Remote Communication Kit, applications can quickly and conveniently initiate data requests to the server. 

Image description

The main business processes for using the Remote Communication Kit are as follows: 

  1. The application client creates a session. 

  2. The application client initiates a request. 

  3. The application client receives the request result and processes the corresponding service. 

II, http library quick use

central Warehouse Address: https://ohpm.openharmony.cn/#/cn/detail/@abner%2Fhttp 

at present, the packaged http library has been submitted to the central warehouse, and everyone can check and use it in the central warehouse. Except for differences in some places, all functions are completely the same as those of the net Library, which is convenient for students who used net to seamlessly connect. 

Remote Dependency Use [recommend]] 

method 1: in the Terminal window, run the following command to install the third-party package. DevEco Studio automatically adds the third-party package dependency to the project oh-package.json5.

Build proposal: Execute the command under the module path used.

 

ohpm install @abner/http
Enter fullscreen mode Exit fullscreen mode

Method 2: Set the three-party package dependency in the project oh-package.json5. The configuration example is as follows:

 

"dependencies": { "@abner/http": "^1.0.0"}
Enter fullscreen mode Exit fullscreen mode

III, differences between http library and net Library

difference point one, interceptor 

interceptors are different. net library is encapsulated based on http. http does not have an interceptor. In order to intercept requests from net library, a layer of synchronous callback processing is done, while http library is based on Remote Communication Kit (Far Field Communication Service) Encapsulated, comes with an interceptor, which can be configured when requested globally or individually. 

Global Configuration 

the global configuration may handle each network request.

 

Net.getInstance().init({
      interceptors: [new GlobalInterceptor()]
    })
Enter fullscreen mode Exit fullscreen mode

Can also pass

 

Net.getInstance().setInterceptors([new GlobalInterceptor()])
Enter fullscreen mode Exit fullscreen mode

single configuration 

A single configuration applies only to the current network request.

 

Net.post("/cert/xx")
          .setInterceptor(new TestInterceptor())
            // .setRequestInterceptors() 
          .setParams({
            "index_type": -1,
            "size": 1,
          })
          .requestString((result) => {
            console.log("===success:" + result)
          }, (err) => {
            console.log("===error:" + err.getMessage())
          })
Enter fullscreen mode Exit fullscreen mode

Difference point two, individual attribute parameters are different 

after all, the Api has changed, and the related methods will also be different, such as setting connection timeout, reading timeout, etc., which need to be adapted by new methods. 

IV, summary of http library

It has the same effect as the net Library. Therefore, if you used the net library before, it can be seamlessly connected. Of course, the official did not say to delete Api http, so it can also be used for Api http. Since the official has already marked it, everyone should try their best to use it. Remote Communication Kit (Far Field Communication Service).

Top comments (0)