OkGo - 3.0 is based on the Http protocol and encapsulates OkHttp's network request framework. It is easier to use than Retrofit, supports RxJava, RxJava2, and supports custom caching. Batch breakpoint download management and batch upload management Stars
This library - com.lzy.demo, is very useful in the development of applications which are in our daily use. Some of such examples mentioned below:
|
|
In this section, we can see the list of features which the library provides which makes the use of this library very easy and friendly. Primarily, this library supports customization of component attributes using the below mechanism.
- Java APIs
okhttp-OkGo uses a simple fluent java API's that allows users to make most requests in a single line:
Features supported by this component includes the below:
- Support for RxJava & RxJava2:
Implementing applications that deal with complex concurrent behavior has always been a challenging aspect of programming. Various paradigms exist that promise a solution to this problem in different ways. RxJava is a Java library that enables Functional Reactive Programming in Harmony development. - custom cache policies.:
Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth. - download management :
Interceptors are simpler. An executor for switching threads and a listener for progress monitoring are passed in from the constructor. By setting this interceptor, the download process is intercepted in our custom DowloadResponseBody. - upload management.:
Support file upload and download, upload progress callback, download progress callback. form upload (multiple files and multi-parameter upload together)
For using the library in your HarmonyOS mobile app, you need to first install it by following below methods.
- Method 1:
Generate the .har package through the library and add the .har package to the libs folder.Add the following code to the entry level build.gradle:implementation fileTree (dir: 'libs', include: ['* .jar', '* .har'])
- Method 2 :
Copy the dependency from the gitee and add it to the entry level build.gradle:
dependencies { implementation fileTree(dir: 'libs', include: ['*.har']) implementation 'COM.LZY.NET : out OkGo: 3.0 . . 4' }
Since we are going to do network operations, we need to add internet permission in config.json file
{ "app": { "bundleName": "com.example.okhttpokGo", "vendor": "example", "version": { "code": 1000000, "name": "1.0" }, "apiVersion": { "compatible": 5, "target": 5, "releaseType": "Beta1" } }, "deviceConfig": { "default": { "network": { "cleartextTraffic": true } } }, "module": { "package": "com.example.okhttpokGo", "name": ".MyApplication", "reqCapabilities": [ "video_support" ], "deviceType": [ "phone" ], "distro": { "deliveryWithInstall": true, "moduleName": "entry", "moduleType": "entry" }, "abilities": [ { "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ], "orientation": "unspecified", "name": "com.example.okhttpokGo.MainAbility", "icon": "$media:icon", "description": "$string:mainability_description", "label": "$string:app_name", "type": "page", "launchType": "standard" } ], "reqPermissions": [ { "name": "ohos.permission.INTERNET" } ] } }
This section will help us to understand the usage of the library as you use it in your Harmony-application developemnt project.
We are going to load list into Button component using This Library. So, add Button component into resource_file.xml file.
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
xmlns:app="http://schemas.huawei.com/hap/res-auto"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical"
ohos:background_element="#000000">
<Button
ohos:id="$+id:get"
ohos:height="30vp"
ohos:width="100vp"
ohos:background_element="#888"
ohos:left_margin="20vp"
ohos:text="GET"/>
</DirectionalLayout>
@Override
public void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_layout_base_details);
OkGo.<String>get("https://httpbin.org/get")
.tag(this)
.execute(new StringCallback() {
@Override
public void onSuccess(Response<String> response) {
OkLogger.e("GET Response" + response.body());
handleResponse(response);
}
});;
}
The public methods below will help us to operate on the component at runtime.
okhttpokGo Methods
|
|
In this section, we can have a look at some the examples where the APIs of this library is put to use and the results which we can acheive.
Example1: okhttp-OkGo with Get Method
Layout.xml:
<Button ohos:id="$+id:Get" ohos:height="30vp" ohos:width="match_parent" ohos:background_element="#9017DD" ohos:left_margin="5vp" ohos:right_margin="5vp" ohos:text="GET" ohos:text_color="white" ohos:text_size="25fp" ohos:top_margin="6vp"/>Java Slice: //GET private void getMethod() { OkGo. get("https://demonuts.com/Demonuts/JsonTest/Tennis/json_parsing.php") .tag(this) .execute(new StringCallback() { @Override public void onSuccess(Response response) { OkLogger.e("GET Response:" + response.body()); handleResponse(response); } }); } |
**Example2: okhttp-OkGo With Download Method **
Layout.xml:
<Button ohos:id="$+id:post" ohos:height="30vp" ohos:width="100vp" ohos:background_element="#888" ohos:left_margin="20vp" ohos:text="Download"/>Java Slice: //Download private void downloadMethod() { |
Example3: okhttp-OkGo with PATCH Method**
Layout.xml:
<Button ohos:id="$+id:patch" ohos:height="30vp" ohos:width="match_parent" ohos:background_element="#9017DD" ohos:left_margin="5vp" ohos:right_margin="5vp" ohos:text="PATCH" ohos:text_color="white" ohos:text_size="20fp" ohos:top_margin="6vp" ohos:visibility="visible"/>Java Slice: private void patchMethod() { // PATCH OkGo.patch("https://httpbin.org/patch") .execute(new StringCallback() { @Override public void onSuccess(Response response) { OkLogger.e("PATCH Response:---" + response.body()); handleResponse(response); } }); } |
okhttp-OkGo is a very easy to use and very powerful library.The performance of the library is very good even when it works on one of the latest operating systems in the world, which is HarmonyOS!
-
For more exciting libraries to develop your app, peep into third-party-components at
OpenHarmony-TPC -
To know more about the developement work happening on harmony aaplication layer, and even be part of the exciting stuff, watch this space of Application Library Engineering Group