Giter Site home page Giter Site logo

ning736661787 / okhttp-rxhttp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liujingxing/rxhttp

0.0 0.0 0.0 8.6 MB

🔥🔥🔥 Based on OkHttp encapsulation, support Kotlin Coroutines、RxJava2、RxJava3; 30s to get started.

Home Page: https://juejin.im/post/5ded221a518825125d14a1d4

License: Apache License 2.0

Java 46.97% Kotlin 53.03%

okhttp-rxhttp's Introduction

Download

RxHttp主要优势

1. 30秒即可上手,学习成本极低

2. 史上最优雅的支持 Kotlin 协程

3. 史上最优雅的处理多个BaseUrl及动态BaseUrl

4. 史上最优雅的对错误统一处理,且不打破Lambda表达式

5. 史上最优雅的文件上传/下载/断点下载/进度监听,已适配Android 10

6. 支持Gson、Xml、ProtoBuf、FastJson等第三方数据解析工具

7. 支持Get、Post、Put、Delete等任意请求方式,可自定义请求方式

8. 支持在Activity/Fragment/View/ViewModel/任意类中,自动关闭请求

9. 支持全局加解密、添加公共参数及头部、网络缓存,均支持对某个请求单独设置

请求三部曲

image

上手教程

30秒上手教程:30秒上手新一代Http请求神器RxHttp

协程文档:RxHttp ,比Retrofit 更优雅的协程体验

掘金详细文档:RxHttp 让你眼前一亮的Http请求框架

wiki详细文档:https://github.com/liujingxing/okhttp-RxHttp/wiki (此文档会持续更新)

自动关闭请求用到的RxLife类,详情请查看RxLife库

更新日志      已知问题     

遇到问题,点击这里,99%的问题都能自己解决

上手准备

RxHttp&RxLife 交流群:378530627

Maven依赖点击这里

1、OkHttp 3.14.x以上版本, 最低要求为API 21,如你想要兼容21以下,请依赖OkHttp 3.12.x,该版本最低要求 API 9

2、asXxx方法内部是通过RxJava实现的,而RxHttp 2.2.0版本起,内部已剔除RxJava,如需使用,请自行依赖RxJava并告知RxHttp依赖的Rxjava版本

//使用kapt依赖rxhttp-compiler,需要导入kapt插件
apply plugin: 'kotlin-kapt'

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                    //必须,告知RxHttp你依赖的okhttp版本,目前已适配 v3.12.0 - v4.9.0版本  (v4.3.0除外)
                    rxhttp_okhttp: '4.9.0'//使用asXxx方法时必须,告知RxHttp你依赖的rxjava版本,可传入rxjava2、rxjava3
                    rxhttp_rxjava: 'rxjava3'rxhttp_package: 'rxhttp'   //非必须,指定RxHttp类包名
                ]
            }
        }
    }
    //必须,java 8或更高
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
dependencies {
    //以下3个为必须,
    implementation 'com.ljx.rxhttp:rxhttp:2.4.1'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0' //rxhttp v2.2.2版本起,需要手动依赖okhttp
    kapt 'com.ljx.rxhttp:rxhttp-compiler:2.4.1' //生成RxHttp类,纯Java项目,请使用annotationProcessor代替kapt
    
    implementation 'com.ljx.rxlife:rxlife-coroutine:2.0.1' //管理协程生命周期,页面销毁,关闭请求
    
    //rxjava2   (RxJava2/Rxjava3二选一,使用asXxx方法时必须)
    implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'com.ljx.rxlife2:rxlife-rxjava:2.0.0' //管理RxJava2生命周期,页面销毁,关闭请求

    //rxjava3
    implementation 'io.reactivex.rxjava3:rxjava:3.0.6'
    implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
    implementation 'com.ljx.rxlife3:rxlife-rxjava:3.0.0' //管理RxJava3生命周期,页面销毁,关闭请求

    //非必须,根据自己需求选择 RxHttp默认内置了GsonConverter
    implementation 'com.ljx.rxhttp:converter-fastjson:2.4.1'
    implementation 'com.ljx.rxhttp:converter-jackson:2.4.1'
    implementation 'com.ljx.rxhttp:converter-moshi:2.4.1'
    implementation 'com.ljx.rxhttp:converter-protobuf:2.4.1'
    implementation 'com.ljx.rxhttp:converter-simplexml:2.4.1'
}

最后,rebuild一下(此步骤是必须的) ,就会自动生成RxHttp类

混淆

RxHttp v2.2.8版本起,无需添加混淆规则(内部自带混淆规则),v2.2.8以下版本,在proguard-rules.pro文件添加以下代码

# okhttp 4.7.0及以上版本混淆规则
-keepclassmembers class okhttp3.internal.Util {
    public static java.lang.String userAgent;
}

# okhttp 4.7.0以下版本混淆规则
-keepclassmembers class okhttp3.internal.Version {
    # 4.0.0<=version<4.7.0
    public static java.lang.String userAgent;
    # version<4.0.0
    public static java.lang.String userAgent();
}
# okhttp 4.0.0以下版本混淆规则
-keepclassmembers class okhttp3.internal.http.StatusLine {
    public static okhttp3.internal.http.StatusLine parse(java.lang.String);
}

小技巧

在这教大家一个小技巧,由于使用RxHttp发送请求都遵循请求三部曲,故我们可以在android studio 设置代码模版,如下

image

如图设置好后,写代码时,输入rp,就会自动生成模版,如下:

image

Demo演示

更多功能,请下载Demo体验

Donations

如果它对你帮助很大,并且你很想支持库的后续开发和维护,那么你可以扫下方二维码随意打赏我,就当是请我喝杯咖啡或是啤酒,开源不易,感激不尽

image

Licenses

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.

okhttp-rxhttp's People

Contributors

liujingxing avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.