Installation
Add this code to your Android project to initialize Hardal and start sending events:
import org.json.JSONObject
import java.net.URL
import java.time.Instant
import javax.net.ssl.HttpsURLConnection
class HardalClient {
private val endpoint = "https://<your-hardal-signal-id>-signal.usehardal.com"
fun sendEvent(eventName: String, properties: Map<String, Any>) {
Thread {
try {
val url = URL("https://<your-hardal-signal-id>-signal.usehardal.com/push/hardal")
val connection = url.openConnection() as HttpsURLConnection
connection.requestMethod = "POST"
connection.setRequestProperty("Content-Type", "application/json")
connection.doOutput = true
val eventData = JSONObject().apply {
put("event", eventName)
put("properties", JSONObject(properties))
put("timestamp", Instant.now().toString())
}
connection.outputStream.use { os ->
os.write(eventData.toString().toByteArray())
}
val responseCode = connection.responseCode
if (responseCode != HttpsURLConnection.HTTP_OK) {
println("Failed to send event: $responseCode")
}
} catch (e: Exception) {
println("Error sending event: ${e.message}")
}
}.start()
}
}
Configuration Options
For Android integration, you can customize the following:
- Custom event properties
- Event batching
- Retry logic for failed requests
- Background thread management
Example Usage
Troubleshooting
Validation
To validate your Android integration:
- Monitor Logcat in Android Studio
- Use the Network Profiler to inspect requests
- Check for successful HTTP responses (200 status code)
- Verify events appear in your Hardal dashboard
For development, you can enable detailed logging by setting the log level to VERBOSE in your application’s debug configuration.