使用 Retrofit(Okhttp) 时,如何从 android studio 中的 log cat 复制请求 body、headers(无标签)?

How copy request body, headers (without tags) from log cat in android studio when using Retrofit(Okhttp)?

我无法从 log cat 复制请求 body(没有标签)。我认为这是非常有用的功能,因为您经常需要通过邮递员检查请求,并且很难再发出类似的请求(特别是如果请求是数字签名的)。这是我想在没有 D: 前缀的情况下复制的日志示例:

D: --> POST http://url
D: Content-Type: application/xml; charset=utf-8
D: Content-Length: 3586
D: <?xml version="1.0" encoding="UTF-8"?><APRequest>
D: <jsonString>{"searchDate":"24.01.2019","userOrgCode":"19710003","userId":"5000000000014720"}</jsonString>
D: <dateOfSend>1548316774835</dateOfSend>
D: <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
D: <ds:SignedInfo>
D: <ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/>
D: <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
D: <ds:Reference URI="">
D: <ds:Transforms>
D: <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/>
D: <ds:Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/>
D: </ds:Transforms>
D: <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
D: <ds:DigestValue>KK5lQsy9CqracgR1YS2nZBkY2gU4ntOCtAgbPQeI7AU=</ds:DigestValue>
D: </ds:Reference>
D: </ds:SignedInfo>
D: <ds:SignatureValue>
D: T1CWxz0kgoKKFFwCBaA+oOfMunnzjF4fZEWoB5ll+Sdm6ql/mcHtJUEcVVF2MgwzYVP9O0TI5iiD
D: VGg9in36dC6WcCqb9UmNJM/I8KM1CDszwAWHLSto7dgSlE1DgXlImI2DhexcrXQyMyXVboQOPrHO
D: qbtT1Ou8hL+M8j/jxe5HMyANH55/3x83/8aae5GBxAVsaCyfJR5wWe4FXdDBRHU65RFe/JxRrDo3
D: 3sqed8P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ILOFZdbTcDZz5OA/MuqKo06S
D: 7nbl89fn4qyhWSvQLwkeWQu5z2elqzgnT19wE8rUC7Q+lsX/xikKkBGweAZT4wYShB5x8opCnRfL
D: sLlo1Rs0VRN16U9EmcgWy4mP5o+QuHWAJyUiYy69WumTjQyvz5ss/vJoLsqzPl0s8uW5bQkg2OXC
D: O/MFXxnc1k7n81RzRbNx+AIn8B8f8W6qfOZ3ighgSw8hweIvtMjtN9xKYY+DRH781QeSVFfb3uUg
D: VEByOAuXc1U+F/v6TFh5DEZZaVRQrIT6OFAf2RHRVlJognrtVyK+7Dr3C0Hi
D: </ds:X509Certificate>
D: </ds:X509Data>
D: </ds:KeyInfo>
D: </ds:Signature></APRequest>
D: --> END POST (3586-byte body)
D: <-- 200 OK http://url (259ms)

现在是否可以复制不带 D:、I: 等标签的日志?或者在使用 okhttp3 改造时是否可以单行打印日志?

有很多用于分析(拦截)网络请求的第 3 方库

如果使用 okhttp 作为基础

  1. stetho
  2. chuck
  3. okhttp-logging-interceptor
  4. LoggingInterceptor

在你的情况下你可以使用这个LoggingInterceptor