1 Star 1 Fork 0

OpenNeusoft / Ohos_lite_http

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

Harmonyos network framework: LiteHttp

Tags : litehttp2.x-tutorials


Harmonyos网络通信为啥子选 lite-http ?

lite-http 初步使用 和 快速上手


1. What‘s lite-http ?

LiteHttp is a simple, intelligent and flexible HTTP framework for harmonyos. With LiteHttp you can make HTTP request with only one line of code! It could convert a java model to the parameter and rander the response JSON as a java model intelligently.

2. Why choose lite-http ?

gradle

Step 1. Add the maven repository to your build file

Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			mavenCentral()
		}
	}

Step 2. Add the dependency

	dependencies {
	        implementation 'io.github.dzsf:ohoslitehttp:1.0.3'
	        implementation 'io.github.dzsf:ohosapacheclient:1.0.3'
	}

Simple, powerful, make HTTP request with only one line of code:

User user = liteHttp.get (url, User.class);

asynchronous download a file(execute on sub-thread,listen on ui-thread):

liteHttp.executeAsync(new FileRequest(url,path).setHttpListener(
	new HttpListener<File>(true, true, true) {
	
        @Override
        public void onLoading(AbstractRequest<File> request, long total, long len) {
            // loading notification
        }

        @Override
        public void onSuccess(File file, Response<File> response) {
            // successfully download 
        }
		
	})
);

configure an asynchronous login request by annotation:

String loginUrl = "http://litesuits.com/mockdata/user_get";

// 1. URL        : loginUrl
// 2. Parameter  : name=value&password= value
// 3. Response   : User
@HttpUri(loginUrl) 
class LoginParam extends HttpRichParamModel<User> {
    private String name;
    private String password;

    public LoginParam(String name, String password) {
        this.name = name;
        this.password = password;
    }
}
liteHttp.executeAsync(new LoginParam("lucy", "123456"));

will be built as http://xxx?name=lucy&password=123456

more details, you can see lite-http introduction: LiteHttp Introduction: Why should developers choose LiteHttp ?

3. What are the fetures ?

  • Lightweight: tiny size overhead to your app. About 99kb for core jar. .

  • One-Thread-Based: all methods work on the same thread as the request was created.

  • Full support: GET, POST, PUT, DELETE, HEAD, TRACE, OPTIONS, PATCH.

  • Automatic: one line of code auto-complete translation between Model and Parameter, Json and Model.

  • Configurable: more flexible configuration options, up to 23+ items.

  • Polymorphic: more intuitive API, input and output is more clear.  

  • Strong Concurrency: concurrent scheduler that comes with a strong, effective control of scheduling and queue control strategies.

  • Annotation Usage: convention over configuration. Parameters, Response, URL, Method, ID, TAG, etc. Can be configured.

  • Easy expansion: extend the abstract class DataParser to parse inputstream(network) to which you want..

  • Alternatively: interface-based, easy to replace the network connection implementations and Json serialization library.

  • Multilayer cache: hit Memory is more efficient! Multiple cache mode. Support for setting cache expire time.

  • Callback Flexible: callback can be on current or UI thread. listen the beginning, ending, success or failure, uploading, downloading, etc.

  • File Upload: support for single, multiple, large file uploads.

  • Downloads: support files, Bimtap download and progress notifications.

  • Network Disabled: disable one of a variety of network environments, such as specifying disabling 2G, 3G.

  • Statistics: time cost statistics and traffic statistics.

  • Exception system: a unified, concise, clear exception is thrown into three categories: client, network, server, and abnormalities can be accurately subdivided.

  • GZIP compression: automatic GZIP compression.

  • Automatic Retry: combined probe exception type and current network conditions, intelligent retry strategies.

  • Automatic redirection: based on the retry 30X state, and can set the maximum number of times to prevent excessive jump.

4. tutorials and analysis (◕‸◕)

Good ◝‿◜, huh:

 [1. Initialization and preliminary usage] 8

 [2. Simplified requests and non-safe method of use] 9

 [3. Automatic model conversion] 10

 [4. Custom DataParser and Json serialization library Replace] 11

 [5. Files, bitmap upload and download] 12

 [6. Disable network and traffic statistics] 13

 [7. Retries and redirect] 14

 [8. Exceptions handling and cancellation request] 15

 [9. Multiple data transmission via POST(PUT)] 16

 [10. Asynchronous concurrency and scheduling strategy] 17

 [11. Global configuration and parameter settings Detailed] 18

 [12. Annotation-Based request] 19

 [13. Multilayer cache mechanism and usage] 20

 [14. Detailed of callback listener] 21

 [15. SmartExecutor: concurrent scheduler] 22

Versions

  • v1.0.0

Licence

  • Apache Licence

LiteHttp: Harmonyos网络通信框架

中文版 换个语种,再来一次

标签: litehttp2.x版本系列教程


Harmonyos网络框架为什么可以选用lite-http?

lite-http 初步使用 和 快速起步上手

本系列文章面向Harmonyos开发者,展示开源网络通信框架LiteHttp的主要用法,并讲解其关键功能的运作原理,同时传达了一些框架作者在日常开发中的一些最佳实践和经验。


LiteHttp之开篇简介和大纲目录

1. lite-http是什么? (・̆⍛・̆)

LiteHttp是一款简单、智能、灵活的HTTP框架库,它在请求和响应层面做到了全自动构建和解析,主要用于Harmonyos快速开发。

2. 为什么选lite-http? (•́ ₃ •̀)

gradle

Step 1. Add the maven repository to your build file

Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			mavenCentral()
		}
	}

Step 2. Add the dependency

	dependencies {
	        implementation 'io.github.dzsf:ohoslitehttp:1.0.3'
	        implementation 'io.github.dzsf:ohosapacheclient:1.0.3'
	}

简单、强大,线程无关,一行代码搞定API请求和数据转化:

User user = liteHttp.get(url, User.class);

当然也可以开启线程异步下载文件:

liteHttp.executeAsync(new FileRequest(url,path).setHttpListener(
	new HttpListener<File>(true, true, true) {
	
        @Override
        public void onLoading(AbstractRequest<File> request, long total, long len) {
            // loading notification
        }

        @Override
        public void onSuccess(File file, Response<File> response) {
            // successfully download 
        }
		
	})
);

通过注解约定完成异步请求:

@HttpUri(loginUrl) 
class LoginParam extends HttpRichParamModel<User> {
    private String name;
    private String password;

    public LoginParam(String name, String password) {
        this.name = name;
        this.password = password;
    }
}
liteHttp.executeAsync(new LoginParam("lucy", "123456"));

将构建类似下面请求:http://xxx?name=lucy&password=123456

案例详情可见我另一篇lite-http引言文章:LiteHttp 引言:开发者为什么要选LiteHttp??

3. lite-http有什么特点? (´ڡ`)

  • 轻量级:微小的内存开销与Jar包体积,99K左右。

  • 单线程:请求本身具有线程无关特性,基于当前线程高效率运作。

  • 全支持:GET, POST, PUT, DELETE, HEAD, TRACE, OPTIONS, PATCH。

  • 全自动:一行代码自动完成Model与Parameter、Json与Model。

  • 可配置:更多更灵活的配置选择项,多达 23+ 项。

  • 多态化:更加直观的API,输入和输出更加明确。

  • 强并发:自带强大的并发调度器,有效控制任务调度与队列控制策略。

  • 注解化:通过注解约定参数,URL、Method、ID、TAG等都可约定。

  • 易拓展:自定义DataParser将网络数据流转化为你想要的数据类型。

  • 可替换:基于接口,轻松替换网络连接实现方式和Json序列化库。

  • 多层缓存:内存命中更高效!多种缓存模式,支持设置缓存有效期。

  • 回调灵活:可选择当前或UI线程执行回调,开始结束、成败、上传、下载进度等都可监听。

  • 文件上传:支持单个、多个大文件上传。

  • 文件下载:支持文件、Bimtap下载及其进度通知。

  • 网络禁用:快速禁用一种、多种网络环境,比如指定禁用 2G,3G 。

  • 数据统计:链接、读取时长统计,以及流量统计。

  • 异常体系:统一、简明、清晰地抛出三类异常:客户端、网络、服务器,且异常都可精确细分。

  • GZIP压缩:Request, Response 自动 GZIP 压缩节省流量。

  • 自动重试:结合探测异常类型和当前网络状况,智能执行重试策略。

  • 自动重定向:基于 30X 状态的重试,且可设置最大次数防止过度跳转。

4. 老湿,来点教学和分析带我飞呗? (◕‸◕)

好的 ◝‿◜ ,下面直接给你看,疗效好记得联系我,呵呵哒:

1. 初始化和初步使用

2. 简化请求和非安全方法的使用

3. 自动对象转化

4. 自定义DataParser和Json序列化库的替换

5. 文件、位图的上传和下载

6. 禁用网络和流量、时间统计

7. 重试和重定向

8. 处理异常和取消请求

9. POST方式的多种类型数据传输

10. lite-http异步并发与调度策略

11. 全局配置与参数设置详解

12. 通过注解完成API请求

13. 多层缓存机制及用法

14. 回调监听器详解

15. 并发调度控制器详解

版本迭代

  • v1.0.0

版权和许可信息

  • Apache Licence
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

只需一行代码就可以发出HTTP请求!它可以将java模型转换为参数,并智能地将响应JSON命名为java模型 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/openneusoft/ohos_lite_http.git
git@gitee.com:openneusoft/ohos_lite_http.git
openneusoft
ohos_lite_http
Ohos_lite_http
master

搜索帮助