Kotlin:处理超时连接okhttp

Kotlin:处理超时连接okhttp,kotlin,timeout,okhttp,Kotlin,Timeout,Okhttp,我是科特林的初学者。我尝试使用OkHttp构建应用程序,以便在单击按钮时向rapidapi API下载文件发出web请求。我已经添加了设置连接超时,但我想处理连接超时连接,当连接超时时,我想向用户显示一个对话框以重试或取消 private fun StartDwonload() { val url = editText.text.toString() val client = OkHttpClient.Builder() .conne

我是科特林的初学者。我尝试使用OkHttp构建应用程序,以便在单击按钮时向rapidapi API下载文件发出web请求。我已经添加了设置连接超时,但我想处理连接超时连接,当连接超时时,我想向用户显示一个对话框以重试或取消

   private fun StartDwonload() {


    val url = editText.text.toString()


        val client = OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .build()

        val requests = Request.Builder()
            .url("xxxxxxx?igurl="+url)
            .get()
            .addHeader("x-rapidapi-host", "xxxxx")
            .addHeader("x-rapidapi-key", "xxxx")
            .build()
        client.newCall(requests).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {}

            override fun onResponse(call: Call, response: Response){

                val responseData = response.body()?.string()
                runOnUiThread{
                    try {
                        var json = JSONObject(responseData)
                        println("Request Successful!!")
                        println(json)
                        val responseObject = json.get("downloadurl").toString()

                        val request = DownloadManager.Request(Uri.parse((responseObject)))
                        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
                        request.setTitle("Download")
                        request.setDescription("Dwonloading ...")
                        request.allowScanningByMediaScanner()
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
                        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,"${System.currentTimeMillis()}.mp4")
                        print(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
                        val manager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
                        manager.enqueue((request))

                        progressDialog.dismiss()

                    } catch (e: JSONException) {
                        e.printStackTrace()
                    }
                }

            }
        })



    }
有什么想法吗?

关于OnFailure()方法,您可以检查您的错误是否为SocketTimeoutException

override fun onFailure(call: Call, e: IOException) {
if(e is SocketTimeoutException){
//do your work
}
}

我有错误
Function invocation'error(…)'预期
我通过更改错误类型更新了一点答案,请立即尝试并让我知道它是如何运行的。如果我设置了
Throwable
我有错误
对象不是抽象的,并且没有实现抽象成员公共抽象fun onFailure(call:call,e:IOException!):在okhttp3.Callback中定义的单位
在onFailure()方法本身中使用AlertDialog时会发生什么。。因为它是例外,你有没有收到任何错误?如果没有显示任何内容,则表示根本不会触发onFailure()方法。是这样吗?