@@ -8,13 +8,18 @@ import me.hgj.jetpackmvvm.util.CharacterHandler.Companion.xmlFormat
8
8
import me.hgj.jetpackmvvm.util.LogUtils
9
9
import okhttp3.MediaType
10
10
import okhttp3.Request
11
+ import java.security.MessageDigest
12
+ import java.security.NoSuchAlgorithmException
11
13
12
14
/* *
13
15
* 作者 : hegaojian
14
16
* 时间 : 2020/3/26
15
17
* 描述 :
16
18
*/
17
- class DefaultFormatPrinter : FormatPrinter {
19
+ class DefaultFormatPrinter : FormatPrinter {
20
+
21
+ private var appendTag = " "
22
+
18
23
/* *
19
24
* 打印网络请求信息, 当网络请求时 {[okhttp3.RequestBody]} 可以解析的情况
20
25
*
@@ -25,6 +30,7 @@ class DefaultFormatPrinter : FormatPrinter{
25
30
request : Request ,
26
31
bodyString : String
27
32
) {
33
+ appendTag = md5(URL_TAG + request.url())
28
34
val requestBody =
29
35
LINE_SEPARATOR + BODY_TAG + LINE_SEPARATOR + bodyString
30
36
val tag = getTag(true )
@@ -53,6 +59,7 @@ class DefaultFormatPrinter : FormatPrinter{
53
59
* @param request
54
60
*/
55
61
override fun printFileRequest (request : Request ) {
62
+ appendTag = md5(URL_TAG + request.url())
56
63
val tag = getTag(true )
57
64
LogUtils .debugInfo(tag, REQUEST_UP_LINE )
58
65
logLines(
@@ -97,6 +104,7 @@ class DefaultFormatPrinter : FormatPrinter{
97
104
message : String ,
98
105
responseUrl : String
99
106
) {
107
+ appendTag = md5(URL_TAG + responseUrl)
100
108
var bodyString = bodyString
101
109
bodyString =
102
110
when {
@@ -155,6 +163,7 @@ class DefaultFormatPrinter : FormatPrinter{
155
163
message : String ,
156
164
responseUrl : String
157
165
) {
166
+ appendTag = md5(URL_TAG + responseUrl)
158
167
val tag = getTag(false )
159
168
val urlLine = arrayOf<String ?>(
160
169
URL_TAG + responseUrl,
@@ -182,6 +191,14 @@ class DefaultFormatPrinter : FormatPrinter{
182
191
LogUtils .debugInfo(tag, END_LINE )
183
192
}
184
193
194
+ private fun getTag (isRequest : Boolean ): String {
195
+ return if (isRequest) {
196
+ " $TAG -Request-$appendTag "
197
+ } else {
198
+ " $TAG -Response-$appendTag "
199
+ }
200
+ }
201
+
185
202
companion object {
186
203
private const val TAG = " HttpLog"
187
204
private val LINE_SEPARATOR = System .getProperty(" line.separator" )
@@ -349,12 +366,30 @@ class DefaultFormatPrinter : FormatPrinter{
349
366
return builder.toString()
350
367
}
351
368
352
- private fun getTag (isRequest : Boolean ): String {
353
- return if (isRequest) {
354
- " $TAG -Request"
355
- } else {
356
- " $TAG -Response"
369
+ /* *
370
+ * md5加密
371
+ */
372
+ private fun md5 (string : String ): String {
373
+ if (TextUtils .isEmpty(string)) {
374
+ return " "
375
+ }
376
+ val md5: MessageDigest
377
+ try {
378
+ md5 = MessageDigest .getInstance(" MD5" )
379
+ val bytes = md5.digest(string.toByteArray())
380
+ val result = java.lang.StringBuilder ()
381
+ for (b in bytes) {
382
+ var temp = Integer .toHexString(b.toInt() and 0xff )
383
+ if (temp.length == 1 ) {
384
+ temp = " 0$temp "
385
+ }
386
+ result.append(temp)
387
+ }
388
+ return result.toString()
389
+ } catch (e: NoSuchAlgorithmException ) {
390
+ e.printStackTrace()
357
391
}
392
+ return " "
358
393
}
359
394
}
360
395
}
0 commit comments