我是android studio“kotlin
”的新用户,我尝试从url
数据json
,内容JSONObject
和JSONArray
获取信息,我的代码正常工作和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()
}
}
答案 0 :(得分:0)
以下是如何从此响应中获取可用性数组。
{&#34;身份证明&#34;:{ &#34; id&#34;:&#34; f7c2de9&#34;, &#34; row&#34;:4606622416, &#34; number&#34;:{ &#34;默认&#34;:&#34; QR402&#34;, &#34;替代&#34;:null }, &#34;呼号&#34;:&#34; QTR402&#34; },&#34;可用性&#34;:[ &#34; AGE&#34 ;, &#34; MSN&#34; ]}
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()
}
}
答案 1 :(得分:0)
尝试以下代码
val availabilityArray = json.getJSONArray("availability")
for (i in 0 until availabilityArray.length()){
val winspeed = availabilityArray.getString(i);
text.setText(winspeed)
}