1 Star 3 Fork 1

Archermind-TI / RetrofitUrlManager

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 2.79 KB
一键复制 编辑 原始数据 按行查看 历史
kk90 提交于 2021-07-03 03:13 . update README.md.

RetrofitUrlManager

Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime.

Overview

overview

项目介绍

  • 以最简洁的 Api 让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl

集成

方法1: 直接使用har包
通过library生成har包,添加har包到要集成的libs文件夹内
在entry的gradle内添加如下代码
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
方法2: gradle依赖
allprojects{
    repositories{
        mavenCentral()
    }
}
implementation "com.gitee.archermind-ti:retrofit-url-manager:1.0.0-beta"

使用说明

1. 初始化
// When building OkHttpClient, the OkHttpClient.Builder() is passed to the with() method to initialize the configuration
 this.mOkHttpClient = RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder())
              .readTimeout(5, TimeUnit.SECONDS)
              .connectTimeout(5, TimeUnit.SECONDS)
              .build();
2. 步骤一
  public interface OneApiService {
  /**
   * 如果不需要多个 BaseUrl, 继续使用初始化时传入 Retrofit 中的默认 BaseUrl, 就不要加上 DOMAIN_NAME_HEADER 这个 Header
   */
  @Headers({DOMAIN_NAME_HEADER + GITHUB_DOMAIN_NAME})
  /**
   * 可以通过在注解里给全路径达到使用不同的 BaseUrl, 但是这样无法在 App 运行时动态切换 BaseUrl
   */
  @GET("/users")
  Observable<ResponseBody> getUsers(@Query("since") int lastIdQueried, @Query("per_page") int perPage);
3. 步骤二
 // You can change BaseUrl at any time while App is running (The interface that declared the Domain-Name header)
  String APP_DOUBAN_DOMAIN = "https://api.douban.com";
  RetrofitUrlManager.getInstance().putDomain(DOUBAN_DOMAIN_NAME, APP_DOUBAN_DOMAIN);
4. 如果要更改全局BaseUrl
// BaseUrl configured in the Domain-Name header will override BaseUrl in the global setting
 mGlobalUrl = (TextField) findComponentById(ResourceTable.Id_et_global_url);
 RetrofitUrlManager.getInstance().setGlobalDomain(mGlobalUrl.getText().toString().trim());

版权和许可信息

Copyright 2017, jessyan

   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.
1
https://gitee.com/archermind-ti/retrofit-url-manager.git
git@gitee.com:archermind-ti/retrofit-url-manager.git
archermind-ti
retrofit-url-manager
RetrofitUrlManager
master

搜索帮助