HttpCall Event
Android
A flag that determines whether HTTP request and response logging is enabled. When set to true, HTTP requests and responses are logged. If set to false, logging is disabled.
Config
val config =
PaylisherAndroidConfig(apiKey = PAYLISHER_API_KEY, host = PAYLISHER_HOST).apply {
httpLogger=true // for SDK http traffic
...
}
PaylisherAndroid.setup(this, config)
Capturing App-Side HTTP Traffic
Initialization
To log HTTP traffic, you must add LoggingInterceptor() to your OkHttpClient.
// Initialize OkHttpClient with LoggingInterceptor
val okHttpClient = com.paylisher.internal.interceptors.createOkHttpClient();
// or
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(com.paylisher.internal.interceptors.LoggingInterceptor())
.build()
Make sure the LoggingInterceptor is correctly implemented and included in your interceptor chain.
Example
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
/* Custom http error tests for httpCall event */
CoroutineScope(Dispatchers.IO).launch {
makeNetworkCall("https://httpbin.org/status/404")
makeNetworkCall("https://httpbin.org/status/500")
}
...
}
private fun makeNetworkCall(url: String) {
try {
// Initialize OkHttpClient with LoggingInterceptor
// val okHttpClient = com.paylisher.internal.interceptors.createOkHttpClient();
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(com.paylisher.internal.interceptors.LoggingInterceptor())
.build()
// Define the request body with a JSON payload
val requestBody = object : RequestBody() {
override fun contentType(): MediaType =
"application/json; charset=utf-8".toMediaType()
override fun writeTo(sink: BufferedSink) {
val jsonPayload = """
{
"key": "value",
"test": "LoggingInterceptor"
}
""".trimIndent()
sink.writeUtf8(jsonPayload)
}
}
// Build the HTTP request
val request = Request.Builder()
.url(url) // Simulate different status codes
.header("User-Agent", "LoggingInterceptorTest")
.post(requestBody)
.build()
// Execute the request
val response = okHttpClient.newCall(request).execute()
// Handle response status
/* val statusCode = response.code
val responseBody = response.body?.string()
// Log different cases
runOnUiThread {
when (statusCode) {
200 -> println("Success: $responseBody")
404 -> println("Error 404: Not Found - $responseBody")
500 -> println("Server Error 500: $responseBody")
else -> println("Unexpected Status $statusCode: $responseBody")
}
}
*/
} catch (e: Exception) {
e.printStackTrace()
}
}