From ddf79902792143be70cf0fe2993f60c4c8a7afa1 Mon Sep 17 00:00:00 2001 From: br3ant <1106617567@qq.com> Date: Wed, 9 Jun 2021 17:51:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 4 +- gradle/wrapper/gradle-wrapper.properties | 6 +- utils/build.gradle | 22 ++--- .../com/br3ant/utils/rxhttp/RxHttpUtils.java | 2 + .../rxhttp/interceptor/LogInterceptor.kt | 85 +++++++++++++++++++ 5 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 utils/src/main/java/com/br3ant/utils/rxhttp/interceptor/LogInterceptor.kt diff --git a/build.gradle b/build.gradle index be8a1b3..02625ca 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,12 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = "1.4.31" + ext.kotlin_version = "1.5.10" repositories { google() jcenter() } dependencies { - classpath "com.android.tools.build:gradle:4.1.1" + classpath "com.android.tools.build:gradle:4.2.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 360dc4e..4e92d5a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Oct 13 16:43:45 CST 2020 +#Wed Jun 09 17:42:16 CST 2021 distributionBase=GRADLE_USER_HOME +distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip +zipStoreBase=GRADLE_USER_HOME diff --git a/utils/build.gradle b/utils/build.gradle index 2ec32e4..c129451 100644 --- a/utils/build.gradle +++ b/utils/build.gradle @@ -49,31 +49,31 @@ android { dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) - api 'androidx.appcompat:appcompat:1.2.0' + api 'androidx.appcompat:appcompat:1.3.0' api 'androidx.constraintlayout:constraintlayout:2.0.4' //fragmentation - api 'com.github.br3ant:SFragmentation:1.7.7' + api 'com.github.weikaiyun.SFragmentation:fragmentation:1.8.2' //kotlin api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" api "androidx.core:core-ktx:1.3.2" api "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" - api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" - api "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0" - api "androidx.lifecycle:lifecycle-reactivestreams-ktx:2.2.0" - api 'androidx.fragment:fragment-ktx:1.2.5' + api "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1" + api "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1" + api "androidx.lifecycle:lifecycle-reactivestreams-ktx:2.3.1" + api 'androidx.fragment:fragment-ktx:1.3.3' //room - def room_version = '2.2.6' + def room_version = '2.3.0' api "androidx.room:room-runtime:$room_version" api "androidx.room:room-rxjava2:$room_version" api "androidx.room:room-ktx:$room_version" kapt "androidx.room:room-compiler:$room_version" //rxhttp - api 'com.ljx.rxhttp:rxhttp:2.5.6' - kapt 'com.ljx.rxhttp:rxhttp-compiler:2.5.6' //生成RxHttp类,非kotlin项目,请使用annotationProcessor代替kapt + api 'com.ljx.rxhttp:rxhttp:2.5.7' + kapt 'com.ljx.rxhttp:rxhttp-compiler:2.5.7' //生成RxHttp类,非kotlin项目,请使用annotationProcessor代替kapt api 'com.ljx.rxlife:rxlife-coroutine:2.0.1' //管理协程生命周期,页面销毁,关闭请求 api 'com.ljx.rxlife2:rxlife-rxjava:2.0.0' @@ -84,7 +84,7 @@ dependencies { //okhttp api 'com.squareup.okhttp3:okhttp:4.9.1' - api 'com.github.liangjingkanji:BRV:1.3.13' + api 'com.github.liangjingkanji:BRV:1.3.22' //gson api 'com.google.code.gson:gson:2.8.6' @@ -98,7 +98,7 @@ dependencies { api 'com.github.lihangleo2:ShadowLayout:3.1.3' //binding - api 'com.hi-dhl:binding:1.0.9' + api 'com.hi-dhl:binding:1.1.3' //util api 'com.blankj:utilcodex:1.29.0' diff --git a/utils/src/main/java/com/br3ant/utils/rxhttp/RxHttpUtils.java b/utils/src/main/java/com/br3ant/utils/rxhttp/RxHttpUtils.java index 3aeb510..2378e1d 100644 --- a/utils/src/main/java/com/br3ant/utils/rxhttp/RxHttpUtils.java +++ b/utils/src/main/java/com/br3ant/utils/rxhttp/RxHttpUtils.java @@ -1,6 +1,7 @@ package com.br3ant.utils.rxhttp; import com.br3ant.utils.GsonUtil; +import com.br3ant.utils.rxhttp.interceptor.LogInterceptor; import java.util.concurrent.TimeUnit; @@ -22,6 +23,7 @@ public class RxHttpUtils { public static void init() { RxHttp.init(new OkHttpClient.Builder() + .addInterceptor(new LogInterceptor()) .readTimeout(20000, TimeUnit.MILLISECONDS) .writeTimeout(20000, TimeUnit.MILLISECONDS) .connectTimeout(20000, TimeUnit.MILLISECONDS).build()); diff --git a/utils/src/main/java/com/br3ant/utils/rxhttp/interceptor/LogInterceptor.kt b/utils/src/main/java/com/br3ant/utils/rxhttp/interceptor/LogInterceptor.kt new file mode 100644 index 0000000..58aa130 --- /dev/null +++ b/utils/src/main/java/com/br3ant/utils/rxhttp/interceptor/LogInterceptor.kt @@ -0,0 +1,85 @@ +package com.br3ant.utils.rxhttp.interceptor + +import com.blankj.utilcode.util.LogUtils +import okhttp3.Interceptor +import okhttp3.Request +import okhttp3.Response +import okio.Buffer +import java.io.EOFException +import java.io.IOException +import java.nio.charset.StandardCharsets + + +class LogInterceptor : Interceptor { + + @Throws(IOException::class) + override fun intercept(chain: Interceptor.Chain): Response { + val request: Request = chain.request() + logForRequest(request) + val response: Response = chain.proceed(request) + logForResponse(response) + return response + } + + + private fun logForRequest(request: Request) { + try { + val requestBody = request.body + if (requestBody != null) { + val buffer = Buffer() + requestBody.writeTo(buffer) + val charset = requestBody.contentType()?.charset(UTF8) + val body = buffer.readString(charset ?: UTF8) + LogUtils.iTag(TAG, "method = ${request.method} -- url = ${request.url}?${body}") + } else { + LogUtils.iTag(TAG, "method = ${request.method} -- url = ${request.url}") + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + private fun logForResponse(response: Response?) { + val body = response?.body ?: return + try { + val source = body.source() + source.request(Long.MAX_VALUE) + val buffer = source.buffer + if (!isPlaintext(buffer)) { + return + } + if (body.contentLength() != 0L) { + val string = buffer.clone().readString(body.contentType()?.charset(UTF8) ?: UTF8) + LogUtils.iTag(TAG, "response = $string") + } + } catch (e: Exception) { + e.printStackTrace() + } + } + + private fun isPlaintext(buffer: Buffer): Boolean { + return try { + val prefix = Buffer() + val byteCount = if (buffer.size < 64) buffer.size else 64 + buffer.copyTo(prefix, 0, byteCount) + for (i in 0..15) { + if (prefix.exhausted()) { + break + } + val codePoint = prefix.readUtf8CodePoint() + if (Character.isISOControl(codePoint) && !Character.isWhitespace(codePoint)) { + return false + } + } + true + } catch (e: EOFException) { + false + } + } + + + companion object { + private val UTF8 = StandardCharsets.UTF_8 + private const val TAG = "OkHttp" + } +} \ No newline at end of file