/network-logger-android

A simple HTTP/HTTPS network request/response logger and inspector

Primary LanguageKotlinMIT LicenseMIT

networklogger

Konrad Biernacki (kgbier@gmail.com)

A lightweight utility for capturing network traffic in Android apps.

Features

  • Record and inspect HTTP/HTTPS requests and response
  • Identify failed or unexpected responses
  • Share and export individual requests via the clipboard
    • Additionally export as a cURL command
  • Comes with batteries-included for:
    • Ktor
    • OkHttp

Example

A sample Android app is included under ./sample.

Use

The latest release is available on Maven Central.

dependencies {
  implementation 'dev.kgbier.util:networklogger:1.1'
}

Snapshot builds are available.

OkHttp

  1. Depend on the okhttp flavour of this library:
dependencies {
  implementation 'dev.kgbier.util:networklogger-okhttp:1.1'
}
  1. Add the NetworkLoggerOkHttpInterceptor to your OkHttp client:
OkHttpClient.Builder()
    .addInterceptor(NetworkLoggerOkHttpInterceptor(applicationContext))
    .build()
  1. View logs by starting the included activity:
startActivity(makeNetworkLoggerActivityIntent(context))

Ktor

  1. Depend on the ktor flavour of this library:
dependencies {
  implementation 'dev.kgbier.util:networklogger-ktor:1.1'
}
  1. Add the NetworkLoggerKtorPluginInstaller to your Ktor client:
HttpClient(CIO) {
  install(NetworkLoggerKtorPluginInstaller(applicationContext))
}
  1. View logs by starting the included activity:
startActivity(makeNetworkLoggerActivityIntent(context))

DIY

  1. Get a handle on the logging repository:
val logginRepo = NetworkLoggerRepository(applicationContext)
  1. Log a request when it is about to be sent:
loggingRepo.logRequest(
  transactionId = requestTransactionId,
  url = request.url.toString(),
  method = request.method,
  headers = request.headers.toList(),
  body = bodyBuffer.readString(Charsets.UTF_8),
  timestamp = System.currentTimeMillis(),
)
  1. Log a response when it is received:
loggingRepo.logResponse(
  transactionId = requestTransactionId,
  statusCode = response.code,
  headers = response.headers.toList(),
  body = bodyBuffer.readString(Charsets.UTF_8),
  timestamp = System.currentTimeMillis(),
)
  1. View logs by starting the included activity:
startActivity(makeNetworkLoggerActivityIntent(context))