在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):liujingxing/rxhttp开源软件地址(OpenSource Url):https://github.com/liujingxing/rxhttp开源编程语言(OpenSource Language):Kotlin 67.3%开源软件介绍(OpenSource Introduction):RxHttpEnglish | 中文文档 A type-safe HTTP client for Android. Written based on OkHttp //Kotlin + Await //Kotlin + Flow //Kotlin + RxJava //Java + RxJava
RxHttp.get("/server/..") RxHttp.get("/server/..") RxHttp.get("/server/..") RxHttp.get("/server/..")
.add("key", "value") .add("key", "value") .add("key", "value") .add("key", "value")
.toClass<User>() .toFlow<User>() .asClass<User>() .asClass(User.class)
.awaitResult { .catch { .subscribe({ .subscribe(user -> {
//Success //Failure //Success //Success
}.onFailure { }.collect { }, { }, throwable -> {
//Failure //Success //Failure //Failure
} } }) }); 1、Feature
2、usage1、Adding dependencies and configurations Required1、Add jitpack to your build.gradleallprojects {
repositories {
maven { url "https://jitpack.io" }
}
} 2、Java 8 or higherandroid {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
} 3、Add RxHttp dependencyplugins {
// kapt/ksp choose one
// id 'kotlin-kapt'
id 'com.google.devtools.ksp' version '1.7.0-1.0.6'
}
//Make IDE aware of generated code if you use ksp
kotlin {
sourceSets.debug {
kotlin.srcDir("build/generated/ksp/debug/kotlin")
}
}
dependencies {
def rxhttp_version = '2.8.9'
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
// ksp/kapt/annotationProcessor choose one
ksp "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
} Optional1、Coverterimplementation "com.github.liujingxing.rxhttp:converter-fastjson:$rxhttp_version"
implementation "com.github.liujingxing.rxhttp:converter-jackson:$rxhttp_version"
implementation "com.github.liujingxing.rxhttp:converter-moshi:$rxhttp_version"
implementation "com.github.liujingxing.rxhttp:converter-protobuf:$rxhttp_version"
implementation "com.github.liujingxing.rxhttp:converter-simplexml:$rxhttp_version" 2、RxJavaRxHttp + RxJava3implementation 'io.reactivex.rxjava3:rxjava:3.1.5'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'com.github.liujingxing.rxlife:rxlife-rxjava3:2.2.2' //RxJava3, Automatic close request RxHttp + RxJava2implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.github.liujingxing.rxlife:rxlife-rxjava2:2.2.2' //RxJava2, Automatic close request ksp passes the RxJava versionksp {
arg("rxhttp_rxjava", "3.1.5")
} Kapt passes the RxJava versionkapt {
arguments {
arg("rxhttp_rxjava", "3.1.5")
}
} javaCompileOptions passes the RxJava versionandroid {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
rxhttp_rxjava: '3.1.5',
]
}
}
}
} 3、set RxHttp class package nameksp pass package nameksp {
arg("rxhttp_package", "rxhttp.xxx")
} kapt pass package namekapt {
arguments {
arg("rxhttp_package", "rxhttp.xxx")
}
} javaCompileOptions pass package nameandroid {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
rxhttp_package: 'rxhttp.xxx'
]
}
}
}
} Finally, rebuild the project, which is necessary 2、Initialize the SDK This step is optional RxHttpPlugins.init(OkHttpClient)
.setDebug(boolean)
.setOnParamAssembly(Function)
.... 3、Configuration BaseUrl This step is optional public class Url {
//Add the @defaultDomain annotation to BASE_URL
@DefaultDomain
public static BASE_URL = "https://..."
} 4、Perform the requested // java
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
.subscribe(student -> { //3、Subscribing observer
//Success callback,Default IO thread
}, throwable -> {
//Abnormal callback
});
// kotlin
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>()
.subscribe({ student ->
//Default IO thread
}, { throwable ->
})
// 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
.await() //3、Get the return value, await is the suspend method 3、Advanced usage1、Close the request //In Rxjava2 , Automatic close request
RxHttp.get("/service/...")
.asString()
.as(RxLife.as(this)) //The Activity destroys and automatically closes the request
.subscribe(s -> {
//Default IO thread
}, throwable -> {
});
//In Rxjava3 , Automatic close request
RxHttp.get("/service/...")
.asString()
.to(RxLife.to(this)) //The Activity destroys and automatically closes the request
.subscribe(s -> {
//Default IO thread
}, throwable -> {
});
//In RxJava2/RxJava3, close the request manually
Disposable disposable = RxHttp.get("/service/...")
.asString()
.subscribe(s -> {
//Default IO thread
}, throwable -> {
});
disposable.dispose(); //Close the request at the appropriate time 4、ProGuardIf you are using RxHttp v2.2.8 or above the shrinking and obfuscation rules are included automatically. Otherwise you must manually add the options in rxhttp.pro. 5、DonationsIf this project helps you a lot and you want to support the project's development and maintenance of this project, feel free to scan the following QR code for donation. Your donation is highly appreciated. Thank you! Licenses
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论