-
Notifications
You must be signed in to change notification settings - Fork 0
/
FJHttpUtils.kt
58 lines (53 loc) · 2.09 KB
/
FJHttpUtils.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Copyright (C) 2019 FynnJason.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fynnjason.fjokgo.http
import android.app.Application
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheEntity
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.interceptor.HttpLoggingInterceptor
import okhttp3.OkHttpClient
import java.util.concurrent.TimeUnit
import java.util.logging.Level
/**
* 网络请求框架的初始化工具类
*/
object FJHttpUtils {
private const val logTag = "OkHttp" // 日志输出的tag
private const val readTime: Long = 60 // 全局读取时间
private const val writeTime: Long = 60 // 全局写入时间
private const val connectTime: Long = 60 // 全局连接时间
// 构建初始化
fun init(application: Application) {
val builder = OkHttpClient.Builder()
builder.readTimeout(readTime, TimeUnit.SECONDS)
builder.writeTimeout(writeTime, TimeUnit.SECONDS)
builder.connectTimeout(connectTime, TimeUnit.SECONDS)
// 构建日志拦截器
val httpLoggingInterceptor = HttpLoggingInterceptor(logTag)
httpLoggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY)
httpLoggingInterceptor.setColorLevel(Level.INFO)
// 添加日志拦截器
builder.addInterceptor(httpLoggingInterceptor)
// 构建OkGo
OkGo.getInstance()
.init(application)
.setOkHttpClient(builder.build())
.setCacheMode(CacheMode.NO_CACHE)
.setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)
.retryCount = 3
}
}