Android SDK ⁠-⁠ Intercepting the network

OpenReplay exports NetworkListener that accepts both, tasks and requests, here’s how you do it:

import com.openreplay.tracker.OpenReplay

fun makeSampleRequest() {
    Thread {
        try {
            val url = URL("https://jsonplaceholder.typicode.com/posts/1")
            val connection = url.openConnection() as HttpURLConnection
            connection.requestMethod = "GET"

            // Optionally set request headers
            connection.setRequestProperty("Content-Type", "application/json")

            // Initialize the network listener for this connection
            val networkListener = NetworkListener(connection)

            val reader = BufferedReader(InputStreamReader(connection.inputStream))
            val response = StringBuilder()
            var line: String?
            while (reader.readLine().also { line = it } != null) {
                response.append(line)
            }
            reader.close()

            // Using the network listener to log the finish event
            networkListener.finish(connection, response.toString().toByteArray())
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }.start()
}

Two keys are exported to automatically trim the request/response data: ignoredKeys for body and ignoredHeaders for headers. Both, request and response will be sanitized, but body will only be affected if its a valid JSON.

Simply assign a list of strings that you would like to sanitize:

let networkListener = NetworkListener()
networkListener.ignoredHeaders = ["mySecretToken"]
networkListener.ignoredKeys = ["password"]

It is also possible to change the response data before passing it to the listener:

// ... request

var sanitizedData
if let data = data {
  do {
    let json = try JSONSerialization.jsonObject(with: data, options: [])
    sanitizedData = customSanitizeFunction(data)
    print(json)
  } catch {
    print("Error deserializing JSON: \(error)")
  }
}

networkListener.finish(response: response, data: sanitizedData)