如何在onPostExecute Kotlin中使用表达式

时间:2019-03-30 11:53:35

标签: android kotlin

我是Kotlin的初学者,我想在onPostExecute上使用if表达式检查类名handleJson是否添加null来执行警报对话框,如果不为空,则执行handleJson如果为null,则显示警报对话框。连接性“”

 inner class Arr : AsyncTask<String, String, String>() {

        val progressDialog = AlertDialog.Builder(this@MainActivity)
        val dialogView = layoutInflater.inflate(R.layout.progress_dialog, null)
        val message = dialogView.findViewById<TextView>(R.id.message_id)
        val dialog = progressDialog.create()

        override fun onPreExecute() {
            super.onPreExecute()

            dialog.setMessage("يرجى الانتظار")
            dialog.setCancelable(false)
            dialog.show()


        }

        //        for build connection
        override fun doInBackground(vararg url: String?): String {

            var text: String
            val connection = URL(url[0]).openConnection() as HttpURLConnection
            connection.connectTimeout = 300

            try {
                connection.connectTimeout = 5000 // We all timeout here
                connection.connect()
                text = connection.inputStream.use { it.reader().use{reader -> reader.readText()} }
            } finally{
                    connection.disconnect()

            }
            return text
        }

        override fun onPostExecute(result: String?) {

            handleJson(result)
            dialog.dismiss();

            if (handleJson(result)){

            }

        }

        override fun onProgressUpdate(vararg text: String?) {


        }

        @SuppressLint("WrongViewCast")

        private fun handleJson(jsonString: String?) {

            val jsonObj = JSONObject(jsonString)
            val result = jsonObj.getJSONObject("result")
            val response = result.getJSONObject("response")


//        val data = arrivals.getJSONObject("data")
            val jsonArray = JSONArray(arrivals.get("data").toString())

            val list = ArrayList<FlightShdu>()
            var x = 0
            while (x < jsonArray.length()) {

                val jsonObject = jsonArray.getJSONObject(x)



                list.add(
                    FlightShdu(
                        jsonObject.getJSONObject("flight").getJSONObject("identification").getJSONObject("number").getString(
                            "default"
                        ),
                        jsonObject.getJSONObject("flight").getJSONObject("airline").getString("short"),
                        jsonObject.getJSONObject("flight").getJSONObject("status").getJSONObject("generic").getJSONObject(
                            "status"
                        ).getString("text"),
                        jsonObject.getJSONObject("flight").getJSONObject("airline").getJSONObject("code").getString("icao"),
                        jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("scheduled").getString("arrival"),
                        jsonObject.getJSONObject("flight").getJSONObject("airport").getJSONObject("origin").getJSONObject("position").
                            getJSONObject("region").getString("city"),
                        jsonObject.getJSONObject("flight").getJSONObject("aircraft").getJSONObject("model").getString("code"),
//                    for more information
                        jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("real").getString("departure"),
                        jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("estimated").getString("arrival"),
//                    jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("estimated").getString("arrival"),
                        jsonObject.getJSONObject("flight").getJSONObject("aircraft").getString("registration"),
                        jsonObject.getJSONObject("flight").getJSONObject("status").getJSONObject("generic").getJSONObject(
                            "status"
                        ).getString("diverted"),
                        arrivals.getString("timestamp"),
                        jsonObject.getJSONObject("flight").getJSONObject("status").getString("icon")


                    )
                )


                x++
            }
            list.forEach(::println)

            var adapter = ListAdapteArr(this@MainActivity, list)
            flight_arrivel_list.adapter = adapter



        }

    }

我遇到了错误

Type mismatch.
Required: Boolean Found: String?

2 个答案:

答案 0 :(得分:0)

我看不到您正在使用if的确切位置,但是看到Type mismatch的错误似乎是您在if (condition?)中传递了一些可为空的字符串。

请确保您在if语句中的检查条件是boolean值而不是nullable String

答案 1 :(得分:0)

handleJson方法似乎返回一个String,一个if条件必须计算一个布尔表达式,等于其他值,大于等于,等等。

与Java不同,在Kotlin中与Java中没有捷径。因此,如果handleJson()方法的结果是字符串,则不管它是否存在,都不会被评估,这是语法错误。

尝试handleJson != null