0


Android okhttp 网络链接各阶段监控

步骤 1: 添加依赖

在项目的

  1. build.gradle

文件中,添加 OkHttp 依赖:

  1. implementation 'com.squareup.okhttp3:okhttp:4.11.0'

步骤 2: 创建自定义的 EventListener

创建一个自定义的

  1. EventListener

类:

  1. import android.util.Log
  2. import okhttp3.*
  3. import java.io.IOException
  4. import java.net.InetAddress
  5. import java.net.InetSocketAddress
  6. import java.net.Proxy
  7. class LoggingEventListener : EventListener() {
  8. private var mCallStartTime: Long? = null
  9. private var mCallEndTime: Long? = null
  10. private var mDnsStartTime: Long? = null
  11. private var mDnsEndTime: Long? = null
  12. private var mConnectStartTime: Long? = null
  13. private var mSecureConnectStartTime: Long? = null
  14. private var mSecureConnectEndTime: Long? = null
  15. private var mConnectEndTime: Long? = null
  16. private var mRequestHeadersStartTime: Long? = null
  17. private var mRequestHeadersEndTime: Long? = null
  18. private var mResponseHeadersStartTime: Long? = null
  19. private var mResponseHeadersEndTime: Long? = null
  20. private var mResponseBodyStartTime: Long? = null
  21. private var mResponseBodyEndTime: Long? = null
  22. companion object {
  23. const val TAG = "LoggingEventListener"
  24. }
  25. override fun callStart(call: Call) {
  26. mCallStartTime = System.currentTimeMillis()
  27. Log.i(TAG, "Event callStart at $mCallStartTime")
  28. }
  29. override fun dnsStart(call: Call, domainName: String) {
  30. mDnsStartTime = System.currentTimeMillis()
  31. Log.i(TAG, "Event dnsStart at $mDnsStartTime")
  32. }
  33. override fun dnsEnd(call: Call, domainName: String, inetAddressList: List<InetAddress>) {
  34. mDnsEndTime = System.currentTimeMillis()
  35. Log.i(TAG, "Event dnsEnd at $mDnsEndTime dnsDuration is ${mDnsEndTime!! - mDnsStartTime!!} ms")
  36. }
  37. override fun connectStart(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy) {
  38. mConnectStartTime = System.currentTimeMillis()
  39. Log.i(TAG, "Event connectStart at $mConnectStartTime")
  40. }
  41. override fun secureConnectStart(call: Call) {
  42. mSecureConnectStartTime = System.currentTimeMillis()
  43. Log.i(TAG, "Event secureConnectStart at $mSecureConnectStartTime")
  44. }
  45. override fun secureConnectEnd(call: Call, handshake: Handshake?) {
  46. mSecureConnectEndTime = System.currentTimeMillis()
  47. Log.i(TAG, "Event secureConnectEnd at $mSecureConnectEndTime secureConnectDuration is ${mSecureConnectEndTime!! - mSecureConnectStartTime!!} ms")
  48. }
  49. override fun connectEnd(call: Call, inetSocketAddress: InetSocketAddress, proxy: Proxy, protocol: Protocol?) {
  50. mConnectEndTime = System.currentTimeMillis()
  51. Log.i(TAG, "Event connectEnd at $mConnectEndTime ConnectDuration is ${mConnectEndTime!! - mConnectStartTime!!} ms")
  52. }
  53. override fun requestHeadersStart(call: Call) {
  54. mRequestHeadersStartTime = System.currentTimeMillis()
  55. Log.i(TAG, "Event requestHeadersStart at $mRequestHeadersStartTime")
  56. }
  57. override fun requestHeadersEnd(call: Call, request: Request) {
  58. mRequestHeadersEndTime = System.currentTimeMillis()
  59. Log.i(TAG, "Event requestHeadersEnd at $mRequestHeadersEndTime requestHeadersDuration is ${mRequestHeadersEndTime!! - mRequestHeadersStartTime!!} ms")
  60. }
  61. override fun responseHeadersStart(call: Call) {
  62. mResponseHeadersStartTime = System.currentTimeMillis()
  63. Log.i(TAG, "Event responseHeadersStart at $mResponseHeadersStartTime")
  64. }
  65. override fun responseHeadersEnd(call: Call, response: Response) {
  66. mResponseHeadersEndTime = System.currentTimeMillis()
  67. Log.i(TAG, "Event responseHeadersEnd at $mResponseHeadersEndTime responseHeadersDuration is ${mResponseHeadersEndTime!! - mResponseHeadersStartTime!!} ms")
  68. }
  69. override fun responseBodyStart(call: Call) {
  70. mResponseBodyStartTime = System.currentTimeMillis()
  71. Log.i(TAG, "Event responseBodyStart at $mResponseBodyStartTime")
  72. }
  73. override fun responseBodyEnd(call: Call, byteCount: Long) {
  74. mResponseBodyEndTime = System.currentTimeMillis()
  75. Log.i(TAG, "Event responseBodyEnd at $mResponseBodyEndTime responseHeadersDuration is ${mResponseBodyEndTime!! - mResponseBodyStartTime!!} ms")
  76. }
  77. override fun callEnd(call: Call) {
  78. mCallEndTime = System.currentTimeMillis()
  79. Log.i(TAG, "Event callEnd at $mCallEndTime responseHeadersDuration is ${mCallEndTime!! - mCallStartTime!!} ms")
  80. }
  81. override fun callFailed(call: Call, ioe: IOException) {
  82. Log.i(TAG,"Request Failed: ${ioe.message}")
  83. }
  84. }

步骤 3: 在 MainActivity 中发起网络请求

创建一个简单的

  1. MainActivity

,发起 HTTP 请求并使用

  1. LoggingEventListener

  1. import androidx.appcompat.app.AppCompatActivity
  2. import android.os.Bundle
  3. import android.util.Log
  4. import okhttp3.*
  5. import java.io.IOException
  6. class MainActivity : AppCompatActivity() {
  7. companion object {
  8. const val TAG = "MainActivity"
  9. }
  10. override fun onCreate(savedInstanceState: Bundle?) {
  11. super.onCreate(savedInstanceState)
  12. setContentView(R.layout.activity_main)
  13. // 创建 OkHttpClient 并设置自定义 EventListener
  14. val client = OkHttpClient.Builder()
  15. .eventListener(LoggingEventListener())
  16. .build()
  17. // 创建请求对象
  18. val request = Request.Builder()
  19. .url("https://jsonplaceholder.typicode.com/posts/1") // 示例 API
  20. .build()
  21. // 发起异步网络请求
  22. client.newCall(request).enqueue(object : Callback {
  23. override fun onFailure(call: Call, e: IOException) {
  24. Log.i(TAG, "Request Failed: ${e.message}")
  25. }
  26. override fun onResponse(call: Call, response: Response) {
  27. Log.i(TAG, "Response: ${response.body?.string()}")
  28. }
  29. })
  30. }
  31. }

步骤 4: 添加权限

  1. AndroidManifest.xml

中添加网络权限:

  1. <uses-permission android:name="android.permission.INTERNET" />

步骤 5: 布局文件

创建简单的布局文件

  1. activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:gravity="center"
  7. android:padding="16dp">
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="OkHttp EventListener Demo"
  12. android:textSize="18sp"
  13. android:textStyle="bold" />
  14. </LinearLayout>

运行结果

运行应用后,打开日志工具(Logcat)可以看到网络请求的各个阶段日志,例如:

  1. I/LoggingEventListener: Event callStart at 1731855030207
  2. I/LoggingEventListener: Event dnsStart at 1731855030212
  3. I/LoggingEventListener: Event dnsEnd at 1731855030213 dnsDuration is 1 ms
  4. I/LoggingEventListener: Event connectStart at 1731855030215
  5. I/LoggingEventListener: Event secureConnectStart at 1731855030581
  6. I/LoggingEventListener: Event secureConnectEnd at 1731855030878 secureConnectDuration is 297 ms
  7. I/LoggingEventListener: Event connectEnd at 1731855030886 ConnectDuration is 671 ms
  8. I/LoggingEventListener: Event requestHeadersStart at 1731855030887
  9. I/LoggingEventListener: Event requestHeadersEnd at 1731855030889 requestHeadersDuration is 2 ms
  10. I/LoggingEventListener: Event responseHeadersStart at 1731855031406
  11. I/LoggingEventListener: Event responseHeadersEnd at 1731855031406 responseHeadersDuration is 0 ms
  12. I/LoggingEventListener: Event responseBodyStart at 1731855031414
  13. I/LoggingEventListener: Event responseBodyEnd at 1731855031415 responseHeadersDuration is 1 ms
  14. I/LoggingEventListener: Event callEnd at 1731855031415 responseHeadersDuration is 1208 ms

标签: 网络 Android okhttp

本文转载自: https://blog.csdn.net/qq_41688840/article/details/143840503
版权归原作者 柯南二号 所有, 如有侵权,请联系我们删除。

“Android okhttp 网络链接各阶段监控”的评论:

还没有评论