1

am new in android studio " kotlin " , am try to get information from url data json , content JSONObject and JSONArray , my code Working fine with JSONObject , but i still have problem withe JSONArray .

this is my code for any one can help me to how can get JSONArray from my url

  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

9
  • where you are using JSONArray object? Commented Nov 10, 2017 at 9:41
  • i delete JSONArray object because i get error and problem when run app , so i want someone to edit my code and give my short answer pls . Commented Nov 10, 2017 at 9:46
  • show your json output Commented Nov 10, 2017 at 9:52
  • jsoneditoronline.org/?id=57eef4e4f8227fd4544d9decf8b37b3d Commented Nov 10, 2017 at 9:53
  • and what error it was giving? Commented Nov 10, 2017 at 9:53

2 Answers 2

2

here is how to fetch availability array from this response.

{ "identification": { "id": "f7c2de9", "row": 4606622416, "number": { "default": "QR402", "alternative": null }, "callsign": "QTR402" }, "availability": [ "AGE", "MSN" ] }

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

your code should be like

 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()
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

try below code

val availabilityArray = json.getJSONArray("availability")

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.