Skip to content

Commit

Permalink
Upgrade RxHttp to 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
liujingxing committed Oct 30, 2022
1 parent 19d8b22 commit 8421614
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ English | [中文文档](https://github.com/liujingxing/rxhttp/blob/master/READM
A type-safe HTTP client for Android. Written based on OkHttp


![sequence_chart_en.jpg](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2c637845930b49d7a7466ec7b5dcdb77~tplv-k3u1fbpfcp-watermark.image)
![sequence_chart_en.jpg](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/2e25f9c6aa694b57bd43871eff95cecd~tplv-k3u1fbpfcp-watermark.image)


<table>
Expand All @@ -26,13 +26,13 @@ A type-safe HTTP client for Android. Written based on OkHttp
//tryAwait return User?
val user = RxHttp.get("/server/..")
.add("key", "value")
.toClass<User>()
.toAwait<User>()
.await()

//or awaitResult return kotlin.Result<T>
RxHttp.get("/server/..")
.add("key", "value")
.toClass<User>()
.toAwait<User>()
.awaitResult {
//Success
}.onFailure {
Expand Down Expand Up @@ -60,7 +60,7 @@ A type-safe HTTP client for Android. Written based on OkHttp
```java
RxHttp.get("/server/..")
.add("key", "value")
.asClass<User>()
.toObserable<User>()
.subscribe({
//Success
}, {
Expand All @@ -74,7 +74,7 @@ A type-safe HTTP client for Android. Written based on OkHttp
```java
RxHttp.get("/server/..")
.add("key", "value")
.asClass(User.class)
.toObserable(User.class)
.subscribe(user - > {
//Success
}, throwable -> {
Expand Down Expand Up @@ -135,18 +135,23 @@ android {
plugins {
// kapt/ksp choose one
// id 'kotlin-kapt'
id 'com.google.devtools.ksp' version '1.7.10-1.0.6'
id 'com.google.devtools.ksp' version '1.7.20-1.0.7'
}

//Make IDE aware of generated code if you use ksp
kotlin {
sourceSets.debug {
kotlin.srcDir("build/generated/ksp/debug/kotlin")
android {
applicationVariants.all { variant ->
sourceSets {
def name = variant.name
getByName(name) { //告知IDE,ksp生成的kotlin代码
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}
}
}

dependencies {
def rxhttp_version = '2.9.5'
def rxhttp_version = '3.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
// ksp/kapt/annotationProcessor choose one
Expand Down Expand Up @@ -284,7 +289,7 @@ This step is optional
```java
RxHttpPlugins.init(OkHttpClient)
.setDebug(boolean)
.setOnParamAssembly(Function)
.setOnParamAssembly(Consumer)
....
```

Expand All @@ -308,7 +313,7 @@ public class Url {
RxHttp.get("/service/...") //1、You can choose get,postFrom,postJson etc
.addQuery("key", "value") //add query param
.addHeader("headerKey", "headerValue") //add request header
.asClass(Student.class) //2、Use the asXxx method to determine the return value type, customizable
.toObserable(Student.class) //2、Use the asXxx method to determine the return value type, customizable
.subscribe(student -> { //3、Subscribing observer
//Success callback,Default IO thread
}, throwable -> {
Expand All @@ -320,7 +325,7 @@ RxHttp.postForm("/service/...") //post FormBody
.add("key", "value") //add param to body
.addQuery("key1", "value1") //add query param
.addFile("file", File(".../1.png")) //add file to body
.asClass<Student>()
.toObserable<Student>()
.subscribe({ student ->
//Default IO thread
}, { throwable ->
Expand All @@ -329,7 +334,7 @@ RxHttp.postForm("/service/...") //post FormBody

// kotlin coroutine
val students = RxHttp.postJson("/service/...") //1、post {application/json; charset=utf-8}
.toList<Student>() //2、Use the toXxx method to determine the return value type, customizable
.toAwaitList<Student>() //2、Use the toXxx method to determine the return value type, customizable
.await() //3、Get the return value, await is the suspend method
```

Expand All @@ -340,7 +345,7 @@ val students = RxHttp.postJson("/service/...") //1、post {application/json; ch
```java
//In Rxjava2 , Automatic close request
RxHttp.get("/service/...")
.asString()
.toObservableString()
.as(RxLife.as(this)) //The Activity destroys and automatically closes the request
.subscribe(s -> {
//Default IO thread
Expand All @@ -350,7 +355,7 @@ RxHttp.get("/service/...")

//In Rxjava3 , Automatic close request
RxHttp.get("/service/...")
.asString()
.toObservableString()
.to(RxLife.to(this)) //The Activity destroys and automatically closes the request
.subscribe(s -> {
//Default IO thread
Expand All @@ -361,7 +366,7 @@ RxHttp.get("/service/...")

//In RxJava2/RxJava3, close the request manually
Disposable disposable = RxHttp.get("/service/...")
.asString()
.toObservableString()
.subscribe(s -> {
//Default IO thread
}, throwable -> {
Expand Down
26 changes: 13 additions & 13 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@

# 2、请求三部曲

![](https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/96962077a7874293919bc8379b2d45ac~tplv-k3u1fbpfcp-watermark.image)
![](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/29bdab825c4f42c0983c58777f3af675~tplv-k3u1fbpfcp-watermark.image)

***代码表示,
[asXxx、toXxx、toFlowXxx方法介绍点这里](https://github.com/liujingxing/rxhttp/wiki/RxJava%E3%80%81Await%E3%80%81Flow-%E5%AF%B9%E5%BA%94%E7%9A%84-asXxx%E3%80%81toXxx%E3%80%81toFlowXxx%E6%96%B9%E6%B3%95%E4%BB%8B%E7%BB%8D)***
[toObservableXxx、toAwaitXxx、toFlowXxx方法介绍点这里](https://github.com/liujingxing/rxhttp/wiki/RxJava%E3%80%81Await%E3%80%81Flow-%E5%AF%B9%E5%BA%94%E7%9A%84-asXxx%E3%80%81toXxx%E3%80%81toFlowXxx%E6%96%B9%E6%B3%95%E4%BB%8B%E7%BB%8D)***

<table>
<tr>
Expand All @@ -53,13 +53,13 @@
//同步式写法
val user = RxHttp.get("/server/..")
.add("key", "value")
.toClass<User>()
.toAwait<User>()
.await() //tryAwait return User?

//回调式写法
RxHttp.get("/server/..")
.add("key", "value")
.toClass<User>()
.toAwait<User>()
.awaitResult {
//成功回调
}.onFailure {
Expand Down Expand Up @@ -87,7 +87,7 @@
```java
RxHttp.get("/server/..")
.add("key", "value")
.asClass<User>()
.toObserable<User>()
.subscribe({
//成功回调
}, {
Expand All @@ -101,7 +101,7 @@
```java
RxHttp.get("/server/..")
.add("key", "value")
.asClass(User.class)
.toObserable(User.class)
.subscribe(user - > {
//成功回调
}, throwable -> {
Expand All @@ -118,13 +118,13 @@

| 功能说明 | RxHttp | [Retrofit](https://github.com/square/retrofit) |
| --- | :---: | :---: |
| 版本| v2.9.5| v2.9.0 |
| 版本| v3.0.0| v2.9.0 |
| 状态| 维护中| 维护中 |
| 标准RESTful风格|||
| 学习成本|||
| 扩展性|||
| 源码大小| 81k | 75k |
| jar包大小| 269k | 123k |
| jar包大小| 218k | 123k |
| RxJava| RxJava ❌<br>RxJava2✅<br>RxJava3✅| RxJava ✅<br>RxJava2✅<br>RxJava3✅|
| Kotlin协程|||
| Flow流|||
Expand All @@ -143,7 +143,7 @@

**说明**

也许你有会有疑问,RxHttp源码大小仅比retrofit大6k的情况下,jar包大小为何会大一倍多?功能太多导致的代码臃肿?并不是,而是由kotlin导致的,在RxHttp内部,为了支持`Await/Flow`,运用了大量的kotlin内联方法及扩展方法,这些方法在编译为字节码后,都会相对较大,其中[AwaitTransform.kt](https://github.com/liujingxing/rxhttp/blob/master/rxhttp/src/main/java/rxhttp/AwaitTransform.kt)[CallFactoryToAwait.kt](https://github.com/liujingxing/rxhttp/blob/master/rxhttp/src/main/java/rxhttp/CallFactoryToAwait.kt)[CallFactoryToFlow.kt](https://github.com/liujingxing/rxhttp/blob/master/rxhttp/src/main/java/rxhttp/CallFactoryToFlow.kt)这3个kotlin文件,编译为字节码后,就达到了100k+, 再加上其他kotlin文件,就达到了目前的jar包大小
也许你有会有疑问,RxHttp源码大小仅比retrofit大6k的情况下,jar包大小为何会大一倍多?功能太多导致的代码臃肿?并不是,而是由kotlin导致的,在RxHttp内部,为了支持`Await/Flow`,运用了大量的kotlin内联方法及扩展方法,这些方法在编译为字节码后,都会相对较大,其中[AwaitTransform.kt](https://github.com/liujingxing/rxhttp/blob/master/rxhttp/src/main/java/rxhttp/AwaitTransform.kt)[CallFactoryToAwait.kt](https://github.com/liujingxing/rxhttp/blob/master/rxhttp/src/main/java/rxhttp/CallFactoryToAwait.kt)[CallFactoryToFlow.kt](https://github.com/liujingxing/rxhttp/blob/master/rxhttp/src/main/java/rxhttp/CallFactoryToFlow.kt)这3个kotlin文件,编译为字节码后,就达到了100k+


# 3、相关文档
Expand Down Expand Up @@ -194,7 +194,7 @@ android {
}
//3、添加依赖
dependencies {
def rxhttp_version = '2.9.5'
def rxhttp_version = '3.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
annotationProcessor "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
Expand Down Expand Up @@ -226,7 +226,7 @@ plugins {
}
dependencies {
def rxhttp_version = '2.9.5'
def rxhttp_version = '3.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
kapt "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
Expand Down Expand Up @@ -263,11 +263,11 @@ android {
}
//3、添加插件及依赖
plugins {
id 'com.google.devtools.ksp' version '1.7.10-1.0.6'
id 'com.google.devtools.ksp' version '1.7.20-1.0.7'
}
dependencies {
def rxhttp_version = '2.9.5'
def rxhttp_version = '3.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
ksp "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
Expand Down
26 changes: 13 additions & 13 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'com.google.devtools.ksp' version "$ksp_version"
// id 'com.google.devtools.ksp' version "$ksp_version"
// id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10'
}

Expand Down Expand Up @@ -54,18 +54,18 @@ android {
}
}

ksp {
arg("rxhttp_rxjava", rxjava_version)
// arg("rxhttp_package", "rxhttp")
}

//kapt {
// arguments {
// arg("rxhttp_rxjava", rxjava_version) //可传入rxjava2、rxjava3或具体版本号,如 3.1.1
//// arg("rxhttp_package", "rxhttp") //设置RxHttp相关类的包名,多module依赖时,需要配置不同的包名
// }
//ksp {
// arg("rxhttp_rxjava", rxjava_version)
//// arg("rxhttp_package", "rxhttp")
//}

kapt {
arguments {
arg("rxhttp_rxjava", rxjava_version) //可传入rxjava2、rxjava3或具体版本号,如 3.1.1
// arg("rxhttp_package", "rxhttp") //设置RxHttp相关类的包名,多module依赖时,需要配置不同的包名
}
}


//configurations.all {
// resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
Expand All @@ -85,8 +85,8 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"

implementation project(":rxhttp")
ksp project(':rxhttp-compiler')
// kapt project(':rxhttp-compiler')
// ksp project(':rxhttp-compiler')
kapt project(':rxhttp-compiler')
// implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"

implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ org.gradle.jvmargs=-Xmx1536m
okhttp_version=4.10.0
rxjava_version=3.1.5
rxlife_version=2.2.2
rxhttp_version=3.0.0-rc8
rxhttp_version=3.0.0

lifecycle_version=2.5.0
ksp_version=1.7.20-1.0.7
Expand Down
4 changes: 2 additions & 2 deletions maven_dependency.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<dependency>
<groupId>com.github.liujingxing.rxhttp</groupId>
<artifactId>rxhttp</artifactId>
<version>2.9.5</version>
<version>3.0.0</version>
</dependency>

<!-- 非必须 RxJava2/RxJava3 二选一或都不选 -->
Expand Down Expand Up @@ -48,7 +48,7 @@
<path>
<groupId>com.github.liujingxing.rxhttp</groupId>
<artifactId>rxhttp-compiler</artifactId>
<version>2.9.5</version>
<version>3.0.0</version>
</path>
</annotationProcessorPaths>

Expand Down

0 comments on commit 8421614

Please sign in to comment.