android app配置抓包:测试debug可抓包,正式release不可抓包

文章介绍了在Android项目中,针对debug和release两种构建类型创建不同的网络安全配置文件。debug版本允许明文流量并信任系统和用户证书,而release版本仅允许明文流量。配置在AndroidManifest.xml中,并在build.gradle中根据构建类型指定对应的XML资源。

res/xml/目录下新增两个文件

network_security_config_debug.xml

debug文件内容:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" >
    <trust-anchors>
        <certificates src="system" overridePins="true" /> <!--信任系统证书-->
        <certificates src="user" overridePins="true" /> <!--信任用户证书-->
    </trust-anchors>
    </base-config>
</network-security-config>

network_security_config_release.xml

release文件内容:

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

下一步:在AndroidManifest.xml中的application标签中,配置

<application 
android:networkSecurityConfig="@xml/network_security_config"

/>

build.gradle中配置

android{

buildTypes {
        release {
            resValue "xml", "network_security_config",                 "@xml/network_security_config_release"
        }
        debug {
            resValue "xml", "network_security_config",         "@xml/network_security_config_debug"

                }

}

<think>我们正在解决用户问题:"我想在AndroidStudio中使用Logcat捕获网络接口请求信息"用户希望了解如何通过AndroidStudio的Logcat工具来捕获网络请求信息。分析:在Android开发中,网络请求通常使用HttpURLConnection、OkHttp等库。为了在Logcat中查看网络请求信息,我们通常需要添加网络拦截器(Interceptor)来打印日志。参考引用:引用[1]提到了AndroidStudio3.0及更高版本中关于设备运行Android8.0以上时,可以随时查看对象分配。但这不是直接关于网络请求的。引用[2]提到了Adapter类,与网络请求日志无关。引用[3]提到了一些关于视图逻辑的信息,与网络请求无关。引用[4]提到了游戏框架和输入设置,与问题无关。引用[5]提到了Django相关的内容,与Android无关。因此,我们主要依靠自己的知识来回答。解决方案:1.使用OkHttp的日志拦截器(LoggingInterceptor)是最常见的方法。2.在Retrofit或OkHttpClient中添加拦截器,将网络请求的详细信息打印到Logcat。步骤:对于使用OkHttp的情况:a.添加OkHttp和日志拦截器的依赖b.创建OkHttpClient实例时添加日志拦截器c.设置日志拦截器的级别注意:在发布版本中应该移除日志拦截器,以免泄露敏感信息。具体操作:1.添加依赖(build.gradle):```groovyimplementation'com.squareup.okhttp3:okhttp:4.9.0'implementation'com.squareup.okhttp3:logging-interceptor:4.9.0'```2.创建带有日志拦截器的OkHttpClient:```kotlinvalloggingInterceptor=HttpLoggingInterceptor().apply{level=HttpLoggingInterceptor.Level.BODY//设置日志级别为BODY,可以打印请求和响应的body}valclient=OkHttpClient.Builder().addInterceptor(loggingInterceptor).build()```3.将client设置到Retrofit中(如果使用Retrofit)或者直接使用OkHttpClient进行请求。日志级别:-NONE:无日志-BASIC:请求/响应行-HEADERS:请求/响应行和头-BODY:请求/响应行、头和body4.在Logcat中查看:使用TAG"OkHttp"过滤日志。如果不使用OkHttp,而是使用HttpURLConnection,则需要自己手动打印日志,比较繁琐。另外,也可以使用Stetho等工具,但用户要求使用Logcat,所以推荐使用拦截器。注意事项:-日志级别为BODY时,可能会打印出大量的数据,在调试时注意。-在生产环境中务必移除日志拦截器。如果用户使用的是其他网络库,可以类似地添加日志功能。因此,回答如下:</think>### 在 Android Studio 中使用 Logcat 捕获网络请求信息 要在 Android Studio 中通过 Logcat 捕获网络请求信息,主要有以下两种方法: #### ✅ 方法 1:使用网络拦截器(推荐) 这是最常用的方案,通过添加网络日志拦截器自动打印所有网络请求: 1. **添加依赖**(`build.gradle`): ```gradle implementation 'com.squareup.okhttp3:logging-interceptor:4.9.3' ``` 2. **配置 OkHttp 拦截器**: ```kotlin // 创建日志拦截器 val loggingInterceptor = HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY // 打印请求/响应头和内容 } // 添加到 OkHttpClient val client = OkHttpClient.Builder() .addInterceptor(loggingInterceptor) .build() // 应用于 Retrofit Retrofit.Builder() .client(client) .baseUrl("https://api.example.com/") .build() ``` 3. **在 Logcat 中过滤日志**: - 使用过滤器标签:`OkHttp` - 或搜索关键词:`D/OkHttp` #### ✅ 方法 2:手动添加日志输出 在关键网络请求节点插入日志语句: ```kotlin // 发起请求时 Log.d("NETWORK", "请求URL: ${request.url}") Log.d("NETWORK", "请求参数: ${request.body}") // 收到响应时 Log.d("NETWORK", "响应码: ${response.code}") Log.d("NETWORK", "响应体: ${response.body?.string()}") ``` #### ⚠️ 注意事项 1. **敏感信息保护**: - 生产环境务必移除 `Level.BODY` 或使用 `if (BuildConfig.DEBUG)` 包裹 - 避免泄露 token、密码等敏感数据 2. **日志级别建议**: ```kotlin HttpLoggingInterceptor.Level.HEADERS // 打印头信息(较安全) HttpLoggingInterceptor.Level.BASIC // 基础信息(URL/方法/状态码) ``` 3. **Logcat 使用技巧**: - 过滤器语法:`tag:NETWORK` 或 `package:com.your.app` - 启用正则过滤:`.*Request.*` - 保存日志:工具栏 `Export Logcat to File` > **原理说明**:OkHttp 的日志拦截器会在网络请求的生命周期中自动插入日志点[^1],而 Logcat 作为系统级日志工具会捕获这些输出。Android 8.0+ 设备即使不连接调试器也能记录分配信息[^1]。 #### 效果示例 ``` D/OkHttp: --> POST https://api.example.com/login D/OkHttp: Content-Type: application/json D/OkHttp: {"user":"test@email.com"} D/OkHttp: <-- 200 https://api.example.com/login (1200ms) D/OkHttp: {"token": "eyJhbGci...", "expires": 3600} ``` --- ### 相关问题 1. **如何防止网络日志泄露敏感数据?** 2. **除了 Logcat,还有哪些 Android 网络调试工具?** 3. **OkHttp 拦截器除了记录日志还能实现什么功能?** 4. **如何在 Release 版本自动禁用网络日志?** 5. **如何解析 Logcat 中的加密 HTTPS 请求内容?** [^1]: Android Studio 3.0 新功能初体验。If your device is running Android 8.0 or higher, you can view your object allocations at any time as follows: Just click-and-hold in the timeline and drag to select the region where you want to view the allocations (as shown in video 1). There’s no need to begin a recording session because Android 8.0 and higher includes an on-device profiling tool that constantly tracks your app’s allocations.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值