【问题标题】:how get Array json in kotlin from url如何从 url 获取 kotlin 中的 Array json
【发布时间】:2017-11-10 09:25:37
【问题描述】:

我是 android studio "kotlin" 的新手,我尝试从 url 数据 json 、内容 JSONObjectJSONArray 获取信息,我的代码与 JSONObject 一起工作正常,但我仍然有JSONArray 的问题。

这是我的代码,任何人都可以帮助我了解如何从我的网址获取 JSONArray

  fun buclick(view:View){
        val flightSearch=editText2.text.toString()
        val url="xxxxxxx-flight=$flightSearch"
    MyAsyncTask().execute(url)
    }


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

        override fun onPreExecute() {
            super.onPreExecute()
        }

        override fun doInBackground(vararg p0: String?): String {
            //تمرير بيانات هنا
            try {
                val url=URL(p0[0])
                val urlConnect = url.openConnection() as HttpURLConnection
                urlConnect.connectTimeout=500

                val dataJsonAsString=convertStreanToString(urlConnect.inputStream)
                publishProgress(dataJsonAsString)
            }catch (ex:Exception){

            }

            return  ""
        }


        override fun onProgressUpdate(vararg values:String?) {

            val json=JSONObject(values[0])
            val query= json.getJSONObject("identification")
            val sunrise=query.getString("id")
            flighttext.text="sunrise time" + sunrise

        }
        override fun onPostExecute(result: String?) {
            super.onPostExecute(result)
        }

        fun convertStreanToString(inputStream:InputStream):String{
            val  bufferReader = BufferedReader(InputStreamReader(inputStream))
            var line:String
            var allstring:String=""

            try {
                do {
                    line=bufferReader.readLine()
                    if(line!=null)
                        allstring+=line
                }while (line!=null)
                bufferReader.close()
            }catch (ex:Exception){}


            return allstring
        }

    }

//مالنا شغل ببيها
    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

my data json url

【问题讨论】:

  • 你在哪里使用 JSONArray 对象?
  • 我删除 JSONArray 对象,因为我在运行应用程序时遇到错误和问题,所以我希望有人编辑我的代码并给出我的简短回答。
  • 显示你的 json 输出
  • 它给出了什么错误?

标签: android json kotlin


【解决方案1】:

这里是如何从这个响应中获取可用性数组。

{ "识别": { “id”:“f7c2de9”, “行”:4606622416, “数字”: { “默认”:“QR402”, “替代”:空 }, “呼号”:“QTR402”},“可用性”:[ “年龄”, “MSN”] }

    val json=JSONObject(values[0])
    val availabilityArray = json.getJSONArray("availability")

你的代码应该是这样的

 fun buclick(view:View){
        val flightSearch=editText2.text.toString()
        val url="xxxxxxx-flight=$flightSearch"
    MyAsyncTask().execute(url)
    }


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

        override fun onPreExecute() {
            super.onPreExecute()
        }

        override fun doInBackground(vararg p0: String?): String {
            //تمرير بيانات هنا
            try {
                val url=URL(p0[0])
                val urlConnect = url.openConnection() as HttpURLConnection
                urlConnect.connectTimeout=500

                val dataJsonAsString=convertStreanToString(urlConnect.inputStream)
                publishProgress(dataJsonAsString)
                val json=JSONObject(dataJsonAsString);
                ///here is the required array
                val availabilityArray = json.getJSONArray("availability")
                val query= json.getJSONObject("identification")
                val sunrise=query.getString("id")
                flighttext.text="sunrise time" + sunrise
            }catch (ex:Exception){

            }

            return  ""
        }


        override fun onProgressUpdate(vararg values:String?) {

//update progressbar


        }
        override fun onPostExecute(result: String?) {
            super.onPostExecute(result)
        }

        fun convertStreanToString(inputStream:InputStream):String{
            val  bufferReader = BufferedReader(InputStreamReader(inputStream))
            var line:String
            var allstring:String=""

            try {
                do {
                    line=bufferReader.readLine()
                    if(line!=null)
                        allstring+=line
                }while (line!=null)
                bufferReader.close()
            }catch (ex:Exception){}


            return allstring
        }

    }

//مالنا شغل ببيها
    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

【讨论】:

    【解决方案2】:

    试试下面的代码

    val availabilityArray = json.getJSONArray("availability")
    
            for (i in 0 until availabilityArray.length()){
                val winspeed = availabilityArray.getString(i);
                text.setText(winspeed)
            }
    

    【讨论】:

      猜你喜欢
      • 2021-03-21
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      相关资源
      最近更新 更多