免责声明:我对Kotlin经验不足。我有另一个开发人员为Wear OS表面设计了基础,并决定自己进一步开发。
这是一个自定义表盘,每分钟调用一次API端点(不进行身份验证或任何操作),并相应地更新表的值。它在大多数时间都有效。但是它时不时地崩溃。而我要做的就是“再次使用它”是将其重新设置为活动表盘。我不确定下面写的错误是否会导致表面崩溃。
我实现了Crashlytics,可以看到我遇到了这个错误:
Fatal Exception: java.lang.RuntimeException
An error occurred while executing doInBackground()
跟踪
Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
原因
Caused by javax.net.ssl.SSLException
Write error: ssl=0xa5bcad80: I/O error during system call, Broken pipe
APPLICATIONID.util.HandleRequest.doInBackground
跟踪
Caused by javax.net.ssl.SSLException: Write error: ssl=0xa5bcad80: I/O error during system call, Broken pipe
at com.android.org.conscrypt.NativeCrypto.SSL_write(NativeCrypto.java)
at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:807)
at com.android.okhttp.okio.Okio$1.write(Okio.java:76)
...
...
...
相关代码
override fun doInBackground(vararg params: Unit?): String?
{
try{
val url = URL("https://example.org/endpoint")
val httpClient = url.openConnection() as HttpURLConnection
if (httpClient.responseCode == HttpURLConnection.HTTP_OK)
{
try
{
val stream = BufferedInputStream(httpClient.inputStream)
val data: String = readStream(inputStream = stream)
return data
} catch (e: Exception) {
e.printStackTrace()
} finally {
httpClient.disconnect()
}
} else {
println("ERROR ${httpClient.responseCode}")
}
}catch(e:java.net.ConnectException){
}
return null
}
我可以阅读here,以便在进行呼叫之前可能需要检索SSL证书(或信任它)。那有点道理,但是-如果那是导致崩溃的原因,那么我不明白为什么它在大多数时间都起作用?