Showing
8 changed files
with
48 additions
and
25 deletions
| @@ -4,13 +4,15 @@ package com.wd.fastcoding.app; | @@ -4,13 +4,15 @@ package com.wd.fastcoding.app; | ||
| 4 | import android.app.Application; | 4 | import android.app.Application; |
| 5 | 5 | ||
| 6 | import com.wd.capability.network.RetrofitClient; | 6 | import com.wd.capability.network.RetrofitClient; |
| 7 | -import com.wd.common.interceptor.TokenInterceptor; | 7 | +import com.wd.foundation.wdinterfaceimpl.interceptor.TokenInterceptor; |
| 8 | import com.wd.capability.router.ArouteInit; | 8 | import com.wd.capability.router.ArouteInit; |
| 9 | import com.wd.foundation.wdkit.utils.CommonUtil; | 9 | import com.wd.foundation.wdkit.utils.CommonUtil; |
| 10 | import com.wd.fastcoding.app.start.StartTaskConfig; | 10 | import com.wd.fastcoding.app.start.StartTaskConfig; |
| 11 | import com.wd.foundation.wdkitcore.tools.AppContext; | 11 | import com.wd.foundation.wdkitcore.tools.AppContext; |
| 12 | import com.wd.foundation.wdkitcore.tools.StringUtils; | 12 | import com.wd.foundation.wdkitcore.tools.StringUtils; |
| 13 | 13 | ||
| 14 | +import okhttp3.Interceptor; | ||
| 15 | + | ||
| 14 | /** | 16 | /** |
| 15 | * @author devel | 17 | * @author devel |
| 16 | * @time 2024/8/28 星期三 16:23 | 18 | * @time 2024/8/28 星期三 16:23 |
| @@ -35,10 +37,10 @@ public class MyApplication extends Application { | @@ -35,10 +37,10 @@ public class MyApplication extends Application { | ||
| 35 | * 初始化第三方库 | 37 | * 初始化第三方库 |
| 36 | */ | 38 | */ |
| 37 | private void initLibs() { | 39 | private void initLibs() { |
| 38 | - // 网络请求初始化,主线程立即调用 | ||
| 39 | - RetrofitClient.init(this, new TokenInterceptor(RetrofitClient.getInterceptorHosts())); | ||
| 40 | // 路由初始化,主线程立即调用 | 40 | // 路由初始化,主线程立即调用 |
| 41 | ArouteInit.getInstance().init(this); | 41 | ArouteInit.getInstance().init(this); |
| 42 | + // 网络请求初始化,主线程立即调用 | ||
| 43 | + RetrofitClient.init(this); | ||
| 42 | } | 44 | } |
| 43 | 45 | ||
| 44 | /** | 46 | /** |
| 1 | package com.wd.foundation.wdinterface.config | 1 | package com.wd.foundation.wdinterface.config |
| 2 | 2 | ||
| 3 | import com.alibaba.android.arouter.facade.template.IProvider | 3 | import com.alibaba.android.arouter.facade.template.IProvider |
| 4 | +import okhttp3.Interceptor | ||
| 4 | 5 | ||
| 5 | /** | 6 | /** |
| 6 | * @author wangkai | 7 | * @author wangkai |
| 7 | * @version [V1.0.0, 2024/11/8] | 8 | * @version [V1.0.0, 2024/11/8] |
| 8 | * @brief | 9 | * @brief |
| 9 | */ | 10 | */ |
| 10 | -interface IConfig : IProvider { | 11 | +interface INetConfig : IProvider { |
| 11 | fun getBaseUrl(): String | 12 | fun getBaseUrl(): String |
| 13 | + | ||
| 14 | + fun getInterceptors(): Array<Interceptor> | ||
| 12 | } | 15 | } |
| @@ -11,5 +11,5 @@ public class InterfaceConstant { | @@ -11,5 +11,5 @@ public class InterfaceConstant { | ||
| 11 | 11 | ||
| 12 | public static final String PATH_APP_INFO = PATH_MODULE_INTERFACE + "/AppInfoService"; | 12 | public static final String PATH_APP_INFO = PATH_MODULE_INTERFACE + "/AppInfoService"; |
| 13 | 13 | ||
| 14 | - public static final String PATH_CONFIG = PATH_MODULE_INTERFACE + "/Config"; | 14 | + public static final String PATH_NET_CONFIG = PATH_MODULE_INTERFACE + "/NetConfig"; |
| 15 | } | 15 | } |
| @@ -5,26 +5,36 @@ import android.content.Context; | @@ -5,26 +5,36 @@ import android.content.Context; | ||
| 5 | import androidx.annotation.NonNull; | 5 | import androidx.annotation.NonNull; |
| 6 | 6 | ||
| 7 | import com.alibaba.android.arouter.facade.annotation.Route; | 7 | import com.alibaba.android.arouter.facade.annotation.Route; |
| 8 | -import com.wd.foundation.wdinterface.config.IConfig; | 8 | +import com.wd.capability.network.RetrofitClient; |
| 9 | +import com.wd.foundation.wdinterface.config.INetConfig; | ||
| 9 | import com.wd.foundation.wdinterface.constant.InterfaceConstant; | 10 | import com.wd.foundation.wdinterface.constant.InterfaceConstant; |
| 11 | +import com.wd.foundation.wdinterfaceimpl.interceptor.TokenInterceptor; | ||
| 12 | + | ||
| 13 | +import okhttp3.Interceptor; | ||
| 10 | 14 | ||
| 11 | /** | 15 | /** |
| 12 | * @author wangkai | 16 | * @author wangkai |
| 13 | * @version [V1.0.0, 2024/11/8] | 17 | * @version [V1.0.0, 2024/11/8] |
| 14 | * @brief | 18 | * @brief |
| 15 | */ | 19 | */ |
| 16 | -@Route(path = InterfaceConstant.PATH_CONFIG) | ||
| 17 | -public class Config implements IConfig { | 20 | +@Route(path = InterfaceConstant.PATH_NET_CONFIG) |
| 21 | +public class NetConfig implements INetConfig { | ||
| 18 | private static final String baseUrlRel = "https://pdapis.pdnews.cn/"; | 22 | private static final String baseUrlRel = "https://pdapis.pdnews.cn/"; |
| 19 | 23 | ||
| 24 | + @Override | ||
| 25 | + public void init(Context context) { | ||
| 26 | + | ||
| 27 | + } | ||
| 28 | + | ||
| 20 | @NonNull | 29 | @NonNull |
| 21 | @Override | 30 | @Override |
| 22 | public String getBaseUrl() { | 31 | public String getBaseUrl() { |
| 23 | return baseUrlRel; | 32 | return baseUrlRel; |
| 24 | } | 33 | } |
| 25 | 34 | ||
| 35 | + @NonNull | ||
| 26 | @Override | 36 | @Override |
| 27 | - public void init(Context context) { | ||
| 28 | - | 37 | + public Interceptor[] getInterceptors() { |
| 38 | + return new TokenInterceptor[]{new TokenInterceptor(RetrofitClient.getInterceptorHosts())}; | ||
| 29 | } | 39 | } |
| 30 | } | 40 | } |
| 1 | 1 | ||
| 2 | -package com.wd.common.interceptor; | 2 | +package com.wd.foundation.wdinterfaceimpl.interceptor; |
| 3 | 3 | ||
| 4 | import android.os.Handler; | 4 | import android.os.Handler; |
| 5 | import android.os.Looper; | 5 | import android.os.Looper; |
| @@ -11,7 +11,6 @@ import androidx.annotation.NonNull; | @@ -11,7 +11,6 @@ import androidx.annotation.NonNull; | ||
| 11 | import com.wd.base.log.Logger; | 11 | import com.wd.base.log.Logger; |
| 12 | import com.wd.capability.network.BaseObserver; | 12 | import com.wd.capability.network.BaseObserver; |
| 13 | import com.wd.capability.network.RetrofitClient; | 13 | import com.wd.capability.network.RetrofitClient; |
| 14 | -import com.wd.common.api.IRefreshToken; | ||
| 15 | import com.wd.capability.network.bean.MetaBean; | 14 | import com.wd.capability.network.bean.MetaBean; |
| 16 | import com.wd.capability.network.bean.TokenBean; | 15 | import com.wd.capability.network.bean.TokenBean; |
| 17 | import com.wd.capability.network.constant.EventConstants; | 16 | import com.wd.capability.network.constant.EventConstants; |
| @@ -20,6 +19,7 @@ import com.wd.capability.network.constant.ParameterConstant; | @@ -20,6 +19,7 @@ import com.wd.capability.network.constant.ParameterConstant; | ||
| 20 | import com.wd.capability.network.interceptor.LoggingInterceptor; | 19 | import com.wd.capability.network.interceptor.LoggingInterceptor; |
| 21 | import com.wd.capability.network.refreshtoken.IRefreshTokenForJsCallBack; | 20 | import com.wd.capability.network.refreshtoken.IRefreshTokenForJsCallBack; |
| 22 | import com.wd.capability.network.response.BaseResponse; | 21 | import com.wd.capability.network.response.BaseResponse; |
| 22 | +import com.wd.foundation.wdinterfaceimpl.config.IRefreshToken; | ||
| 23 | import com.wd.foundation.wdkit.json.JsonParseUtil; | 23 | import com.wd.foundation.wdkit.json.JsonParseUtil; |
| 24 | import com.wd.foundation.wdkit.utils.DeviceUtil; | 24 | import com.wd.foundation.wdkit.utils.DeviceUtil; |
| 25 | import com.wd.foundation.wdkit.utils.SpUtils; | 25 | import com.wd.foundation.wdkit.utils.SpUtils; |
| @@ -13,6 +13,9 @@ import com.wd.capability.network.interceptor.BaseInterceptor; | @@ -13,6 +13,9 @@ import com.wd.capability.network.interceptor.BaseInterceptor; | ||
| 13 | import com.wd.capability.network.interceptor.DynamicTimeoutInterceptor; | 13 | import com.wd.capability.network.interceptor.DynamicTimeoutInterceptor; |
| 14 | import com.wd.capability.network.interceptor.LoggingInterceptor; | 14 | import com.wd.capability.network.interceptor.LoggingInterceptor; |
| 15 | import com.wd.capability.network.interceptor.MultiDomainInterceptor; | 15 | import com.wd.capability.network.interceptor.MultiDomainInterceptor; |
| 16 | +import com.wd.foundation.wdinterface.config.INetConfig; | ||
| 17 | +import com.wd.foundation.wdinterface.constant.InterfaceConstant; | ||
| 18 | +import com.wd.foundation.wdkitcore.router.ArouterServiceManager; | ||
| 16 | import com.wd.foundation.wdkitcore.tools.ArrayUtils; | 19 | import com.wd.foundation.wdkitcore.tools.ArrayUtils; |
| 17 | 20 | ||
| 18 | import java.io.File; | 21 | import java.io.File; |
| @@ -63,25 +66,20 @@ public class RetrofitClient { | @@ -63,25 +66,20 @@ public class RetrofitClient { | ||
| 63 | * 过滤接口名字不做429弹窗 | 66 | * 过滤接口名字不做429弹窗 |
| 64 | */ | 67 | */ |
| 65 | private static List<String> interceptorHosts; | 68 | private static List<String> interceptorHosts; |
| 66 | - private static Interceptor mTokenInterceptor; | ||
| 67 | 69 | ||
| 68 | 70 | ||
| 69 | public static void init(Context context) { | 71 | public static void init(Context context) { |
| 70 | mContext = context; | 72 | mContext = context; |
| 71 | } | 73 | } |
| 72 | 74 | ||
| 73 | - public static void init(Context context, Interceptor tokenInterceptor) { | ||
| 74 | - mContext = context; | ||
| 75 | - mTokenInterceptor = tokenInterceptor; | ||
| 76 | - } | ||
| 77 | - | ||
| 78 | /** | 75 | /** |
| 79 | * 429不弹出 | 76 | * 429不弹出 |
| 77 | + * | ||
| 80 | * @param requestHosts | 78 | * @param requestHosts |
| 81 | */ | 79 | */ |
| 82 | - public static void initInterceptor( List<String> requestHosts) { | ||
| 83 | - if (ArrayUtils.isNotEmpty(requestHosts)){ | ||
| 84 | - if (null == interceptorHosts){ | 80 | + public static void initInterceptor(List<String> requestHosts) { |
| 81 | + if (ArrayUtils.isNotEmpty(requestHosts)) { | ||
| 82 | + if (null == interceptorHosts) { | ||
| 85 | interceptorHosts = new ArrayList<>(); | 83 | interceptorHosts = new ArrayList<>(); |
| 86 | } | 84 | } |
| 87 | interceptorHosts.clear(); | 85 | interceptorHosts.clear(); |
| @@ -112,6 +110,10 @@ public class RetrofitClient { | @@ -112,6 +110,10 @@ public class RetrofitClient { | ||
| 112 | if (TextUtils.isEmpty(url)) { | 110 | if (TextUtils.isEmpty(url)) { |
| 113 | url = baseUrl; | 111 | url = baseUrl; |
| 114 | } | 112 | } |
| 113 | + INetConfig config = ArouterServiceManager.provide(InterfaceConstant.PATH_NET_CONFIG); | ||
| 114 | + if (config != null) { | ||
| 115 | + url = config.getBaseUrl(); | ||
| 116 | + } | ||
| 115 | 117 | ||
| 116 | if (httpCacheDirectory == null) { | 118 | if (httpCacheDirectory == null) { |
| 117 | httpCacheDirectory = new File(mContext.getCacheDir(), NetManager.getNetManager().builder.getCacheFile()); | 119 | httpCacheDirectory = new File(mContext.getCacheDir(), NetManager.getNetManager().builder.getCacheFile()); |
| @@ -124,12 +126,17 @@ public class RetrofitClient { | @@ -124,12 +126,17 @@ public class RetrofitClient { | ||
| 124 | e.printStackTrace(); | 126 | e.printStackTrace(); |
| 125 | } | 127 | } |
| 126 | 128 | ||
| 129 | + Interceptor[] mInterceptors = config.getInterceptors(); | ||
| 127 | // HttpsUtils.SslParams sslParams = HttpsUtils.getSslSocketFactory(); | 130 | // HttpsUtils.SslParams sslParams = HttpsUtils.getSslSocketFactory(); |
| 128 | OkHttpClient.Builder builder = new OkHttpClient.Builder().cache(cache); | 131 | OkHttpClient.Builder builder = new OkHttpClient.Builder().cache(cache); |
| 129 | builder.addInterceptor(new MultiDomainInterceptor()) | 132 | builder.addInterceptor(new MultiDomainInterceptor()) |
| 130 | - .addInterceptor(new BaseInterceptor(mContext)) | ||
| 131 | - .addInterceptor(mTokenInterceptor) | ||
| 132 | - .addInterceptor(new DynamicTimeoutInterceptor()); | 133 | + .addInterceptor(new BaseInterceptor(mContext)); |
| 134 | + if (mInterceptors != null && mInterceptors.length > 0) { | ||
| 135 | + for (Interceptor interceptor : mInterceptors) { | ||
| 136 | + builder.addInterceptor(interceptor); | ||
| 137 | + } | ||
| 138 | + } | ||
| 139 | + builder.addInterceptor(new DynamicTimeoutInterceptor()); | ||
| 133 | if (BuildConfig.DEBUG) { | 140 | if (BuildConfig.DEBUG) { |
| 134 | builder.addInterceptor(new LoggingInterceptor()); | 141 | builder.addInterceptor(new LoggingInterceptor()); |
| 135 | } | 142 | } |
-
Please register or login to post a comment