我有一个在我的 Activity 之一中生成的 token ,并且我希望在执行 sendHttpRequests 时能够从我的 HttpSender 检索它。
问题是我总是以空 token 结束...发生崩溃时会触发 sendHttpRequests()
方法。
(我是 Acra 的初学者)
@AcraCore(reportSenderFactoryClasses = [SenderFactory::class])
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
ACRA.init(this)
}
}
class SenderFactory : ReportSenderFactory {
override fun create(context: Context, config: CoreConfiguration): ReportSender = AcraSender(config)
}
class AcraSender(
config: CoreConfiguration,
method: Method = POST,
type: StringFormat? = null
) : HttpSender(config, method, type) {
override fun sendHttpRequests(
configuration: CoreConfiguration,
context: Context,
method: Method,
contentType: String,
login: String?,
password: String?,
connectionTimeOut: Int,
socketTimeOut: Int,
headers: Map<String, String>?,
content: String,
url: URL,
attachments: MutableList<Uri>
) {
//I want to retrieve my TOKEN here
}
}
最佳答案
根据您的要求,有几种解决方案:
- 如果您希望 token 成为报告的一部分,请使用
ErrorReporter.putCustomData
,并在ReportSender.send
中使用 检索它
((JSONObject)report.get(ReportField.CUSTOM_DATA.name())).optString("YOUR_KEY")
- 如果您的 token 是在没有用户交互的情况下生成的,只需在
ReportSender
中生成即可。 - 否则,请使用任何持久存储,例如
SharedPreferences
或纯文件。
背景信息:
Acra 在单独的进程中运行 ReportSender
(因为崩溃后的死亡进程不适合进行网络操作)。
这意味着大多数内存存储不能用于在应用和 ReportSender
之间共享数据。
ErrorReporter.putCustomData
可以正常工作,因为报告会临时保存到磁盘。
关于java - Acra:如何从继承自 ReportSenderFactory 的自定义类中检索 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59072462/