接收http
3.3.1
英語 | 中文文檔
適用於 Android 的類型安全 HTTP 用戶端。基於OkHttp編寫
等待 | 流動 | 接收Java (科特林) | 接收Java (爪哇) |
---|---|---|---|
()
.await()
//or awaitResult return kotlin.Result //await return User //tryAwait return User? val user = RxHttp . get ( "/server/.." ) . add ( "key" , "value" ) . toAwait < User >() . await () //or awaitResult return kotlin.Result | ()
.catch {
//Failure
}.collect {
//Success
} "> RxHttp . get ( "/server/.." ) . add ( "key" , "value" ) . toFlow < User >() . catch { //Failure }. collect { //Success } | ()
.subscribe({
//Success
}, {
//Failure
}) "> RxHttp . get ( "/server/.." ) . add ( "key" , "value" ) . toObservable < User >() . subscribe ({ //Success }, { //Failure }) | {
//Success
}, throwable -> {
//Failure
}) "> RxHttp . get ( "/server/.." ) . add ( "key" , "value" ) . toObservable ( User . class ) . subscribe ( user - > { //Success }, throwable -> { //Failure }) |
支援kotlin協程、RxJava2、RxJava3
支援Gson、Xml、ProtoBuf、FastJson等第三方資料解析工具
支援自動關閉FragmentActivity、Fragment、View、ViewModel以及任何類別中的請求
支援全域加解密,新增通用參數和標頭,網路緩存,均支援一次請求單獨設置
1、新增依賴和配置
allprojects {
repositories {
maven { url "https://jitpack.io" }
}
}
android {
compileOptions {
sourceCompatibility JavaVersion . VERSION_1_8
targetCompatibility JavaVersion . VERSION_1_8
}
}
plugins {
// kapt/ksp choose one
// id 'kotlin-kapt'
id ' com.google.devtools.ksp ' version ' 1.9.23-1.0.19 '
}
dependencies {
def rxhttp_version = ' 3.3.0 '
implementation ' com.squareup.okhttp3:okhttp:4.12.0 '
implementation " com.github.liujingxing.rxhttp:rxhttp: $rxhttp_version "
// ksp/kapt/annotationProcessor choose one
ksp " com.github.liujingxing.rxhttp:rxhttp-compiler: $rxhttp_version "
}
implementation " com.github.liujingxing.rxhttp:converter-serialization: $rxhttp_version "
implementation " 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 "
implementation 'io.reactivex.rxjava3:rxjava:3.1.6'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.2'
implementation 'com.github.liujingxing.rxlife:rxlife-rxjava3:2.2.2' //RxJava3, Automatic close request
implementation '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 {
arg ( "rxhttp_rxjava" , "3.1.6" )
}
kapt {
arguments {
arg ( "rxhttp_rxjava" , "3.1.6" )
}
}
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
rxhttp_rxjava : '3.1.6' ,
]
}
}
}
}
ksp {
arg ( "rxhttp_package" , "rxhttp.xxx" )
}
kapt {
arguments {
arg ( "rxhttp_package" , "rxhttp.xxx" )
}
}
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = [
rxhttp_package : 'rxhttp.xxx'
]
}
}
}
}
最後重建項目,這是必須的
2、初始化SDK
此步驟是可選的
RxHttpPlugins . init ( OkHttpClient )
. setDebug ( boolean )
. setOnParamAssembly ( Consumer )
....
3、配置BaseUrl
此步驟是可選的
public class Url {
//Add the @defaultDomain annotation to BASE_URL
@ DefaultDomain
public static BASE_URL = "https://..."
}
4、執行要求的操作
// java RxHttp . get ( "/service/..." ) //1、You can choose get,postFrom,postJson etc . addQuery ( "key" , "value" ) //add query param . addHeader ( "headerKey" , "headerValue" ) //add request header . toObservable ( Student . class ) //2、Use the toXxx 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 . toObservable < Student >() . subscribe ({ student -> //Default IO thread }, { throwable -> }) // kotlin coroutine val students = RxHttp . postJson ( "/service/..." ) //1、post {application/json; charset=utf-8} . 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
1、關閉請求
//In Rxjava2 , Automatic close request
RxHttp . get ( "/service/..." )
. toObservableString ()
. 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/..." )
. toObservableString ()
. 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/..." )
. toObservableString ()
. subscribe ( s -> {
//Default IO thread
}, throwable -> {
});
disposable . dispose (); //Close the request at the appropriate time
如果您使用 RxHttp v2.2.8 或更高版本,則會自動包含收縮和混淆規則。否則,您必須在 rxhttp.pro 中手動新增選項。
如果這個專案對您有很大幫助,並且您想支持該專案的開發和維護,請隨時掃描以下二維碼進行捐贈。非常感謝您的捐贈。謝謝你!
Copyright 2019 liujingxing
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.