【发布时间】:2021-08-17 09:10:51
【问题描述】:
我想通过改造获得网络连接状态。
我使用CoroutineExceptionHandler 显示Toast,但它会得到Can't toast on a thread that has not called Looper.prepare()。
如果我不使用CoroutineExceptionHandler,当网络未连接时它会崩溃并得到FATAL EXCEPTION: DefaultDispatcher-worker-1。
如何获得网络未连接和连接超时状态?
这是我的代码,谢谢!
object APIClientManager {
var tag: String = "APIClientManager"
fun postMethod(_jsonObject: JSONObject, _parameters: JSONObject) {
getResult(_jsonObject, _parameters)
}
fun getResult(_jsonObject: JSONObject, _parameters: JSONObject) {
// set okHttpClient.
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor { chain ->
val original = chain.request()
val requestBuilder = original.newBuilder()
requestBuilder
.header("Content-Type", "application/json; charset=UTF-8")
val request = requestBuilder.build()
chain.proceed(request)
}
httpClient.connectTimeout(30, TimeUnit.SECONDS)
httpClient.readTimeout(30, TimeUnit.SECONDS)
val okHttpClient = httpClient.build()
// Create Retrofit
val retrofit = Retrofit.Builder()
.baseUrl(GbApiSite)
.client(okHttpClient)
.build()
// Create Service
val service = retrofit.create(APIService::class.java)
// Convert JSONObject to String
val jsonObjectString = _jsonObject.toString()
// Create RequestBody
val requestBody = jsonObjectString.toRequestBody("application/json".toMediaTypeOrNull())
val exceptionHandler = CoroutineExceptionHandler{_ , throwable->
throwable.printStackTrace()
// I want to show toast here but will get "Can't toast on a thread that has not called Looper.prepare()".
}
CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
// Do the POST request and get response
val response = APIServiceFun(_parameters, service, requestBody).chooseFun()
withContext(Dispatchers.Main) {
if (response != null) {
if (response.isSuccessful) {
val gson = GsonBuilder().serializeNulls().create()
val json = gson.toJson(
JsonParser.parseString(
response.body()?.
string()
)
)
var returnJSONObj = JSONObject(json)
if(returnJSONObj.has("return_code")){
var returnCode = returnJSONObj.getString("return_code")
if(returnCode.compareTo("0") == 0){
if(returnJSONObj.has("return_msg"))
APIServiceResult(returnJSONObj.getJSONObject("return_msg"), _parameters).chooseActivity()
}
}
} else
Log.e("retrofit error ", response.code().toString())
} else {
Log.e(tag, "response is null")
}
}
}
}
}
【问题讨论】: