This commit is contained in:
zhongchao
2021-01-28 16:13:08 +08:00
5 changed files with 23 additions and 16 deletions

View File

@@ -23,8 +23,8 @@ class OkHttpFactory private constructor() {
val okHttpClient: OkHttpClient by lazy { val okHttpClient: OkHttpClient by lazy {
OkHttpClient.Builder() OkHttpClient.Builder()
.addNetworkInterceptor(HttpHeaderInterceptor()) .addNetworkInterceptor(HttpHeaderInterceptor())
.addNetworkInterceptor(HttpLoggingInterceptor())
.addInterceptor(HttpDnsInterceptor()) .addInterceptor(HttpDnsInterceptor())
.addInterceptor(HttpLoggingInterceptor())
.sslSocketFactory(createSSLSocketFactory(), createTrustAllManager()) .sslSocketFactory(createSSLSocketFactory(), createTrustAllManager())
.hostnameVerifier(SSLSocketFactoryUtils.TrustAllHostnameVerifier()) .hostnameVerifier(SSLSocketFactoryUtils.TrustAllHostnameVerifier())
.connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS) .connectTimeout(CONNECT_TIMEOUT, TimeUnit.MILLISECONDS)

View File

@@ -5,6 +5,7 @@ import com.mogo.utils.logger.Logger
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Request import okhttp3.Request
import okhttp3.Response import okhttp3.Response
import java.lang.Exception
/** /**
* created by wujifei on 2021/1/27 15:57 * created by wujifei on 2021/1/27 15:57
@@ -13,14 +14,16 @@ import okhttp3.Response
class HttpDnsInterceptor : Interceptor { class HttpDnsInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
val request: Request = chain.request() val request: Request = chain.request()
val path: String = request.url().encodedPath() var url = request.url().toString()
val query: String? = request.url().encodedQuery() val host = request.url().host()
val host = "http://" + MogoHttpDnsClient.getHttpDnsAddressUseCacheIfNecessary(0, request.url().host().replace("http://", "").replace("https://", "")) try {
var url = host + path MogoHttpDnsClient.getHttpDnsAddressUseCacheIfNecessary(0, request.url().host())?.let {
query?.let { url = url.replace(host, it)
url=url+"?"+query Logger.d("DomainExchange", """oriHost: ${host} newHost: $it newUrl: $url""")
}
} catch (e: Exception) {
Logger.d("DomainExchange", e.toString())
} }
Logger.d("DomainExchange", """oriHost: ${request.url().host().toString()} newHost: $host newUrl: $url""")
return chain.proceed(request.newBuilder().url(url).build()) return chain.proceed(request.newBuilder().url(url).build())
} }
} }

View File

@@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit
/** /**
* created by wujifei on 2021/1/27 14:33 * created by wujifei on 2021/1/27 14:33
* describe: * describe:网络日志拦截
*/ */
class HttpLoggingInterceptor : Interceptor { class HttpLoggingInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
@@ -18,7 +18,7 @@ class HttpLoggingInterceptor : Interceptor {
val requestBody = request.body() val requestBody = request.body()
val hasRequestBody = requestBody != null val hasRequestBody = requestBody != null
var protocol = Protocol.HTTP_1_1.toString() var protocol = Protocol.HTTP_1_1.toString()
var body = "" var body = "null"
if (chain.connection() != null && chain.connection()!!.protocol() != null) { if (chain.connection() != null && chain.connection()!!.protocol() != null) {
protocol = chain.connection()!!.protocol().toString() protocol = chain.connection()!!.protocol().toString()
} }
@@ -44,8 +44,8 @@ class HttpLoggingInterceptor : Interceptor {
val logMsg = StringBuilder() val logMsg = StringBuilder()
.append("------> http request start").append("\r\n") .append("------> http request start").append("\r\n")
.append(protocol).append(", ") .append(protocol).append(", ")
.append(request.method()).append(", ") .append(request.method()).append("\r\n")
.append("Request Headers: ").append(request.headers().toString()).append("\r\n") .append("Request Headers: {").append(request.headers().toString().replace("\n",", ")).append("}\r\n")
.append("Url: ").append(request.url()).append("\r\n") .append("Url: ").append(request.url()).append("\r\n")
.append("Content-Type: ").append(requestBody?.contentType()).append("\r\n") .append("Content-Type: ").append(requestBody?.contentType()).append("\r\n")
.append("Content-Length: ").append(requestBody?.contentLength()).append("\r\n") .append("Content-Length: ").append(requestBody?.contentLength()).append("\r\n")
@@ -78,7 +78,7 @@ class HttpLoggingInterceptor : Interceptor {
.append("Response Content: ").append(responseContent).append("\r\n") .append("Response Content: ").append(responseContent).append("\r\n")
.append("Content-Type: ").append(contentType).append("\r\n") .append("Content-Type: ").append(contentType).append("\r\n")
.append("Content-Length: ").append(bodySize).append("\r\n") .append("Content-Length: ").append(bodySize).append("\r\n")
.append(" (").append(endTime).append("ms)").append("\r\n") .append("Time: (").append(endTime).append("ms)").append("\r\n")
.append("------> http response end") .append("------> http response end")
Logger.d(TAG, logMsg.toString()) Logger.d(TAG, logMsg.toString())
return if (consumedResponse) response.newBuilder().body(ResponseBody.create(contentType, responseContent)).build() else response return if (consumedResponse) response.newBuilder().body(ResponseBody.create(contentType, responseContent)).build() else response

View File

@@ -4,15 +4,18 @@ import android.content.Context;
import android.util.Log; import android.util.Log;
import com.mogo.cloud.commons.network.BaseData; import com.mogo.cloud.commons.network.BaseData;
import com.mogo.cloud.commons.network.NetConstants;
import com.mogo.cloud.commons.network.RetrofitFactory; import com.mogo.cloud.commons.network.RetrofitFactory;
import com.mogo.cloud.passport.MoGoAiCloudClient; import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.tanlu.api.IRoadInfoSearchCallback; import com.mogo.cloud.tanlu.api.IRoadInfoSearchCallback;
import com.mogo.cloud.tanlu.bean.RoadInfoRequest; import com.mogo.cloud.tanlu.bean.RoadInfoRequest;
import com.mogo.cloud.tanlu.bean.RoadInfos; import com.mogo.cloud.tanlu.bean.RoadInfos;
import com.mogo.cloud.tanlu.bean.location.Location;
import com.mogo.cloud.tanlu.constant.HttpConstant; import com.mogo.cloud.tanlu.constant.HttpConstant;
import com.mogo.cloud.tanlu.net.TanluApiService; import com.mogo.cloud.tanlu.net.TanluApiService;
import com.mogo.utils.network.utils.GsonUtil; import com.mogo.utils.network.utils.GsonUtil;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -48,7 +51,7 @@ public class MogoRoadSearchManager {
} }
public void init () { public void init () {
apiService = RetrofitFactory.INSTANCE.getInstance(HttpConstant.DEV_HOTS) apiService = RetrofitFactory.INSTANCE.getInstance(NetConstants.LAUNCHER_SNAPSHOT_HOST)
.create(TanluApiService.class); .create(TanluApiService.class);
} }
@@ -67,7 +70,7 @@ public class MogoRoadSearchManager {
// location.setLatitude(39.968139); // location.setLatitude(39.968139);
// location.setLongitude(116.411468); // location.setLongitude(116.411468);
// RoadInfoRequest request = new RoadInfoRequest(location, "", list, false, false); // RoadInfoRequest request = new RoadInfoRequest(location, "", list, false, false);
//
// Map<String, Object> map = new HashMap<>(); // Map<String, Object> map = new HashMap<>();
// map.put("sn", "F803EB2046PZD00228"); // map.put("sn", "F803EB2046PZD00228");
// map.put("data", GsonUtil.jsonFromObject(request)); // map.put("data", GsonUtil.jsonFromObject(request));

View File

@@ -5,6 +5,7 @@ import android.util.Log;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.mogo.cloud.commons.network.BaseData; import com.mogo.cloud.commons.network.BaseData;
import com.mogo.cloud.commons.network.NetConstants;
import com.mogo.cloud.commons.network.RetrofitFactory; import com.mogo.cloud.commons.network.RetrofitFactory;
import com.mogo.cloud.passport.MoGoAiCloudClient; import com.mogo.cloud.passport.MoGoAiCloudClient;
import com.mogo.cloud.tanlu.api.ITanluUploadCallback; import com.mogo.cloud.tanlu.api.ITanluUploadCallback;
@@ -46,7 +47,7 @@ public class UploadManager {
} }
public void init () { public void init () {
apiService = RetrofitFactory.INSTANCE.getInstance(HttpConstant.DEV_HOTS) apiService = RetrofitFactory.INSTANCE.getInstance(NetConstants.DEVA_HOST)
.create(TanluApiService.class); .create(TanluApiService.class);
} }