「Update」
1、修改网络请求超时时间 const val READ_TIMEOUT = 10000L const val WRITE_TIMEOUT = 10000L 2、升级retrofit版本到2.6.4 3、增加OkHttp线程池管理
This commit is contained in:
@@ -14,6 +14,7 @@ android {
|
||||
packagingOptions {
|
||||
//解决编译时com.android.builder.merge.DuplicateRelativeFileException: More than one file was found with OS independent path 'META-INF/rxjava.properties'这个错误
|
||||
exclude 'META-INF/rxjava.properties'
|
||||
exclude 'META-INF/DEPENDENCIES'
|
||||
}
|
||||
|
||||
ndk {
|
||||
|
||||
@@ -34,9 +34,9 @@ ext {
|
||||
glide : 'com.github.bumptech.glide:glide:4.8.0',
|
||||
fresco : 'com.facebook.fresco:fresco:1.9.0',
|
||||
|
||||
retrofit : "com.squareup.retrofit2:retrofit:2.6.0",
|
||||
retrofitadapter : "com.squareup.retrofit2:adapter-rxjava2:2.6.0",
|
||||
retrofitconvertergson : "com.squareup.retrofit2:converter-gson:2.6.0",
|
||||
retrofit : "com.squareup.retrofit2:retrofit:2.6.4",
|
||||
retrofitadapter : "com.squareup.retrofit2:adapter-rxjava2:2.6.4",
|
||||
retrofitconvertergson : "com.squareup.retrofit2:converter-gson:2.6.4",
|
||||
retrofitconverterscalars : "com.squareup.retrofit2:converter-scalars:2.1.0",
|
||||
|
||||
// leakcanary
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.mogo.cloud.network
|
||||
|
||||
import java.util.concurrent.*
|
||||
|
||||
object CustomThreadPool {
|
||||
private const val CORE_POOL_SIZE = 5 // 核心线程数
|
||||
private const val MAXIMUM_POOL_SIZE = 10 // 最大线程数
|
||||
private const val KEEP_ALIVE_TIME = 1L // 空闲线程存活时间(单位:秒)
|
||||
private const val QUEUE_CAPACITY = 100 // 队列容量
|
||||
|
||||
private val workQueue = LinkedBlockingQueue<Runnable>(QUEUE_CAPACITY) // 任务队列
|
||||
|
||||
private fun createThreadPool(): ExecutorService {
|
||||
return ThreadPoolExecutor(
|
||||
CORE_POOL_SIZE,
|
||||
MAXIMUM_POOL_SIZE,
|
||||
KEEP_ALIVE_TIME,
|
||||
TimeUnit.SECONDS,
|
||||
workQueue,
|
||||
NamedThreadFactory("OkHttp Dispatcher"),
|
||||
ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
|
||||
)
|
||||
}
|
||||
|
||||
fun getThreadPool(): ExecutorService {
|
||||
return createThreadPool()
|
||||
}
|
||||
|
||||
// 自定义线程工厂,为线程命名
|
||||
private class NamedThreadFactory(private val namePrefix: String) : ThreadFactory {
|
||||
private var threadId = 1
|
||||
|
||||
override fun newThread(runnable: Runnable): Thread {
|
||||
val thread = Thread(runnable, "$namePrefix-#$threadId")
|
||||
thread.isDaemon = true
|
||||
threadId++
|
||||
return thread
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,11 +14,11 @@ class NetConstants {
|
||||
// 当连接已经建立之后,readTimeout 设置了从服务器读取数据的最大时间。
|
||||
// 这意味着客户端在等待从服务器接收响应体中的数据时,如果超过了这个时间限制,也会抛出SocketTimeoutException。
|
||||
// 此设置对于长时间运行的服务端事件(如HTTP长轮询)尤其重要。
|
||||
const val READ_TIMEOUT = 5000L
|
||||
const val READ_TIMEOUT = 10000L
|
||||
// 类似于readTimeout,writeTimeout 控制着向服务器写入数据的最大时间。
|
||||
// 如果客户端在发送请求体时遇到问题,并且超过了这个设定的时间,那么同样会抛出SocketTimeoutException。
|
||||
// 这对于大文件上传或者需要发送大量数据的情况尤为重要。
|
||||
const val WRITE_TIMEOUT = 5000L
|
||||
const val WRITE_TIMEOUT = 10000L
|
||||
|
||||
const val DEVA_HOST = "http://dzt-deva.zhidaozhixing.com"
|
||||
const val LAUNCHER_SNAPSHOT_HOST = "http://dzt-launcherSnapshot.zhidaozhixing.com"
|
||||
|
||||
@@ -22,7 +22,7 @@ import java.util.concurrent.TimeUnit
|
||||
*/
|
||||
class OkHttpFactory private constructor() {
|
||||
companion object {
|
||||
var dispatcher: Dispatcher = Dispatcher()
|
||||
var dispatcher: Dispatcher = Dispatcher(CustomThreadPool.getThreadPool())
|
||||
|
||||
val okHttpClient: OkHttpClient by lazy {
|
||||
//自定义上限,所有总数的最大上限
|
||||
|
||||
@@ -78,7 +78,7 @@ public class OkHttpEventListener extends EventListener {
|
||||
@Override
|
||||
public void callFailed(Call call, IOException ioe) {
|
||||
super.callFailed(call, ioe);
|
||||
logMethod(call.request().toString() + "\n方法的 callFailed", mCallStartTime);
|
||||
logMethod(call.request().toString() + "方法的 callFailed IOException =" + ioe.getMessage(), mCallStartTime);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,7 +124,7 @@ public class OkHttpEventListener extends EventListener {
|
||||
@Override
|
||||
public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol, IOException ioe) {
|
||||
super.connectFailed(call, inetSocketAddress, proxy, protocol, ioe);
|
||||
logMethod(call.request().toString() + "方法的 connectFailed", mConnectStartTime);
|
||||
logMethod(call.request().toString() + "方法的 connectFailed IOException =" + ioe.getMessage(), mConnectStartTime);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user