Skip to content

Commit

Permalink
优化RxUrlManager(动态切换全局baseUrl更方便)
Browse files Browse the repository at this point in the history
  • Loading branch information
lygttpod committed Apr 21, 2019
1 parent 327c2be commit 1ef7ff6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion 14
targetSdkVersion 27
versionCode 232
versionName "2.3.2"
versionName "2.3.3"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
67 changes: 57 additions & 10 deletions library/src/main/java/com/allen/library/manage/RxUrlManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.allen.library.manage;

import com.allen.library.RxHttpUtils;
import com.allen.library.factory.ApiFactory;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -36,35 +37,81 @@ private RxUrlManager() {
urlMap = new HashMap<>();
}

public void setUrl(String urlValue) {
urlMap.put(DEFAULT_URL_KEY, urlValue);
}

public void setMultipleUrl(Map<String, String> urlMap) {
/**
* 一次性传入urlMap
*
* @param urlMap map
* @return RxUrlManager
*/
public RxUrlManager setMultipleUrl(Map<String, String> urlMap) {
this.urlMap = urlMap;
return this;
}

public void addUrl(String urlKey, String urlValue) {
/**
* 向map中添加url
*
* @param urlKey key
* @param urlValue value
* @return RxUrlManager
*/
public RxUrlManager addUrl(String urlKey, String urlValue) {
urlMap.put(urlKey, urlValue);
return this;
}

public void removeUrl(String urlKey) {
/**
* 从map中删除某个url
*
* @param urlKey 需要删除的urlKey
* @return RxUrlManager
*/
public RxUrlManager removeUrlByKey(String urlKey) {
urlMap.remove(urlKey);
return this;
}

/**
* 针对单个baseUrl切换的时候清空老baseUrl的所有信息
*
* @param urlValue url
* @return RxUrlManager
*/
public RxUrlManager setUrl(String urlValue) {
urlMap.put(DEFAULT_URL_KEY, urlValue);
return this;
}

/**
* 获取全局唯一的baseUrl
*
* @return url
*/
public String getUrl() {
return getUrl(DEFAULT_URL_KEY);
return getUrlByKey(DEFAULT_URL_KEY);
}

public String getUrl(String urlKey) {
/**
* 根据key
*
* @param urlKey 获取对应的url
* @return url
*/
public String getUrlByKey(String urlKey) {
return urlMap.get(urlKey);
}

/**
* 清空设置的url相关的所以信息
* 相当于重置url
* 动态切换生产测试环境时候调用
*
* @return RxUrlManager
*/
public void clear() {
public RxUrlManager clear() {
urlMap.clear();
ApiFactory.getInstance().clearAllApi();
RxHttpUtils.removeAllCookie();
return this;
}
}

0 comments on commit 1ef7ff6

Please sign in to comment.