1). 思路
I. 在MainApplication中的getPackages方法中,重複添加NetworkingModule子產品,後者覆寫前者。但官方的NetworkingModule中canOverrideExistingModule方法始終傳回false, 故無法替換。
@Override
public boolean canOverrideExistingModule() {
// TODO(t11394819): Make this final and use annotation
return false;
}
II. 替換原有的OkHttpClient,此方法經試驗過後依舊無效。。
OkHttpClient okHttpClient = OkHttpClientProvider.getOkHttpClient().newBuilder().addNetworkInterceptor(getHttpLoggingInterceptor()).build();
OkHttpClientProvider.replaceOkHttpClient(okHttpClient);
III. 建立MainReactPackage,并繼承MainReactPackage類,實作getNativeModules方法,在getNativeModules方法中修改清單。
- 修改方法一:周遊子產品清單,并判斷哪一個下标的type(類型)是
, 并在重新設定目前下标的數值。出現的問題,使用自增變量周遊,效率不如疊代。NetworkingModule.class
- 修改方法二:建立一個子產品清單,疊代原有的子產品清單,并判斷type是否為
,如果是則添加自定義的,反之直接添加即可。 出現的問題:每次都有周遊所有的子產品影響效率。NetworkingModule.class
- **修改方法三:****将原有的子產品清單拷貝為新的子產品清單,疊代原有的子產品清單,并判斷type是否為
,如果是則先在拷貝的子產品清單中移除目前項,再添加自定義的子產品,完成後break跳出。NetworkingModule.class
2). 代碼
/**
* 自定義的MainReactPackage工具包
* 1. 移除了官方添加的NetworkingModule子產品
* 2. 添加了帶有攔截器的自定義的NetworkingModule子產品
* 3. 添加'com.squareup.okhttp3:logging-interceptor:3.10.0'
* 4. 實作了網絡通路時的日志列印
* Created by mazaiting on 2018/6/13.
*/
public class CustomMainReactPackage extends MainReactPackage {
/**
* 擷取日志攔截器
*
* @return Http日志攔截器
*/
private static HttpLoggingInterceptor getHttpLoggingInterceptor() {
// 日志顯示級别
HttpLoggingInterceptor.Level level = HttpLoggingInterceptor.Level.BODY;
// 建立攔截器
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(@NonNull String message) {
// 判斷是否有内容
if (!TextUtils.isEmpty(message)) {
// 列印日志
Log.e("Okhttp3 =====>", message);
}
}
});
// 設定顯示級别
loggingInterceptor.setLevel(level);
return loggingInterceptor;
}
/**
* 重寫NativeModules子產品
*
* @param context 上下文
*/
@Override
public List<ModuleSpec> getNativeModules(ReactApplicationContext context) {
List<ModuleSpec> nativeModules = super.getNativeModules(context);
// 傳回整改後的Modules清單
return adjustModules(context, nativeModules);
}
/**
* 修改系統的Modules
*
* @param context 上下文
* @param nativeModules 本地子產品
* @return 子產品清單
*/
private List<ModuleSpec> adjustModules(final ReactApplicationContext context, List<ModuleSpec> nativeModules) {
// 建立攔截器清單,NetworkInterceptorCreator在com.facebook.react.modules.network包下
final List<NetworkInterceptorCreator> list = new ArrayList<>();
// 添加攔截器
list.add(new NetworkInterceptorCreator() {
@Override
public Interceptor create() {
// 傳回自定義的攔截器
return getHttpLoggingInterceptor();
}
});
// 建立子產品清單
List<ModuleSpec> modules = new ArrayList<>(nativeModules);
// 周遊子產品
for (ModuleSpec moduleSpec : nativeModules) {
// 判斷是否為NetworkingModule網絡子產品
if (NetworkingModule.class.equals(moduleSpec.getType())) {
modules.remove(moduleSpec);
// 添加自定義的網絡子產品
modules.add(ModuleSpec.nativeModuleSpec(
NetworkingModule.class,
new Provider<NativeModule>() {
@Override
public NativeModule get() {
// 傳回帶有攔截器的NetworkingModule網絡子產品
return new NetworkingModule(context, list);
}
}));
break;
}
}
return modules;
}
}
3). kotlin版本
/**
* 自定義的MainReactPackage工具包
* 1. 移除了官方添加的NetworkingModule子產品
* 2. 添加了帶有攔截器的自定義的NetworkingModule子產品
* 3. 添加'com.squareup.okhttp3:logging-interceptor:3.10.0'
* 4. 實作了網絡通路時的日志列印
* Created by mazaiting on 2018/6/13.
*/
class CustomMainReactPackage : MainReactPackage() {
/**
* 擷取日志攔截器
* @return Http日志攔截器
*/
private fun getHttpLoggingInterceptor(): HttpLoggingInterceptor {
// 建立
val loggingInterceptor = HttpLoggingInterceptor(
HttpLoggingInterceptor.Logger { message: String? ->
if (!TextUtils.isEmpty(message)) {
Log.d("OkHttp=====>", message)
}
}
)
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY;
return loggingInterceptor
}
override fun getNativeModules(context: ReactApplicationContext?): MutableList<ModuleSpec> {
// 擷取父類的子產品
val nativeModules = super.getNativeModules(context)
return adjustModules(context, nativeModules)
}
/**
* 矯正子產品清單
* @param context 上下文
* @param nativeModules 子產品清單
*/
private fun adjustModules(context: ReactApplicationContext?, nativeModules: List<ModuleSpec>): MutableList<ModuleSpec> {
// 建立攔截器,并添加Http攔截器
val list = Arrays.asList(NetworkInterceptorCreator { getHttpLoggingInterceptor() })
// 拷貝子產品
val modules = ArrayList(nativeModules)
// 周遊
nativeModules.forEach { moduleSpec: ModuleSpec ->
// 判斷類型
if (NetworkingModule::class.java == moduleSpec.type) {
// 移除目前項
modules.remove(moduleSpec)
// 添加新項
modules.add(ModuleSpec.nativeModuleSpec(
NetworkingModule::class.java,
{ NetworkingModule(context, list) }
))
return@forEach
}
}
return modules
}
}