refactor(http): 移除旧的HTTP客户端配置并整合到网络管理器中
- 删除了废弃的HttpClient.kt文件中的自定义HTTP客户端实现 - 将基础URL配置从UrlConfig迁移到GlobalData统一管理 - 更新HttpManager使用GlobalData.appBaseUrl作为基础URL - 移除了HttpUtil.kt文件中的遗留HTTP工具类 - 简化了网络层架构,移除重复的配置类
This commit is contained in:
@@ -1,70 +0,0 @@
|
||||
//package com.shuwei.dish.match.http
|
||||
//
|
||||
//import android.annotation.SuppressLint
|
||||
//import android.util.Log
|
||||
//import okhttp3.Call
|
||||
//import okhttp3.Interceptor
|
||||
//import okhttp3.OkHttpClient
|
||||
//import okhttp3.Request
|
||||
//import okhttp3.Response
|
||||
//import java.util.concurrent.TimeUnit
|
||||
//
|
||||
//import javax.net.ssl.*
|
||||
//import java.security.SecureRandom
|
||||
//import java.security.cert.X509Certificate
|
||||
//
|
||||
//class HttpClient private constructor() {
|
||||
// private val client: OkHttpClient by lazy {
|
||||
// OkHttpClient.Builder()
|
||||
// .apply {
|
||||
// connectTimeout(15, TimeUnit.SECONDS)
|
||||
// readTimeout(30, TimeUnit.SECONDS)
|
||||
// writeTimeout(15, TimeUnit.SECONDS)
|
||||
// sslSocketFactory(createSSLSocketFactory(), TrustAllCerts())
|
||||
// hostnameVerifier { _, _ -> true }
|
||||
// //if (BuildConfig.Debug) {
|
||||
// addInterceptor(LoggingInterceptor())
|
||||
// // }
|
||||
// }
|
||||
// .build()
|
||||
// }
|
||||
//
|
||||
// companion object {
|
||||
// val instance by lazy { HttpClient() }
|
||||
// }
|
||||
//
|
||||
// fun newCall(request: Request): Call = client.newCall(request)
|
||||
//
|
||||
// inner class LoggingInterceptor : Interceptor {
|
||||
// override fun intercept(chain: Interceptor.Chain): Response {
|
||||
// val request = chain.request()
|
||||
// // 打印请求日志
|
||||
// Log.d("OkHttp", "--> ${request.method} ${request.url}")
|
||||
//
|
||||
// val response = chain.proceed(request)
|
||||
// // 打印响应日志
|
||||
// Log.d("OkHttp", "<-- ${response.code} ${response.request.url}")
|
||||
//
|
||||
// return response
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 信任所有证书的TrustManager实现
|
||||
// @SuppressLint("CustomX509TrustManager")
|
||||
// class TrustAllCerts : X509TrustManager {
|
||||
// @SuppressLint("TrustAllX509TrustManager")
|
||||
// override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
|
||||
// @SuppressLint("TrustAllX509TrustManager")
|
||||
// override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}
|
||||
// override fun getAcceptedIssuers(): Array<X509Certificate> = arrayOf()
|
||||
// }
|
||||
//
|
||||
// // 创建信任所有证书的SSLSocketFactory
|
||||
// fun createSSLSocketFactory(): SSLSocketFactory {
|
||||
// val sslContext = SSLContext.getInstance("TLS").apply {
|
||||
// init(null, arrayOf(TrustAllCerts()), SecureRandom())
|
||||
// }
|
||||
// return sslContext.socketFactory
|
||||
// }
|
||||
//
|
||||
//}
|
||||
@@ -1,199 +0,0 @@
|
||||
//package com.shuwei.dish.match.http
|
||||
//
|
||||
//import android.os.Handler
|
||||
//import android.os.Looper
|
||||
//import android.util.Log
|
||||
//import com.google.gson.reflect.TypeToken
|
||||
//import com.shuwei.dish.match.base.BaseApp
|
||||
//import com.shuwei.dish.match.base.BaseReq
|
||||
//import com.shuwei.dish.match.utils.SpTool
|
||||
//import com.shuwei.dish.match.utils.ext.toType
|
||||
//import okhttp3.Call
|
||||
//import okhttp3.Callback
|
||||
//import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
//import okhttp3.Request
|
||||
//import okhttp3.RequestBody.Companion.toRequestBody
|
||||
//import okhttp3.Response
|
||||
//import java.io.IOException
|
||||
//import java.util.concurrent.Executors
|
||||
//import java.util.concurrent.TimeUnit
|
||||
//
|
||||
//
|
||||
//object HttpUtil {
|
||||
//
|
||||
// var loopGetToken = true
|
||||
//
|
||||
// fun runMainThread(action: () -> Unit) {
|
||||
// Handler(Looper.getMainLooper()).post {
|
||||
// action()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // GET请求(HTTPS)
|
||||
// fun get(
|
||||
// url: String,
|
||||
// header: Map<String, String>?=null,
|
||||
// doSuccess: (data: Any) -> Unit,
|
||||
// doFailure: (code: Int?, msg: String?) -> Unit
|
||||
// ) {
|
||||
// val request = Request.Builder()
|
||||
// .url(if (url.isHttpApi()) url else UrlConfig.BASE_URL + url)
|
||||
// .apply {
|
||||
// if (header != null) {
|
||||
// header.forEach {
|
||||
// addHeader(it.key, it.value)
|
||||
// }
|
||||
// } else {
|
||||
// val token = getToken()
|
||||
// if (token.isNotBlank()) {
|
||||
// addHeader("X-Access-Token", token)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// .get()
|
||||
// .build()
|
||||
// HttpClient.instance.newCall(request).enqueue(CallbackImpl(doSuccess, doFailure))
|
||||
// }
|
||||
////
|
||||
//// fun get(
|
||||
//// isHttps: Boolean = false,
|
||||
//// host: String = "vip.shuziweidao.com",
|
||||
//// pathSegmentList: List<String>,
|
||||
//// queryParams: Map<String, String>,
|
||||
//// doSuccess: (data: Any) -> Unit,
|
||||
//// doFailure: (code: Int?, msg: String?) -> Unit
|
||||
//// ) {
|
||||
//// val token = getToken()
|
||||
//// val url = HttpUrl.Builder()
|
||||
//// .scheme(if (isHttps) "https" else "http")
|
||||
//// .host(host)
|
||||
//// .apply {
|
||||
//// pathSegmentList.forEach { addPathSegment(it) }
|
||||
//// queryParams.forEach { (key, value) -> addQueryParameter(key, value) }
|
||||
//// }
|
||||
//// .build()
|
||||
//// val request = Request.Builder()
|
||||
//// .url(url)
|
||||
//// .apply {
|
||||
//// if (token.isNotBlank()) {
|
||||
//// addHeader("Authorization", token)
|
||||
//// }
|
||||
//// }
|
||||
//// .get()
|
||||
//// .build()
|
||||
//// HttpClient.instance.newCall(request).enqueue(CallbackImpl(doSuccess, doFailure))
|
||||
//// }
|
||||
//
|
||||
// // POST表单(HTTPS)
|
||||
//// fun postForm(url: String, params: Map<String, String>, callback: Callback) {
|
||||
//// val formBody = FormBody.Builder().apply {
|
||||
//// params.forEach { (k, v) -> add(k, v) }
|
||||
//// }.build()
|
||||
////
|
||||
//// Request.Builder()
|
||||
//// .url(url)
|
||||
//// .post(formBody)
|
||||
//// .build().let { HttpClient.instance.newCall(it).enqueue(callback) }
|
||||
//// }
|
||||
//
|
||||
// // POST JSON(HTTPS)
|
||||
// fun postJson(
|
||||
// url: String,
|
||||
// json: String,
|
||||
// doSuccess: (data: Any) -> Unit,
|
||||
// doFailure: (code: Int?, msg: String?) -> Unit
|
||||
// ) {
|
||||
// val body = json
|
||||
// .toRequestBody("application/json; charset=utf-8".toMediaTypeOrNull())
|
||||
// val token = getToken()
|
||||
// Request.Builder()
|
||||
// .url(if (url.isHttpApi()) url else UrlConfig.BASE_URL + url)
|
||||
// .apply {
|
||||
// if (token.isNotBlank()) {
|
||||
// addHeader("X-Access-Token", token)
|
||||
// }
|
||||
// }
|
||||
// .post(body)
|
||||
// .build()
|
||||
// .let { HttpClient.instance.newCall(it).enqueue(CallbackImpl(doSuccess, doFailure)) }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// val executor = Executors.newSingleThreadScheduledExecutor()
|
||||
// var getTokenIsRunning = false
|
||||
// private fun exeTokenThread() {
|
||||
// if (getTokenIsRunning.not()) {
|
||||
// executor.scheduleWithFixedDelay(
|
||||
// task, 0, 1, TimeUnit.MINUTES
|
||||
// )
|
||||
// getTokenIsRunning = true
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// val task = Runnable {
|
||||
// if (BaseApp.token.isNullOrBlank()) {
|
||||
// getAppToken()
|
||||
// } else {
|
||||
// executor.shutdown()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// fun getToken() = SpTool.getString(SpTool.TOKEN)
|
||||
////BaseApp.instance?.token ?:""
|
||||
////BaseApp.getSharedPref()?.getString("token", "") ?: ""
|
||||
//
|
||||
// fun getAppToken() {
|
||||
// val url = "${UrlConfig.GET_TOKEN}?qrcodeId=${SpTool.getString(SpTool.DEVICE_ID)}"
|
||||
//// Log.d(TAG, "getToken: url = $url")
|
||||
// get(url = url, doSuccess = {
|
||||
// SpTool.put(SpTool.TOKEN, it)
|
||||
// BaseApp.token = it.toString()
|
||||
// }) { code, msg ->
|
||||
// //toast(msg)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// class CallbackImpl(
|
||||
// private val doSuccess: (data: Any) -> Unit,
|
||||
// private val doFailure: (code: Int?, msg: String?) -> Unit
|
||||
// ) :
|
||||
// Callback {
|
||||
// override fun onFailure(call: Call, e: IOException) {
|
||||
// runMainThread {
|
||||
// e.printStackTrace()
|
||||
// doFailure(-1, "服务异常,${e.message}")
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// override fun onResponse(call: Call, response: Response) {
|
||||
// val respData = response.body?.string()
|
||||
// runMainThread {
|
||||
// Log.d("HttpUtil", respData ?: "")
|
||||
// runCatching {
|
||||
// val typeToken = object : TypeToken<BaseReq<Any>>() {}
|
||||
// val baseReq = respData?.toType<BaseReq<Any>>(typeToken = typeToken)
|
||||
// if (baseReq == null) {
|
||||
// doFailure(-1, "查询数据失败")
|
||||
// return@runCatching
|
||||
// }
|
||||
// if (baseReq.code != 200) {
|
||||
// doFailure(baseReq.code, baseReq.message)
|
||||
// //baseReq.code == 500 ||
|
||||
// if (baseReq.code == 401 && loopGetToken) {
|
||||
// exeTokenThread()
|
||||
// }
|
||||
// return@runCatching
|
||||
// }
|
||||
// doSuccess(baseReq.result ?: "")
|
||||
// }.onFailure {
|
||||
// it.printStackTrace()
|
||||
// doFailure(-1, "解析异常")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
//}
|
||||
//
|
||||
//fun String.isHttpApi() = this.startsWith("http://") || this.startsWith("https://")
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.shuwei.dish.match.http
|
||||
|
||||
//import com.shuwei.dish.match.base.BaseApp
|
||||
|
||||
object UrlConfig {
|
||||
// private const val DEVICE_BASE_URL = "http://device.shuziweidao.com:8889"
|
||||
// /**
|
||||
// * device获取token
|
||||
// */
|
||||
// const val DEVICE_TOKEN = "${DEVICE_BASE_URL}/sys/getEquipmentToken"
|
||||
// /**
|
||||
// *获取配置信息
|
||||
// */
|
||||
// const val DEVICE_CONFIG = "${DEVICE_BASE_URL}/equipment/stEquipment/queryByEquipmentCode"
|
||||
|
||||
// const val BASE_URL = "http://vip.shuziweidao.com/shuwei-zhct"
|
||||
// const val DISH_DETAIL = "$BASE_URL/scales/goodsUseList?foodId={foodId}&foodWeight={foodWeight}"
|
||||
// const val DISH_LIST = "$BASE_URL/scales/getRestInfoFoods?eaId=99&type=0&foodName"
|
||||
|
||||
// const val BASE_URL = "http://192.168.1.207:9102"
|
||||
// const val BASE_URL = "https://yyjk.shuziweidao.com/gateway"
|
||||
const val BASE_URL = "https://yyjk.shuziweidao.com/gateway/"
|
||||
// const val DISH_LIST = "$BASE_URL/scales/getRestInfoFoods?eaId=99&type=0&foodName"
|
||||
|
||||
|
||||
// var DISH_DETAIL = "${BaseApp.configUrl}/food/stFoodInfoMatching/queryById"
|
||||
//
|
||||
// // "$BASE_URL/scales/generateToken?deviceId=1111111111111111111111111111"
|
||||
//
|
||||
// var GET_TOKEN = "${BaseApp.configUrl}/restaurant/equipment/stEquipment/getEquipmentToken"
|
||||
// var SUBMIT_DISH = "${BaseApp.configUrl}/food/stFoodInfoMatching/saveoredit"
|
||||
//
|
||||
// var QUERY_GOODS_LIST = "${BaseApp.configUrl}/food/stFoodInfoMatching/queryGoodsInfoList"
|
||||
// var QUERY_FOOD_LIST = "${BaseApp.configUrl}/food/stFoodInfoMatching/list"
|
||||
// var SAMPLING_LIST = "${BaseApp.configUrl}/food/stFoodInfoMatching/queryHistorical/goodsInfoList"
|
||||
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package com.shuwei.dish.match.net
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.util.Log
|
||||
import com.shuwei.dish.match.http.UrlConfig
|
||||
import com.shuwei.dish.match.base.GlobalData
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
@@ -16,7 +16,7 @@ import kotlin.apply
|
||||
import kotlin.jvm.java
|
||||
|
||||
val apiService: ApiService = Retrofit.Builder()
|
||||
.baseUrl(UrlConfig.BASE_URL)
|
||||
.baseUrl(GlobalData.appBaseUrl)
|
||||
// .baseUrl(UrlConfig.DEVICE_BASE_URL)
|
||||
.client(HttpManager.instance.client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
|
||||
Reference in New Issue
Block a user