Skip to content

Commit 66d21d9

Browse files
committed
1、修改FastRetrofit里初始化多BaseUrl 类FastMultiUrl.with方法
2、新增FastRetrofit里获取Retrofit、OkHttp 对象及Builder方法用于外边自定义并将原setHttpClient标记为废弃
1 parent 9f50a1a commit 66d21d9

File tree

8 files changed

+45
-15
lines changed

8 files changed

+45
-15
lines changed

apk/sample.apk

88 Bytes
Binary file not shown.

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ android {
1515
minSdkVersion ext.minSdkVersion
1616
targetSdkVersion ext.targetSdkVersion
1717
versionCode 219
18-
versionName "2.1.9-alpha"
18+
versionName "2.1.9-beta"
1919
multiDexEnabled true
2020
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2121
buildConfigField "String", "BASE_URL", "\"https://api.douban.com/\""

app/src/main/java/com/aries/library/fast/demo/App.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public void onCreate() {
6060
//.setHeaders(header)//设置统一请求头
6161
.setLogEnable(BuildConfig.DEBUG)//设置请求全局log-可设置tag及Level类型
6262
//.setLogEnable(BuildConfig.DEBUG, TAG, HttpLoggingInterceptor.Level.BASIC)
63-
.setTimeout(20);//设置统一超时--也可单独调用read/write/connect超时(可以设置时间单位TimeUnit)
63+
//设置统一超时--也可单独调用read/write/connect超时(可以设置时间单位TimeUnit)
64+
//默认20 s
65+
.setTimeout(20);
6466

6567
//以下为配置多BaseUrl
6668
//step1

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
minSdkVersion 15
1010
targetSdkVersion 25
1111
versionCode 219
12-
versionName "2.1.9-alpha"
12+
versionName "2.1.9-beta"
1313
}
1414
buildTypes {
1515
release {

library/src/main/java/com/aries/library/fast/FastConfig.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ private FastConfig(@Nullable Context context) {
7676
if (context == null) {
7777
throw new NullPointerException(FastConstant.EXCEPTION_FAST_CONFIG_CONTEXT_NOT_NULL);
7878
}
79-
LoggerManager.i("FastConfig:" + context);
8079
if (context != null) {
8180
this.mContext = context.getApplicationContext();
82-
LoggerManager.i("FastConfig:" + mContext + ";isApplication:" + (mContext instanceof Application));
8381
if (FastUtil.isClassExist("com.aries.ui.view.title.TitleBarView")) {
8482
setTitleConfig(new FastTitleConfigEntity()
8583
.setTitleBackgroundResource(R.color.colorTitleBackground)

library/src/main/java/com/aries/library/fast/entity/FastTitleConfigEntity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import android.view.View;
77

88
import com.aries.library.fast.i.IFastTitleView;
9-
import com.aries.library.fast.manager.LoggerManager;
109
import com.aries.library.fast.module.activity.FastTitleActivity;
1110

1211
/**
@@ -134,7 +133,6 @@ public FastTitleConfigEntity setLightStatusBarEnable(boolean lightStatusBarEnabl
134133
* @return
135134
*/
136135
public FastTitleConfigEntity setTitleElevation(float mTitleElevation) {
137-
LoggerManager.i("mTitleElevation:"+mTitleElevation);
138136
this.mTitleElevation = mTitleElevation;
139137
return this;
140138
}

library/src/main/java/com/aries/library/fast/retrofit/FastMultiUrl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class FastMultiUrl {
4242
private final List<OnUrlChangedListener> mListeners = new ArrayList<>();
4343
private FastUrlParser mUrlParser;
4444
private static volatile FastMultiUrl instance;
45+
private OkHttpClient.Builder builder;
4546

4647
public interface OnUrlChangedListener {
4748
/**
@@ -108,6 +109,7 @@ public Response intercept(Chain chain) throws IOException {
108109
* @return
109110
*/
110111
public OkHttpClient.Builder with(OkHttpClient.Builder builder) {
112+
this.builder = builder;
111113
return builder
112114
.addInterceptor(mInterceptor);
113115
}
@@ -131,7 +133,7 @@ public Request processRequest(Request request) {
131133
}
132134
if (null != httpUrl) {
133135
HttpUrl newUrl = mUrlParser.parseUrl(httpUrl, request.url());
134-
LoggerManager.d(FastMultiUrl.TAG, "New Url is { " + newUrl.toString() + " },Old Url is { " + request.url().toString() + " }");
136+
LoggerManager.i(FastMultiUrl.TAG, "New Url is { " + newUrl.toString() + " },Old Url is { " + request.url().toString() + " }");
135137
Object[] listeners = listenersToArray();
136138
if (listeners != null) {
137139
for (int i = 0; i < listeners.length; i++) {

library/src/main/java/com/aries/library/fast/retrofit/FastRetrofit.java

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class FastRetrofit {
3535
private static volatile Retrofit.Builder sRetrofitBuilder;
3636
private static OkHttpClient.Builder sClientBuilder;
3737
private static OkHttpClient sClient;
38-
private long mDelayTime = 10;
38+
private long mDelayTime = 20;
3939
private HttpLoggingInterceptor mLoggingInterceptor;
4040

4141
private FastRetrofit() {
@@ -44,6 +44,7 @@ private FastRetrofit() {
4444
.addConverterFactory(GsonConverterFactory.create())
4545
.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
4646
setTimeout(mDelayTime);
47+
FastMultiUrl.getInstance().with(sClientBuilder);
4748
}
4849

4950
public static FastRetrofit getInstance() {
@@ -57,13 +58,38 @@ public static FastRetrofit getInstance() {
5758
return sManager;
5859
}
5960

60-
public <T> T createService(Class<T> apiService) {
61-
setHttpClient(sClientBuilder.build());
61+
/**
62+
* 对外暴露 OkHttpClient,方便自定义
63+
*
64+
* @return
65+
*/
66+
public static OkHttpClient.Builder getOkHttpClientBuilder() {
67+
return getInstance().sClientBuilder;
68+
}
69+
70+
public static OkHttpClient getOkHttpClient() {
71+
return getOkHttpClientBuilder().build();
72+
}
73+
74+
/**
75+
* 对外暴露 Retrofit,方便自定义
76+
*
77+
* @return
78+
*/
79+
public static Retrofit.Builder getRetrofitBuilder() {
80+
sRetrofitBuilder.client(getOkHttpClient());
81+
return getInstance().sRetrofitBuilder;
82+
}
83+
84+
public static Retrofit getRetrofit() {
6285
if (sRetrofit == null) {
63-
sRetrofit = sRetrofitBuilder.build();
86+
sRetrofit = getRetrofitBuilder().build();
6487
}
65-
FastMultiUrl.getInstance().with(sClientBuilder);
66-
return sRetrofit.create(apiService);
88+
return sRetrofit;
89+
}
90+
91+
public <T> T createService(Class<T> apiService) {
92+
return getRetrofit().create(apiService);
6793
}
6894

6995
/**
@@ -79,11 +105,15 @@ public FastRetrofit setBaseUrl(String baseUrl) {
79105
}
80106

81107
/**
82-
* 设置自定义OkHttpClient
108+
* 设置自定义OkHttpClient--2.1.9-beta版本后废弃可调用
109+
* 可以调用以下方法实现自定义
110+
* {@link #getOkHttpClient()}{@link #getOkHttpClientBuilder()}
111+
* {@link #getRetrofitBuilder()}{@link #getRetrofit()}
83112
*
84113
* @param okClient
85114
* @return
86115
*/
116+
@Deprecated
87117
public FastRetrofit setHttpClient(OkHttpClient okClient) {
88118
if (okClient != null && sClient != okClient) {
89119
sClient = okClient;

0 commit comments

Comments
 (0)