...
|
...
|
@@ -3,7 +3,6 @@ package com.wjx.android.wanandroidmvvm.network |
|
|
import android.util.Log
|
|
|
import com.br_technology.electricityandroid.util.token
|
|
|
import com.br_technology.securitytrain_master.base.common.Constant
|
|
|
import com.br_technology.securitytrain_master.base.common.StateType
|
|
|
import okhttp3.Interceptor
|
|
|
import okhttp3.OkHttpClient
|
|
|
import okhttp3.Request
|
...
|
...
|
@@ -11,6 +10,10 @@ import okhttp3.Response |
|
|
import retrofit2.Retrofit
|
|
|
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
|
|
|
import retrofit2.converter.gson.GsonConverterFactory
|
|
|
import java.io.IOException
|
|
|
import java.nio.Buffer
|
|
|
import java.nio.charset.Charset
|
|
|
|
|
|
|
|
|
/**
|
|
|
* Time: 7/26/2021 10:11
|
...
|
...
|
@@ -19,9 +22,9 @@ import retrofit2.converter.gson.GsonConverterFactory |
|
|
*/
|
|
|
|
|
|
class RetrofitFactory private constructor() {
|
|
|
private val retrofit : Retrofit
|
|
|
private val retrofit: Retrofit
|
|
|
|
|
|
fun <T> create(clazz: Class<T>) : T {
|
|
|
fun <T> create(clazz: Class<T>): T {
|
|
|
return retrofit.create(clazz)
|
|
|
}
|
|
|
|
...
|
...
|
@@ -41,7 +44,6 @@ class RetrofitFactory private constructor() { |
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private fun initOkHttpClient(): OkHttpClient {
|
|
|
return OkHttpClient.Builder()
|
|
|
.addInterceptor(initCookieIntercept())
|
...
|
...
|
@@ -63,7 +65,7 @@ class RetrofitFactory private constructor() { |
|
|
|
|
|
|
|
|
//只保存登录或者注册
|
|
|
if(requestUrl.contains(Constant.SAVE_USER_LOGIN_KEY) || requestUrl.contains(Constant.SAVE_USER_REGISTER_KEY)){
|
|
|
if (requestUrl.contains(Constant.SAVE_USER_LOGIN_KEY) || requestUrl.contains(Constant.SAVE_USER_REGISTER_KEY)) {
|
|
|
val mCookie = response.headers(Constant.SET_COOKIE_KEY)
|
|
|
mCookie?.let {
|
|
|
// saveCookie(domain,parseCookie(it))
|
...
|
...
|
@@ -80,7 +82,7 @@ class RetrofitFactory private constructor() { |
|
|
val builder = request.newBuilder()
|
|
|
val domain = request.url().host()
|
|
|
|
|
|
if(domain.isNotEmpty()){
|
|
|
if (domain.isNotEmpty()) {
|
|
|
// val mCookie by SPreference("cookie","")
|
|
|
// if(mCookie.isNotEmpty()){
|
|
|
// builder.addHeader(Constant.COOKIE_NAME,mCookie)
|
...
|
...
|
@@ -100,12 +102,14 @@ class RetrofitFactory private constructor() { |
|
|
.header("token", token)
|
|
|
.build()
|
|
|
|
|
|
chain.proceed(request)
|
|
|
val response = chain.proceed(request)
|
|
|
Log.e("response", getResponseInfo(response))
|
|
|
response
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private fun parseCookie(it: List<String>): String {
|
|
|
if(it.isEmpty()){
|
|
|
if (it.isEmpty()) {
|
|
|
return ""
|
|
|
}
|
|
|
|
...
|
...
|
@@ -115,7 +119,7 @@ class RetrofitFactory private constructor() { |
|
|
stringBuilder.append(cookie).append(";")
|
|
|
}
|
|
|
|
|
|
if(stringBuilder.isEmpty()){
|
|
|
if (stringBuilder.isEmpty()) {
|
|
|
return ""
|
|
|
}
|
|
|
//末尾的";"去掉
|
...
|
...
|
@@ -128,4 +132,32 @@ class RetrofitFactory private constructor() { |
|
|
// resutl = parseCookie
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
/**
|
|
|
* 打印返回消息
|
|
|
*
|
|
|
* @param response 返回的对象
|
|
|
*/
|
|
|
private fun getResponseInfo(response: Response?): String {
|
|
|
var str = ""
|
|
|
if (response == null || !response.isSuccessful) {
|
|
|
return str
|
|
|
}
|
|
|
val responseBody = response.body()
|
|
|
val contentLength = responseBody!!.contentLength()
|
|
|
val source = responseBody.source()
|
|
|
try {
|
|
|
source.request(Long.MAX_VALUE) // Buffer the entire body.
|
|
|
} catch (e: IOException) {
|
|
|
e.printStackTrace()
|
|
|
}
|
|
|
val buffer: okio.Buffer? = source.buffer()
|
|
|
val charset: Charset = Charset.forName("utf-8")
|
|
|
if (contentLength != 0L) {
|
|
|
if (buffer != null) {
|
|
|
str = buffer.clone().readString(charset)
|
|
|
}
|
|
|
}
|
|
|
return str
|
|
|
}
|
|
|
} |
|
|
\ No newline at end of file |
...
|
...
|
|