# RetrofitUrlManager **Repository Path**: archermind-ti/retrofit-url-manager ## Basic Information - **Project Name**: RetrofitUrlManager - **Description**: Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 3 - **Forks**: 1 - **Created**: 2021-04-14 - **Last Updated**: 2022-09-06 ## Categories & Tags **Categories**: harmonyos-network **Tags**: None ## README # RetrofitUrlManager ## Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime. #### Overview ![overview](screenshot/overview.gif) #### 项目介绍 - 以最简洁的 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 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. ```