如何从 kotlin (android studio) 中的 asynctask + try & catch 块中获取变量?

How to get variables out from a asynctask + try & catch block in kotlin (android studio)?

在下面的两个函数中,我得到了 referrerUrl 和 addId。我希望它们都在 onCreate 中获取,但不知道如何获取,因为它在 try & catch 块中,而且 getGaid() 函数在没有 AsyncTask 的情况下不是 运行。

    fun getreferrUrl() {

     //to install referrer client
   val referrerClient = InstallReferrerClient.newBuilder(this).build()
    referrerClient.startConnection(object : InstallReferrerStateListener {

        override fun onInstallReferrerSetupFinished(responseCode: Int) {
            when (responseCode) {
                InstallReferrerResponse.OK -> {
                    // Connection established.
                    try {
                        val response: ReferrerDetails = referrerClient.installReferrer
                        val referrerUrl = response.installReferrer

                                // here we need referrerUrl out from this fuction

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

//

fun getGaid() {
    AsyncTask.execute {

        try {
            val adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this)
            val myId: String = if (adInfo != null) adInfo.id else null.toString()

                       //here we need myId out from this fuction

        } catch (e: java.lang.Exception) {...}
     }  
  }

在 onCreate 中我们需要这两个字符串。

      // In onCreate

                 val url = "http://instbng.com?device_id=$device_id&                             
                            &kd_id=$kd_id&ref=$referrerUrl&gaid=$myId"

                   loadUrl(url)

没有协程,您可以将结果放在属性中,并创建一个使用这两个属性的函数并从两个回调中调用它。我将您的 get... 函数重命名为 fetch...,因为它们是异步的。函数名称中的单词 get 表示它们是同步的。

private var referrerUrl: String? = null
private var myId: String? = null

override fun onCreate(bundle: SavedInstanceState?) {
    super.onCreate(bundle)

    //...

    fetchReferrerUrl()
    fetchGaId()
}

// proceeds with workflow if referrerUrl and myId are both available
private fun proceedIfReady() {
    val referrer = referrerUrl ?: return
    val id = myId ?: return
    val url = "http://instbng.com?device_id=$device_id&kd_id=$kd_id&ref=$referrer&gaid=$idd"
    loadUrl(url)
}

fun fetchReferrerUrl() {
    val referrerClient = InstallReferrerClient.newBuilder(this).build()
    referrerClient.startConnection(object : InstallReferrerStateListener {

        override fun onInstallReferrerSetupFinished(responseCode: Int) {
            when (responseCode) {
                InstallReferrerResponse.OK -> {
                    // Connection established.
                    try {
                        val response: ReferrerDetails = referrerClient.installReferrer
                        referrerUrl = response.installReferrer

                        proceedIfReady()

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

        //... handle closed connection callback

    }
}

private fun fetchGaId() {
    AsyncTask.execute {
        try {
            val adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this)
            runOnUiThread { // in a Fragment use view?.post
                myId = if (adInfo != null) adInfo.id else null.toString()
                proceedIfReady()
            }
        } catch (e: java.lang.Exception) {...}
    }
}