1
1
package com.mredrock.cyxbs.common.network
2
2
3
+ import android.app.Application
4
+ import android.content.pm.PackageManager
3
5
import android.os.Handler
4
6
import android.util.SparseArray
5
7
import com.google.gson.Gson
@@ -23,6 +25,7 @@ import retrofit2.converter.gson.GsonConverterFactory
23
25
import java.util.concurrent.TimeUnit
24
26
import android.os.Looper
25
27
import android.util.Log
28
+ import androidx.core.content.PackageManagerCompat
26
29
import com.mredrock.cyxbs.common.BaseApp
27
30
import com.mredrock.cyxbs.common.utils.LogLocal
28
31
import com.mredrock.cyxbs.common.utils.extensions.toast
@@ -150,13 +153,19 @@ object ApiGenerator {
150
153
*/
151
154
private fun Retrofit.Builder.configRetrofitBuilder (client : ((OkHttpClient .Builder ) -> OkHttpClient )): Retrofit .Builder {
152
155
return this .client(client.invoke(OkHttpClient ().newBuilder().apply {
153
- val logging = HttpLoggingInterceptor (object : HttpLoggingInterceptor .Logger {
154
- override fun log (message : String ) {
155
- Log .d(" OKHTTP" ," OKHTTP$message " )
156
- LogLocal .log(" OKHTTP" ," OKHTTP$message " )
157
- }
158
- })
156
+ val logging = HttpLoggingInterceptor { message ->
157
+ LogUtils .d(" OKHTTP" ,message)
158
+ LogLocal .log(" OKHTTP" , " OKHTTP$message " )
159
+ }
159
160
logging.level = HttpLoggingInterceptor .Level .BODY
161
+ addInterceptor(Interceptor {
162
+ val response = proceedPoxyWithTryCatch {
163
+ it.proceed(it.request().newBuilder()
164
+ .addHeader(" APPVersion" , BaseApp .version)
165
+ .build()
166
+ )}
167
+ response!!
168
+ })
160
169
addInterceptor(logging)
161
170
// 这里是在debug模式下方便开发人员简单确认 http 错误码 和 url(magipoke开始切的)
162
171
if (BuildConfig .DEBUG ){
@@ -194,6 +203,10 @@ object ApiGenerator {
194
203
// 不带token请求的OkHttp配置
195
204
private fun OkHttpClient.Builder.configureCommonOkHttp (): OkHttpClient {
196
205
return this .apply {
206
+ /* *
207
+ * 发送版本号
208
+ */
209
+
197
210
/* *
198
211
* 连接失败时切换备用url的Interceptor
199
212
* 一旦切换,只有重启app才能切回来(因为如果请求得到的url不是原来的@{link getBaseUrl()},则切换到新的url,而以后访问都用这个新的url了)
@@ -206,6 +219,17 @@ object ApiGenerator {
206
219
// 带token请求的OkHttp配置
207
220
private fun OkHttpClient.Builder.configureTokenOkHttp (): OkHttpClient {
208
221
return this .apply {
222
+ /* *
223
+ * 发送版本号
224
+ */
225
+ interceptors().add(Interceptor {
226
+ val response = proceedPoxyWithTryCatch {
227
+ it.proceed(it.request().newBuilder()
228
+ .addHeader(" version" , BaseApp .version)
229
+ .build()
230
+ )}
231
+ return @Interceptor response!!
232
+ })
209
233
/* *
210
234
* 连接失败时切换备用url的Interceptor
211
235
* 一旦切换,只有重启app才能切回来(因为如果请求得到的url不是原来的@{link getBaseUrl()},则切换到新的url,而以后访问都用这个新的url了)
0 commit comments