Skip to content

Instantly share code, notes, and snippets.

@cypressious
Forked from ashtom/HockeySender.java
Last active August 29, 2015 14:27
Show Gist options
  • Save cypressious/ce618458f42ac6459b14 to your computer and use it in GitHub Desktop.
Save cypressious/ce618458f42ac6459b14 to your computer and use it in GitHub Desktop.
Custom ReportSender for HockeyApp and ACRA with OkHTTP in Kotlin
package net.hockeyapp.android.demo
import com.squareup.okhttp.FormEncodingBuilder
import com.squareup.okhttp.OkHttpClient
import com.squareup.okhttp.Request
import org.acra.ACRA
import org.acra.ReportField
import org.acra.collector.CrashReportData
import org.acra.sender.ReportSender
import java.util.Date
import java.util.Locale
class HockeyappCrashSender : ReportSender {
override fun send(report: CrashReportData) {
val log = createCrashLog(report)
val url = "https://rink.hockeyapp.net/api/2/apps/${ACRA.getConfig().formKey()}/crashes"
try {
val bodyBuilder = FormEncodingBuilder() //
.add("raw", log) //
.add("userID", report.get(ReportField.INSTALLATION_ID) ?: "") //
.add("contact", report.get(ReportField.USER_EMAIL) ?: "") //
.add("description", report.get(ReportField.USER_COMMENT) ?: "")
val post = Request.Builder() //
.url(url) //
.post(bodyBuilder.build())
OkHttpClient().newCall(post.build()).execute()
} catch (e: Exception) {
e.printStackTrace()
}
}
private fun createCrashLog(report: CrashReportData) = StringBuilder {
append("Package: ").append(report.get(ReportField.PACKAGE_NAME)).append("\n")
append("Version Code: ").append(report.get(ReportField.APP_VERSION_CODE)).append("\n")
append("Version Name: ").append(report.get(ReportField.APP_VERSION_NAME)).append("\n")
append("Android: ").append(report.get(ReportField.ANDROID_VERSION)).append("\n")
append("Manufacturer: ").append(android.os.Build.MANUFACTURER).append("\n")
append("Model: ").append(report.get(ReportField.PHONE_MODEL)).append("\n")
append("Custom: ").append("\n").append(report.get(ReportField.CUSTOM_DATA))
append("Language: ").append(Locale.getDefault().getLanguage()).append("\n")
append("Date: ").append(Date()).append("\n")
append("\n")
append(report.get(ReportField.STACK_TRACE))
}.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment