Android studio 在运行doInBackground之前,如何调用函数?

Android studio 在运行doInBackground之前,如何调用函数?,android-studio,kotlin,Android Studio,Kotlin,因此,我正在从doInBackground调用一个api,但是URL需要我拥有函数的城市名称。如果我从onPreExecute()调用函数,它实际上不会运行,直到doInBackground之后,应用程序崩溃,因为它有一个不正确的链接。但我也不能在doInBackground中调用它,因为它告诉我需要执行Looper.prepare(),但却违背了这一点,因为该函数是在api链接之后调用的。那么,我如何调用getNewLocation()函数,在运行doInBackground之前在api链接中

因此,我正在从
doInBackground
调用一个api,但是URL需要我拥有函数的城市名称。如果我从onPreExecute()调用函数,它实际上不会运行,直到
doInBackground
之后,应用程序崩溃,因为它有一个不正确的链接。但我也不能在doInBackground中调用它,因为它告诉我需要执行
Looper.prepare()
,但却违背了这一点,因为该函数是在api链接之后调用的。那么,我如何调用getNewLocation()函数,在运行
doInBackground
之前在api链接中更改城市名称,以便获得正确的数据呢

inner class weatherTask(): AsyncTask<String, Void, String>(){

    override fun onPreExecute() {
        super.onPreExecute()
        findViewById<ProgressBar>(R.id.progress).visibility = View.VISIBLE
        findViewById<RelativeLayout>(R.id.mainTing).visibility = View.GONE
        //getNewLocation() this is supposed to change city name
        Log.d("preCity", city)


    }


    override fun doInBackground(vararg p0: String?): String? {
        var response: String? = ""
        response = URL("https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&APPID=$API").readText(Charsets.UTF_8)
        //Log.d("response", response!!)
        return response

    }

    override fun onPostExecute(result: String?) {
        super.onPostExecute(result)
        try{
            val jsonObj = JSONObject(result)
            Log.d("json", jsonObj.toString())
            val main = jsonObj.getJSONObject("main")
            val sys = jsonObj.getJSONObject("sys")
            val weather = jsonObj.getJSONArray("weather").getJSONObject(0)
            val pressure = main.getString("pressure")
            val humidity = main.getString("humidity")
            val temp = main.getString("temp") + "°C"
            val weatherDescription = weather.getString("description")
            val address = jsonObj.getString("name") + ", " + sys.getString("country")

            updateTime()

            findViewById<TextView>(R.id.cityLocation).text = address
            findViewById<TextView>(R.id.status).text = weatherDescription.capitalize()
            findViewById<TextView>(R.id.temp).text = temp
            findViewById<TextView>(R.id.pressure).text = pressure + " mBar"
            findViewById<TextView>(R.id.humidity).text = humidity + "%"

            findViewById<RelativeLayout>(R.id.mainTing).visibility = View.VISIBLE
            findViewById<ProgressBar>(R.id.progress).visibility = View.GONE


        }
        catch(e: Exception){
            Log.d("error", "Somethings wrong...")
        }


    }
}
内部类weatherTask():AsyncTask(){
覆盖乐趣onPreExecute(){
super.onPreExecute()
findViewById(R.id.progress).visibility=View.VISIBLE
findViewById(R.id.mainTing).visibility=View.GONE
//getNewLocation()这应该更改城市名称
对数d(“精度”,城市)
}
覆盖有趣的背景(vararg p0:String?:String{
变量响应:字符串?=“”
响应=URL(“https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&APPID=$API”).readText(Charsets.UTF_8)
//Log.d(“响应”,响应!!)
返回响应
}
重写onPostExecute(结果:字符串?){
super.onPostExecute(结果)
试一试{
val jsonObj=JSONObject(结果)
Log.d(“json”,jsonObj.toString())
val main=jsonObj.getJSONObject(“main”)
val sys=jsonObj.getJSONObject(“sys”)
val weather=jsonObj.getJSONArray(“天气”).getJSONObject(0)
val压力=main.getString(“压力”)
val湿度=main.getString(“湿度”)
val temp=main.getString(“温度”)+“摄氏度”
val weatherDescription=weather.getString(“说明”)
val address=jsonObj.getString(“名称”)+,“+sys.getString(“国家”)
更新时间()
findViewById(R.id.cityLocation)。text=地址
findViewById(R.id.status).text=weatherDescription.capitalize()
findViewById(R.id.temp).text=temp
findViewById(R.id.pressure)。文本=压力+“毫巴”
findViewById(R.id.湿度)。文本=湿度+“%”
findViewById(R.id.mainTing).visibility=View.VISIBLE
findViewById(R.id.progress).visibility=View.GONE
}
捕获(e:例外){
Log.d(“错误”,“某些事情出错…”)
}
}
}

doInBackground
onPreExecute()返回后才会调用。
getNewLocation()
是否异步运行?您可能需要等待它完成。似乎代码也应该在
doInBackground
中,这样您就可以同步获取位置。顺便说一下,AsyncTask被贬低,你应该考虑使用协同程序,这是更简单的工作。