Skip to content

Commit a0ff9b9

Browse files
committed
fix:1、添加Proxy.NO_PROXY
1 parent 7f22e51 commit a0ff9b9

5 files changed

Lines changed: 43 additions & 8 deletions

File tree

.idea/sonarlint/issuestore/c/f/cf2ccc77107ade4b4a9b0b7e8e988c2cb28568fa

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sonarlint/issuestore/e/f/ef64fd164d2083a14e4ffe5a00abb31194c0a8ef

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FlowHttp/src/main/java/com/bhm/network/core/GenerateOkHttpClient.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
44
import com.bhm.network.core.HttpCache.getCache
55
import com.bhm.network.core.interceptor.*
66
import okhttp3.OkHttpClient
7+
import java.net.Proxy
78
import java.security.SecureRandom
89
import java.security.cert.X509Certificate
910
import java.util.concurrent.TimeUnit
@@ -13,7 +14,7 @@ import javax.net.ssl.*
1314
* @author Buhuiming
1415
*/
1516
@SuppressLint("CustomX509TrustManager", "TrustAllX509TrustManager")
16-
class GenerateOkHttpClient {
17+
open class GenerateOkHttpClient {
1718

1819
private var timeOutRead = 30 //读取超时
1920

@@ -29,7 +30,11 @@ class GenerateOkHttpClient {
2930
if (builder.connectTimeOut > 0) {
3031
timeOutConnection = builder.connectTimeOut
3132
}
32-
return OkHttpClient.Builder()
33+
val clientBuilder = OkHttpClient.Builder()
34+
if (builder.noProxy) {
35+
clientBuilder.proxy(Proxy.NO_PROXY)
36+
}
37+
return clientBuilder
3338
.sslSocketFactory(unsafeOkHttpClient, object : X509TrustManager {
3439
override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {}
3540
override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {}

FlowHttp/src/main/java/com/bhm/network/core/HttpOptions.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class HttpOptions(private val builder: Builder) {
7272
get() = builder.successCode
7373
val parseDataKey: Boolean
7474
get() = builder.parseDataKey
75+
val noProxy: Boolean
76+
get() = builder.noProxy
7577

7678
class Builder(val activity: FragmentActivity) {
7779
internal var isShowDialog = HttpConfig.isShowDialog
@@ -97,6 +99,7 @@ class HttpOptions(private val builder: Builder) {
9799
internal var successCode = HttpConfig.successCode
98100
internal var parseDataKey = HttpConfig.parseDataKey
99101
internal var jobKey = System.currentTimeMillis().toString()
102+
internal var noProxy = true
100103

101104
init {
102105
activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
@@ -155,6 +158,13 @@ class HttpOptions(private val builder: Builder) {
155158
this.connectTimeOut = connectTimeOut
156159
}
157160

161+
/**
162+
* 设置应用层是否可以抓包
163+
*/
164+
fun setNoProxy(noProxy: Boolean) = apply {
165+
this.noProxy = noProxy
166+
}
167+
158168
/** 不推荐使用,使用此方法,将取消默认的设置,包括但不限于日志,缓存,下载,上传,网络,SSL。
159169
* @param okHttpClient
160170
*/

FlowHttp/src/main/java/com/bhm/network/core/RetrofitHelper.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,33 @@ import com.bhm.network.adapter.IntegerDefaultAdapter
44
import com.bhm.network.adapter.LongDefaultAdapter
55
import com.google.gson.Gson
66
import com.google.gson.GsonBuilder
7+
import okhttp3.OkHttpClient
78
import retrofit2.Retrofit
89

910
/**
1011
* Created by bhm on 2023/5/6.
1112
*/
1213
class RetrofitHelper(private val builder: HttpOptions) {
1314

15+
fun <T> createRequest(clazz: Class<T>, url: String, client: OkHttpClient): T {
16+
if (builder.isShowDialog && null != builder.dialog) {
17+
builder.dialog?.showLoading(builder)
18+
}
19+
val retrofit = Retrofit.Builder()
20+
.baseUrl(url)
21+
.client(client)
22+
.addConverterFactory(NetCoreConverterFactory.create(
23+
gsonBuilder,
24+
builder.messageKey,
25+
builder.codeKey,
26+
builder.dataKey,
27+
builder.successCode,
28+
builder.parseDataKey)
29+
)
30+
.build()
31+
return retrofit.create(clazz)
32+
}
33+
1434
fun <T> createRequest(clazz: Class<T>, url: String): T {
1535
if (builder.isShowDialog && null != builder.dialog) {
1636
builder.dialog?.showLoading(builder)

0 commit comments

Comments
 (0)