修复:在子线程中调用Toast的异常情况处理

This commit is contained in:
pppscn 2022-06-09 16:02:17 +08:00
parent b0465eec54
commit 43f33e5fe2
2 changed files with 16 additions and 2 deletions

View File

@ -1,5 +1,6 @@
package com.idormy.sms.forwarder.utils.sender
import android.os.Looper
import android.text.TextUtils
import android.util.Log
import com.google.gson.Gson
@ -104,7 +105,9 @@ class TelegramUtils {
override fun onFailure(call: Call, e: IOException) {
SendUtils.updateLogs(logId, 0, e.message.toString())
e.printStackTrace()
//XToastUtils.error("发送失败:" + e.message)
Looper.prepare()
XToastUtils.error("发送失败:" + e.message)
Looper.loop()
}
@Throws(IOException::class)
@ -115,10 +118,14 @@ class TelegramUtils {
val resp = Gson().fromJson(responseStr, TelegramResult::class.java)
if (resp.ok == true) {
SendUtils.updateLogs(logId, 2, responseStr.toString())
Looper.prepare()
XToastUtils.success(ResUtils.getString(R.string.request_succeeded))
Looper.loop()
} else {
SendUtils.updateLogs(logId, 0, responseStr.toString())
Looper.prepare()
XToastUtils.error(ResUtils.getString(R.string.request_failed) + response)
Looper.loop()
}
}
})

View File

@ -1,6 +1,7 @@
package com.idormy.sms.forwarder.utils.sender
import android.annotation.SuppressLint
import android.os.Looper
import android.text.TextUtils
import android.util.Base64
import android.util.Log
@ -164,7 +165,10 @@ class WebhookUtils {
override fun onFailure(call: Call, e: IOException) {
SendUtils.updateLogs(logId, 0, e.message.toString())
//LogUtils.updateLog(logId, 0, e.message)
//解决在子线程中调用Toast的异常情况处理
Looper.prepare()
XToastUtils.error(ResUtils.getString(R.string.request_failed) + e.message)
Looper.loop()
}
@Throws(IOException::class)
@ -172,14 +176,17 @@ class WebhookUtils {
val responseStr = response.body().toString()
Log.d(TAG, "Response" + response.code() + "" + responseStr)
//返回http状态200即为成功
if (200 == response.code()) {
SendUtils.updateLogs(logId, 2, responseStr)
Looper.prepare()
XToastUtils.success(ResUtils.getString(R.string.request_succeeded))
Looper.loop()
} else {
SendUtils.updateLogs(logId, 0, responseStr)
Looper.prepare()
XToastUtils.error(ResUtils.getString(R.string.request_failed) + response)
Looper.loop()
}
}
})