Skip to content

Commit a5e7903

Browse files
refactor(*): 优化RetrofitFactory,添加debug开关;优化TitleBarView,添加获取标题文字方法
1 parent ae43631 commit a5e7903

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

app/src/main/java/com/pengxh/androidx/lib/util/RetrofitServiceManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class RetrofitServiceManager {
1010
private static final String TAG = "RetrofitServiceManager";
1111

1212
private static final RetrofitService api = RetrofitFactory.createRetrofit(
13-
"https://way.jd.com", RetrofitService.class
13+
"https://way.jd.com", RetrofitService.class, true
1414
);
1515

1616
public static Observable<ResponseBody> getImageList(String channel, int start) {

lite/src/main/java/com/pengxh/androidx/lite/utils/RetrofitFactory.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,30 @@
1515
public class RetrofitFactory {
1616
private static final String TAG = "RetrofitFactory";
1717

18-
public static <T> T createRetrofit(String httpConfig, Class<T> tClass) {
19-
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
20-
@Override
21-
public void log(@NonNull String s) {
22-
Log.d(TAG, ">>>>> " + s);
23-
}
24-
});
25-
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
18+
public static <T> T createRetrofit(String httpConfig, Class<T> clazz, boolean debug) {
2619
OkHttpClient.Builder builder = new OkHttpClient.Builder()
2720
.connectTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
2821
.readTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS)
2922
.writeTimeout(Constant.HTTP_TIMEOUT, TimeUnit.SECONDS);
30-
OkHttpClient httpClient = builder.addInterceptor(interceptor).build();
23+
OkHttpClient httpClient;
24+
if (debug) {
25+
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
26+
@Override
27+
public void log(@NonNull String s) {
28+
Log.d(TAG, ">>>>> " + s);
29+
}
30+
});
31+
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
32+
httpClient = builder.addInterceptor(interceptor).build();
33+
} else {
34+
httpClient = builder.build();
35+
}
3136
return new Retrofit.Builder()
3237
.baseUrl(httpConfig)
3338
.addConverterFactory(GsonConverterFactory.create())//Gson转换器
3439
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
3540
.client(httpClient) //log拦截器
36-
.build().create(tClass);
41+
.build()
42+
.create(clazz);
3743
}
3844
}

lite/src/main/java/com/pengxh/androidx/lite/widget/TitleBarView.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ public void setTitle(String title) {
138138
invalidate();
139139
}
140140

141+
/**
142+
* 获取当前显示标题文字
143+
*/
144+
public String getTitle() {
145+
return textView.getText().toString();
146+
}
147+
141148
private OnClickListener listener;
142149

143150
public void setOnClickListener(OnClickListener listener) {

0 commit comments

Comments
 (0)