RxHttp Documentation

repository·master·Indexed 26 days ago

https://github.com/liujingxing/rxhttp

A type-safe HTTP client for Android built on OkHttp. It supports Kotlin Coroutines (Await/Flow), RxJava2, and RxJava3, with integration for various data parsing tools including Gson, ProtoBuf, FastJson, Jackson, Moshi, and SimpleXML. Features include automatic request closure via RxLife, customizable generated package names, and specialized output stream factories for file and Uri-based downloads.

Tokens
4.1K
Snippets
12
Records
19
Agent score
87%

What's inside RxHttp

  1. Install RxHttp via kapt

    master

    To use the kapt method, apply the kotlin-kapt plugin and ensure your project uses Java 8 or higher with the JitPack repository.

    // 1. Project build.gradle
    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    
    // 2. Android build.gradle
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    // 3. Plugins and Dependencies
    plugins {
        id 'kotlin-kapt'
    }
     
    dependencies {
        def rxhttp_version = '3.5.1'
        implementation 'com.squareup.okhttp3:okhttp:4.12.0'  
        implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
        kapt "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
    }
  2. Configure RxHttp with RxJava

    master

    If you are using RxJava, you must include the appropriate RxJava and RxAndroid dependencies. Additionally, you should use rxlife for automatic request closure.

    You can pass the specific RxJava version to the RxHttp compiler using ksp, kapt, or javaCompileOptions to ensure compatibility.

    // RxJava3 Setup
    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'
    
    // Pass RxJava version to KSP
    ksp {
        arg("rxhttp_rxjava", "3.1.6")
    }
  3. Install RxHttp

    master

    To use RxHttp in your Android project, follow these steps:

    1. Add the JitPack repository to your build.gradle file.
    2. Ensure your project uses Java 8 or higher.
    3. Add the RxHttp dependencies and the compiler (using KSP, Kapt, or annotationProcessor).

    Note: You must rebuild the project after adding these dependencies.

    // 1. Add jitpack to your build.gradle
    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    
    // 2. Java 8 or higher
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    // 3. Add RxHttp dependency
    plugins {
        id 'com.google.devtools.ksp' version '2.3.4'
    }
        
    dependencies {
        def rxhttp_version = '3.5.1'
        implementation 'com.squareup.okhttp3:okhttp:4.12.0'  
        implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
        ksp "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
     }
  4. Add RxHttp Converters

    master

    RxHttp supports various third-party data parsing tools via optional converter modules. Add the one that matches your preferred serialization library.

    // Example converters
    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"
  5. Install RxHttp via ksp

    master

    To use the ksp method, apply the com.google.devtools.ksp plugin and ensure your project uses Java 8 or higher with the JitPack repository.

    // 1. Project build.gradle
    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    
    // 2. Android build.gradle
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    // 3. Plugins and Dependencies
    plugins {
        id 'com.google.devtools.ksp' version '2.3.4'
    }
     
    dependencies {
        def rxhttp_version = '3.5.1'
        implementation 'com.squareup.okhttp3:okhttp:4.12.0'  
        implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
        ksp "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
    }
  6. Install RxHttp via annotationProcessor

    master

    To use the annotationProcessor method, ensure your project uses Java 8 or higher and includes the JitPack repository.

    Note: RxHttp is compatible with OkHttp 3.12.0 - v4.12.0 (excluding 4.3.0). For compatibility with API levels below 21, use OkHttp 3.12.x.

    // 1. Project build.gradle
    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
        }
    }
    
    // 2. Android build.gradle (Java 8 requirement)
    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    
    // 3. Dependencies
    dependencies {
        def rxhttp_version = '3.5.1'
        implementation 'com.squareup.okhttp3:okhttp:4.12.0'  
        implementation "com.github.liujingxing.rxhttp:rxhttp:$rxhttp_version"
        annotationProcessor "com.github.liujingxing.rxhttp:rxhttp-compiler:$rxhttp_version"
    }
  7. Configure RxJava support

    master

    To use methods like toObservableXxx, you must add an RxJava dependency (either RxJava2 or RxJava3) and inform RxHttp of the version you are using via your annotation processor (ksp, kapt, or annotationProcessor).

    // RxJava3 dependencies
    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'
    
    // RxJava2 dependencies
    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'
    
    // Pass RxJava version via KSP
    ksp {
        arg("rxhttp_rxjava", "3.1.6")
    }
    
    // Pass RxJava version via KAPT
    kapt {
        arguments {
            arg("rxhttp_rxjava", "3.1.6")
        }
    }
    
    // Pass RxJava version via annotationProcessor
    android {
        defaultConfig {
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                        rxhttp_rxjava: '3.1.6',
                    ]
                }
            }
        }
    }
  8. Configure Converters

    master

    RxHttp includes a default GsonConverter. If you prefer other serialization libraries, add the corresponding dependency. Choose only one converter from the following list:

    // Choose one based on your needs
    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"
  9. Customize RxHttp class package name

    master

    If you have multiple modules depending on rxhttp-compiler, you may encounter class conflicts because RxHttp classes are generated in the same default directory. To avoid this, customize the package name for the generated RxHttp classes using rxhttp_package via ksp, kapt, or annotationProcessor. After configuration, you must rebuild the project.

    // Using KSP
    ksp {
        arg("rxhttp_package", "rxhttp")
    }
    
    // Using KAPT
    kapt {
        arguments {
            arg("rxhttp_package", "rxhttp")
        }
    }
    
    // Using annotationProcessor
    android {
        defaultConfig {
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                        rxhttp_package: 'rxhttp',
                    ]
                }
            }
        }
    }
  10. Configure RxHttp Package Name

    master

    You can customize the generated RxHttp class package name by passing the rxhttp_package argument to your annotation processor (KSP, Kapt, or javaCompileOptions).

    // Using KSP
    ksp {
         arg("rxhttp_package", "rxhttp.xxx")
    }
    
    // Using javaCompileOptions
    android {
        defaultConfig {
            javaCompileOptions {
                annotationProcessorOptions {
                    arguments = [
                        rxhttp_package: 'rxhttp.xxx'
                    ]
                }
            }
        }
    }
  11. Configure ProGuard/R8 obfuscation

    master

    Obfuscation requirements depend on your RxHttp version:

    • RxHttp v2.2.8 and above: No specific RxHttp rules are required. You only need to add -keep rules for your own Bean classes.
    • RxHttp below v2.2.8: You must add the official RxHttp obfuscation rules to your project in addition to keeping your Bean classes.