All Projects → jenly1314 → RetrofitHelper

jenly1314 / RetrofitHelper

Licence: MIT license
💪 RetrofitHelper是一个支持配置多个BaseUrl,支持动态改变BaseUrl,动态配置超时时长的Retrofit帮助类

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to RetrofitHelper

Retrofiturlmanager
🔮 Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime (以最简洁的 Api 让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl).
Stars: ✭ 1,961 (+12973.33%)
Mutual labels:  retrofit, okhttp, baseurl, retrofit2, baseurl-domain
WanAndroid
wanandroid的Kotlin版,采用Android X
Stars: ✭ 20 (+33.33%)
Mutual labels:  retrofit, okhttp, retrofit2
Mvvmhabit
goldze: 本人喜欢尝试新的技术,以后发现有好用的东西,我将会在企业项目中实战,没有问题了就会把它引入到MVVMHabit中,一直维护着这套框架,谢谢各位朋友的支持。如果觉得这套框架不错的话,麻烦点个 star,你的支持则是我前进的动力!
Stars: ✭ 6,789 (+45160%)
Mutual labels:  retrofit, okhttp, retrofit2
Easyhttp
Android 网络请求框架,简单易用,so easy
Stars: ✭ 423 (+2720%)
Mutual labels:  retrofit, okhttp, retrofit2
RxHttp
基于RxJava2+Retrofit+OkHttp4.x封装的网络请求类库,亮点多多,完美兼容MVVM(ViewModel,LiveData),天生支持网络请求和生命周期绑定,天生支持多BaseUrl,支持文件上传下载进度监听,支持断点下载,支持Glide和网络请求公用一个OkHttpClient⭐⭐⭐
Stars: ✭ 25 (+66.67%)
Mutual labels:  retrofit, okhttp, retrofit2
RxHttp
对RxJava2+Retrofit2+OkHttp3的封装
Stars: ✭ 65 (+333.33%)
Mutual labels:  retrofit, okhttp
StatefulLiveData
StatefulLiveData is a lean, yet powerful tool that harnesses the capabilities of LiveData and enhances them, enabling the observer to distinguish between different states the data can be in, such as Success, Loading and Error.
Stars: ✭ 18 (+20%)
Mutual labels:  retrofit, retrofit2
Android-Model-View-Presenter
No description or website provided.
Stars: ✭ 26 (+73.33%)
Mutual labels:  retrofit, okhttp
AvengersChat
💙 Android sample Avengers chat application using Stream Chat SDK based on MVVM (ViewModel, Coroutines, Room, Hilt, Repository) architecture.
Stars: ✭ 350 (+2233.33%)
Mutual labels:  retrofit, okhttp
MVPHulk
Android MVP 快速集成方案 (支持AndroidX)
Stars: ✭ 19 (+26.67%)
Mutual labels:  retrofit, okhttp
android-clean-architecture
🚀🚀🚀 The boilerplate for Android using Kotlin & Clean architecture.
Stars: ✭ 21 (+40%)
Mutual labels:  retrofit, okhttp
RestaurantsExplorer
Android application build with MVVM Pattern, using Zomato API to enable search cities arround the world and display the city restaurants on a map.
Stars: ✭ 32 (+113.33%)
Mutual labels:  retrofit, okhttp
Retrosheet
Turn Google Spreadsheet to JSON endpoint (for Android and JVM) for FREE (100%)
Stars: ✭ 245 (+1533.33%)
Mutual labels:  retrofit, okhttp
weather
基于MVP的安卓天气demo
Stars: ✭ 49 (+226.67%)
Mutual labels:  okhttp, retrofit2
Okhttp Json Mock
Mock your datas for Okhttp and Retrofit in json format in just a few moves
Stars: ✭ 231 (+1440%)
Mutual labels:  retrofit, okhttp
Progressmanager
⏳ Listen the progress of downloading and uploading in Okhttp, compatible Retrofit and Glide (一行代码即可监听 App 中所有网络链接的上传以及下载进度, 包括 Glide 的图片加载进度).
Stars: ✭ 2,463 (+16320%)
Mutual labels:  retrofit, okhttp
rxjava2 retrofit2
rxjava2+retrofit2 网络封装
Stars: ✭ 19 (+26.67%)
Mutual labels:  retrofit, retrofit2
Kriptofolio
Free open source minimalistic cryptocurrencies portfolio app for Android.
Stars: ✭ 79 (+426.67%)
Mutual labels:  retrofit, okhttp
iMoney
iMoney 金融项目
Stars: ✭ 55 (+266.67%)
Mutual labels:  okhttp, retrofit2
Android Kotlin Mvi Cleanarchitecture
Android + Kotlin + Modularization + Gradle Depedency managment + Gradle written in Kotlin DSL + Custom Gradle Plugin + MVVM + MVI + Clean Architecture + Repository Pattern + Coroutines + Flows + Koin + Retrofit2 + ROOM + Kotlin-Android-Extension + KtLints
Stars: ✭ 187 (+1146.67%)
Mutual labels:  retrofit, retrofit2

RetrofitHelper

Image

Download JCenter MavenCentral JitPack CI CircleCI API License Blog QQGroup

RetrofitHelper for Android 是一个为 Retrofit 提供便捷配置多个BaseUrl相关的扩展帮助类。

主要功能介绍

  • 支持配置多个BaseUrl
  • 支持动态改变BaseUrl
  • 支持动态配置超时时长
  • 支持添加公共请求头

Gif 展示

Image

引入

由于2021年2月3日 JFrog宣布将关闭Bintray和JCenter,计划在2022年2月完全关闭。 所以后续版本不再发布至 JCenter

  1. 在Project的 build.gradle 里面添加远程仓库
allprojects {
    repositories {
        //...
        mavenCentral()
    }
}
  1. 在Module的 build.gradle 里面添加引入依赖项
//AndroidX 版本
implementation 'com.github.jenly1314:retrofit-helper:1.0.1'

RetrofitHelper引入的库(具体对应版本请查看 Versions

    compileOnly "androidx.appcompat:appcompat:$versions.appcompat"
    compileOnly "com.squareup.retrofit2:retrofit:$versions.retrofit"

因为 RetrofitHelper 的依赖只在编译时有效,并未打入包中,所以您的项目中必须依赖上面列出相关库

示例

主要集成步骤代码示例

Step.1 需使用JDK8编译,在你项目中的build.gradle的android{}中添加配置:

compileOptions {
    targetCompatibility JavaVersion.VERSION_1_8
    sourceCompatibility JavaVersion.VERSION_1_8
}

Step.2 通过RetrofitUrlManager初始化OkHttpClient,进行初始化配置

//通过RetrofitHelper创建一个支持多个BaseUrl的 OkHttpClient
//方式一
val clientBuilder = RetrofitHelper.getInstance()
            .createClientBuilder()
            //...你自己的其他配置
            
//方式二
val okHttpClient = RetrofitHelper.getInstance()
            .with(clientBuilder)
            //...你自己的其他配置
            .build()
//完整示例
val okHttpClient = RetrofitHelper.getInstance()
            .createClientBuilder()
            .addInterceptor(LogInterceptor())
            .build()
val retrofit = Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create(Gson())) 
            .build()

Step.3 定义接口时,通过注解标记对应接口,支持动态改变 BaseUrl相关功能

 interface ApiService {
    
     /**
      * 接口示例,没添加任何标识,和常规使用一致
      * @return
      */
     @GET("api/user")
     fun getUser(): Call<User>


     /**
      * Retrofit默认返回接口定义示例,添加 DomainName 标示 和 Timeout 标示
      * @return
      */
     @DomainName(domainName) // domainName 域名别名标识,用于支持切换对应的 BaseUrl
     @Timeout(connectTimeout = 15,readTimeout = 15,writeTimeout = 15,timeUnit = TimeUnit.SECONDS) //超时标识,用于自定义超时时长
     @GET("api/user")
     fun getUser(): Call<User>

     /**
      * 动态改变 BaseUrl
      * @return
      */
     @BaseUrl(baseUrl) //baseUrl 标识,用于支持指定 BaseUrl
     @GET("api/user")
     fun getUser(): Call<User>


     //--------------------------------------

     /**
      * 使用RxJava返回接口定义示例,添加 DomainName 标示 和 Timeout 标示
      * @return
      */
     @DomainName(domainName) // domainName 域名别名标识,用于支持切换对应的 BaseUrl
     @Timeout(connectTimeout = 20,readTimeout = 10) //超时标识,用于自定义超时时长
     @GET("api/user")
     fun getUser(): Observable<User>

 }

Step.4 添加多个 BaseUrl 支持

        //添加多个 BaseUrl 支持 ,domainName为域名别名标识,domainUrl为域名对应的 BaseUrl,与上面的接口定义表示一致即可生效
        RetrofitHelper.getInstance().putDomain(domainName,domainUrl)
        //添加多个 BaseUrl 支持 示例
        RetrofitHelper.getInstance().apply {
            //GitHub baseUrl
            putDomain(Constants.DOMAIN_GITHUB,Constants.GITHUB_BASE_URL)
            //Google baseUrl
            putDomain(Constants.DOMAIN_GOOGLE,Constants.GOOGLE_BASE_URL)
        }

RetrofitHelper

/**
 * Retrofit帮助类
 * <p>
 * 主要功能介绍:
 *      1.支持管理多个 BaseUrl,且支持运行时动态改变
 *      2.支持接口自定义超时时长,满足每个接口动态定义超时时长
 *      3.支持添加公共请求头
 * <p>
 *
 * RetrofitHelper中的核心方法
 *
 * {@link #createClientBuilder()} 创建 {@link OkHttpClient.Builder}初始化一些配置参数,用于支持多个 BaseUrl
 *
 * {@link #with(OkHttpClient.Builder)} 传入 {@link OkHttpClient.Builder} 配置一些参数,用于支持多个 BaseUrl
 *
 * {@link #setBaseUrl(String)} 和 {@link #setBaseUrl(HttpUrl)} 主要用于设置默认的 BaseUrl。
 *
 * {@link #putDomain(String, String)} 和 {@link #putDomain(String, HttpUrl)} 主要用于支持多个 BaseUrl,且支持 BaseUrl 动态改变。
 *
 * {@link #setDynamicDomain(boolean)} 设置是否支持 配置多个BaseUrl,且支持动态改变,一般会通过其他途径自动开启,此方法一般不会主动用到,只有在特殊场景下可能会有此需求,所以提供此方法主要用于提供更多种可能。
 *
 * {@link #setHttpUrlParser(HttpUrlParser)} 设置 HttpUrl解析器 , 当前默认采用的 {@link DomainParser} 实现类,你也可以自定义实现 {@link HttpUrlParser}
 *
 * {@link #setAddHeader(boolean)} 设置是否添加头,一般会通过{@link #addHeader(String, String)}相关方法自动开启,此方法一般不会主动用到,只有特殊场景下会有此需求,主要用于提供统一控制。
 *
 * {@link #addHeader(String, String)} 设置头,主要用于添加公共头消息。
 *
 * {@link #addHeaders(Map)} 设置头,主要用于设置公共头消息。
 *
 * 这里只是列出一些对外使用的核心方法,和相关的简单说明。如果想了解更多,可以查看对应的方法和详情。
 *
 * <p>
 *
 * @author <a href="mailto:[email protected]">Jenly</a>
 */
public final class RetrofitHelper{
    //...
}

特别说明

        //通过setBaseUrl可以动态改变全局的 BaseUrl,优先级比putDomain(domainName,domainUrl)低,谨慎使用
        RetrofitHelper.getInstance().setBaseUrl(dynamicUrl)

更多使用详情,请查看Demo中的源码使用示例或直接查看API帮助文档

版本记录

v1.0.1:2021-4-23

  • 新增支持添加公共请求头
  • 新增 @BaseUrl 注解

v1.0.0:2020-5-30

  • RetrofitHelper初始版本

赞赏

如果你喜欢RetrofitHelper,或感觉RetrofitHelper帮助到了你,可以点右上角“Star”支持一下,你的支持就是我的动力,谢谢 😃

你也可以扫描下面的二维码,请作者喝杯咖啡

关于我

Name: Jenly

Email: jenly1314#gmail.com / jenly1314#vip.qq.com

CSDN: jenly121

CNBlogs: jenly

GitHub: jenly1314

Gitee: jenly1314

加入QQ群: 20867961

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].