初步完成网络配置
This commit is contained in:
@@ -23,6 +23,7 @@ android {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@@ -30,5 +31,6 @@ dependencies {
|
||||
implementation rootProject.ext.dependencies.androidxappcompat
|
||||
implementation rootProject.ext.dependencies.androidxconstraintlayout
|
||||
implementation project(path: ':foudations:mogo-passport')
|
||||
implementation project(path: ':foudations:mogo-commons')
|
||||
|
||||
}
|
||||
@@ -29,11 +29,13 @@ dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
implementation 'androidx.core:core-ktx:1.1.0'
|
||||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||||
implementation project(path: ':foudations:mogo-passport')
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
implementation rootProject.ext.dependencies.retrofit
|
||||
implementation rootProject.ext.dependencies.retrofitadapter
|
||||
implementation rootProject.ext.dependencies.retrofitconvertergson
|
||||
implementation rootProject.ext.dependencies.retrofitconverterscalars
|
||||
api rootProject.ext.dependencies.retrofit
|
||||
api rootProject.ext.dependencies.retrofitadapter
|
||||
api rootProject.ext.dependencies.retrofitconvertergson
|
||||
api rootProject.ext.dependencies.retrofitconverterscalars
|
||||
implementation project(path: ':foudations:mogo-httpdns')
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.mogo.cloud.commons.network
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/20 10:46
|
||||
* describe:
|
||||
*/
|
||||
class NetConstants {
|
||||
|
||||
companion object {
|
||||
/** 无数据 */
|
||||
const val NO_DATA = -800
|
||||
|
||||
/** 数据返回正常 */
|
||||
const val OK = 0
|
||||
|
||||
const val READ_TIMEOUT = 20000L
|
||||
const val WRITE_TIMEOUT = 20000L
|
||||
const val CONNECT_TIMEOUT = 15000L
|
||||
|
||||
const val HTTP_DNS_ADDRESS_TYPE_HTTP = 0
|
||||
const val HTTP_DNS_ADDRESS_TYPE_WS = 1
|
||||
const val HTTP_DNS_ADDRESS_TYPE_IM = 2
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,16 @@
|
||||
package com.mogo.cloud.commons.network
|
||||
|
||||
import com.mogo.cloud.commons.network.NetConstants.Companion.CONNECT_TIMEOUT
|
||||
import com.mogo.cloud.commons.network.NetConstants.Companion.READ_TIMEOUT
|
||||
import com.mogo.cloud.commons.network.NetConstants.Companion.WRITE_TIMEOUT
|
||||
import com.mogo.cloud.commons.network.interceptor.HeaderNetworkInterceptor
|
||||
import com.mogo.cloud.commons.network.interceptor.RequestLogInterceptor
|
||||
import com.mogo.cloud.commons.network.interceptor.ResponseLogInterceptor
|
||||
import com.mogo.cloud.commons.utils.lookup
|
||||
import okhttp3.Dns
|
||||
import okhttp3.OkHttpClient
|
||||
import java.net.InetAddress
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
|
||||
/**
|
||||
@@ -10,11 +20,24 @@ import okhttp3.OkHttpClient
|
||||
* @Description: 描述
|
||||
*/
|
||||
class OkHttpFactory private constructor() {
|
||||
|
||||
companion object {
|
||||
val okHttpClient: OkHttpClient by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
|
||||
val builder = OkHttpClient.Builder()
|
||||
builder.build()
|
||||
OkHttpClient.Builder()
|
||||
.addInterceptor(RequestLogInterceptor())
|
||||
.addInterceptor(ResponseLogInterceptor())
|
||||
.addNetworkInterceptor(HeaderNetworkInterceptor())
|
||||
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(READ_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(WRITE_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.dns { hostname: String? ->
|
||||
val addresses: List<InetAddress?>? = lookup(hostname)
|
||||
addresses?.let {
|
||||
if (!it.isEmpty()) {
|
||||
return@dns addresses
|
||||
}
|
||||
}
|
||||
Dns.SYSTEM.lookup(hostname)
|
||||
}.build()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,40 +12,37 @@ import retrofit2.converter.gson.GsonConverterFactory
|
||||
* @CreateDate: 1/19/21 11:21 PM
|
||||
* @Description: 描述
|
||||
*/
|
||||
class RetrofitFactory private constructor() {
|
||||
object RetrofitFactory {
|
||||
private val sRpcServiceMap: MutableMap<String, Retrofit?> = ArrayMap()
|
||||
private val sRpcNoAdapterServiceMap: MutableMap<String, Retrofit?> = ArrayMap()
|
||||
|
||||
companion object {
|
||||
private val sRpcServiceMap: MutableMap<String, Retrofit?> = ArrayMap()
|
||||
private val sRpcNoAdapterServiceMap: MutableMap<String, Retrofit?> = ArrayMap()
|
||||
|
||||
@Synchronized
|
||||
fun getInstance(baseUrl: String): Retrofit? {
|
||||
var target = sRpcServiceMap[baseUrl]
|
||||
if (target == null) {
|
||||
target = Retrofit.Builder()
|
||||
.client(OkHttpFactory.okHttpClient)
|
||||
.baseUrl(baseUrl)
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
sRpcServiceMap[baseUrl] = target
|
||||
}
|
||||
return target
|
||||
@Synchronized
|
||||
fun getInstance(baseUrl: String): Retrofit? {
|
||||
var target = sRpcServiceMap[baseUrl]
|
||||
if (target == null) {
|
||||
target = Retrofit.Builder()
|
||||
.client(OkHttpFactory.okHttpClient)
|
||||
.baseUrl(baseUrl)
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
sRpcServiceMap[baseUrl] = target
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun getInstanceNoCallAdapter(baseUrl: String): Retrofit? {
|
||||
var target = sRpcNoAdapterServiceMap[baseUrl]
|
||||
if (target == null) {
|
||||
target = Retrofit.Builder()
|
||||
.client(OkHttpFactory.okHttpClient)
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
sRpcNoAdapterServiceMap[baseUrl] = target
|
||||
}
|
||||
return target
|
||||
@Synchronized
|
||||
fun getInstanceNoCallAdapter(baseUrl: String): Retrofit? {
|
||||
var target = sRpcNoAdapterServiceMap[baseUrl]
|
||||
if (target == null) {
|
||||
target = Retrofit.Builder()
|
||||
.client(OkHttpFactory.okHttpClient)
|
||||
.baseUrl(baseUrl)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
sRpcNoAdapterServiceMap[baseUrl] = target
|
||||
}
|
||||
return target
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.mogo.cloud.commons.network.interceptor
|
||||
|
||||
import com.mogo.cloud.passport.MoGoAiCloudClient
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/20 15:18
|
||||
* describe:
|
||||
*/
|
||||
class HeaderNetworkInterceptor : Interceptor {
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val original = chain.request()
|
||||
val request = original.newBuilder()
|
||||
.header("token", MoGoAiCloudClient.getInstance().config.token)
|
||||
.header("thirdPartyAppKey", MoGoAiCloudClient.getInstance().config.thirdPartyAppKey)
|
||||
.method(original.method(), original.body())
|
||||
.build()
|
||||
return chain.proceed(request)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.mogo.cloud.commons.network.interceptor
|
||||
|
||||
import android.util.Log
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Protocol
|
||||
import okhttp3.Response
|
||||
import okio.Buffer
|
||||
import java.io.IOException
|
||||
import java.nio.charset.Charset
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/20 10:48
|
||||
* describe:
|
||||
*/
|
||||
class RequestLogInterceptor: Interceptor {
|
||||
private val TAG = "RequestLogInterceptor"
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val request = chain.request()
|
||||
val requestBody = request.body()
|
||||
val hasRequestBody = requestBody != null
|
||||
|
||||
var protocol = Protocol.HTTP_1_1.toString()
|
||||
if (chain.connection() != null && chain.connection()!!.protocol() != null) {
|
||||
protocol = chain.connection()!!.protocol().toString()
|
||||
}
|
||||
|
||||
val logMsg = StringBuilder()
|
||||
logMsg.append("--> ")
|
||||
logMsg.append(protocol).append(", ")
|
||||
logMsg.append(request.method()).append(", ")
|
||||
logMsg.append("Request Headers: ").append(request.headers()).append("\r\n")
|
||||
logMsg.append(request.url()).append("\r\n")
|
||||
if (hasRequestBody) {
|
||||
logMsg.append("Content-Type: ").append(requestBody!!.contentType()).append(", ")
|
||||
logMsg.append("\r\nContent-Length: ").append(requestBody!!.contentLength())
|
||||
try {
|
||||
var body: String? = null
|
||||
val UTF8 = Charset.forName("UTF-8")
|
||||
val buffer = Buffer()
|
||||
requestBody!!.writeTo(buffer)
|
||||
var charset = UTF8
|
||||
val contentType = requestBody!!.contentType()
|
||||
if (contentType != null) {
|
||||
charset = contentType.charset(UTF8)
|
||||
}
|
||||
if (charset != null) {
|
||||
body = buffer.readString(charset)
|
||||
}
|
||||
logMsg.append("\r\nContent-body: ").append(body)
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
logMsg.append("\r\n<-- end http request")
|
||||
|
||||
if (NetConfig.INSTANCE.isLoggable()) {
|
||||
Log.d(TAG, logMsg.toString())
|
||||
}
|
||||
|
||||
return chain.proceed(request)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.mogo.cloud.commons.network.interceptor
|
||||
|
||||
import android.util.Log
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Response
|
||||
import okhttp3.ResponseBody
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/20 10:50
|
||||
* describe:
|
||||
*/
|
||||
class ResponseLogInterceptor : Interceptor {
|
||||
private val TAG = "ResponseLogInterceptor"
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val startTime = System.nanoTime()
|
||||
val response = chain.proceed(chain.request())
|
||||
val endTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)
|
||||
|
||||
val responseBody = response.body()
|
||||
var responseContent: String? = null
|
||||
var bodySize: String? = null
|
||||
var contentType: MediaType? = null
|
||||
var consumedResponse = false
|
||||
|
||||
val logMsg = StringBuilder()
|
||||
|
||||
if (responseBody != null) {
|
||||
val contentLength = responseBody.contentLength()
|
||||
bodySize = if (contentLength != -1L) "$contentLength-byte" else "unknown-length"
|
||||
contentType = responseBody.contentType()
|
||||
responseContent = responseBody.string()
|
||||
consumedResponse = true
|
||||
}
|
||||
|
||||
logMsg.append("--> ")
|
||||
logMsg.append(response.code()).append(" ")
|
||||
logMsg.append(response.message()).append(" ")
|
||||
logMsg.append(response.protocol()).append(" ")
|
||||
logMsg.append(response.request().url()).append("\r\n")
|
||||
logMsg.append("Response Content: ").append(responseContent).append("\r\n")
|
||||
logMsg.append("Content-Type: ").append(contentType).append(", ")
|
||||
logMsg.append("Content-Length: ").append(bodySize).append(", ")
|
||||
logMsg.append(" (").append(endTime).append("ms)")
|
||||
logMsg.append(" <-- end http response")
|
||||
|
||||
if (NetConfig.INSTANCE.isLoggable()) {
|
||||
Log.d(TAG, logMsg.toString())
|
||||
}
|
||||
|
||||
return if (consumedResponse) response.newBuilder().body(ResponseBody.create(contentType, responseContent)).build() else response
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.mogo.cloud.commons.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkInfo
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/20 11:04
|
||||
* describe:
|
||||
*/
|
||||
class CheckUtils {
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* 网络是否可用
|
||||
*/
|
||||
fun isNetworkConnected(context: Context?): Boolean {
|
||||
if (context == null) {
|
||||
return false
|
||||
}
|
||||
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
var network: NetworkInfo? = null
|
||||
if (cm != null) {
|
||||
network = cm.activeNetworkInfo
|
||||
}
|
||||
return network != null && network.isAvailable && network.isConnected
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.mogo.cloud.commons.utils
|
||||
|
||||
import android.text.TextUtils
|
||||
import com.mogo.cloud.commons.network.NetConstants.Companion.HTTP_DNS_ADDRESS_TYPE_HTTP
|
||||
import com.mogo.cloud.httpdns.MogoHttpDnsClient
|
||||
import java.net.InetAddress
|
||||
import java.net.UnknownHostException
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* created by wujifei on 2021/1/20 12:51
|
||||
* describe:
|
||||
*/
|
||||
@Throws(UnknownHostException::class)
|
||||
fun lookup(hostname: String?): List<InetAddress?>? {
|
||||
val cacheIp: String? = hostname?.let { MogoHttpDnsClient.getHttpDnsAddressUseCacheIfNecessary(HTTP_DNS_ADDRESS_TYPE_HTTP, it) }
|
||||
if (cacheIp == null || TextUtils.isEmpty(cacheIp)) {
|
||||
return emptyList<InetAddress>()
|
||||
}
|
||||
val info = cacheIp.split(":").toTypedArray()
|
||||
return if (info.size > 1) {
|
||||
Arrays.asList(*InetAddress.getAllByName(info[0]))
|
||||
} else {
|
||||
Arrays.asList(*InetAddress.getAllByName(cacheIp))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user