2017-08-17 111 views
2

我無法請求多個連續的url http請求。我的應用程序沒有崩潰。在此代碼中使用loadUrls()函數後,它會連續執行6個url請求並將它們存儲在數組中,但我只能看到請求的第一個字符串響應。Asynctask Android中的多個連續URL請求

fun loadUrls(){ 
val locationInput = 「AsiaWorldExpo」 
val locationCoordinatesLong = 113.947 
val locationCoordinatesLat = 22.2913 
val myAPIKey = "REMOVED" 
val transportMode = arrayListOf("&mode=driving" ,"&mode=walking", "&mode=bicycling", 
     "&mode=transit&transit_mode=bus", "mode=transit&transit_mode=subway", "mode=transit&transit_mode=train") 

var requestURLArray = ArrayList<String>() 
for (i in 0 until transportMode.size) { 

    requestURLArray.add("https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins= " + 
      "$locationCoordinatesLat,$locationCoordinatesLong &destinations=$locationInput ${transportMode[i]} &key= $myAPIKey") 
} 
MyAsyncTask().execute(requestURLArray[0],requestURLArray[1],requestURLArray[2] 
     ,requestURLArray[3], requestURLArray[4], requestURLArray[5]) 
} 

override fun doInBackground(vararg p0: String?): String { 
     try { 
      val inString = ArrayList<String>() 
      for (i in 0 until p0.size) { 
       val url = URL(p0[i]) 

       val urlConnect = url.openConnection() as HttpURLConnection 
       urlConnect.connectTimeout = 7000 
       inString.add(ConvertStreamToString(urlConnect.inputStream)) 
       publishProgress(inString[i]) 
       Log.d("APP_TEST", "" + p0[i]) //Added for debugging 
       try { 
        Thread.sleep(500) 
       } catch (e: InterruptedException) {} 
      } 
     }catch (r:Exception){} 

     return " " 
    } 

override fun onProgressUpdate(vararg values: String?) { 
    try{ 
     val jsonObject = ArrayList<String>() 
     for (i in 0 until values.size){ 

      jsonObject.add(values[i]!!) 
      Log.d("Testing", "" + values[i]) //Added for debugging 

     } 
     idTextView2.text = jsonObject[1] 

編輯:上面的代碼行不打印出的文字查看來自的JSONObject到UI [1]到的JSONObject [5]。只有jsonObject [0]有效。

}catch (e:Exception){} 
} 
override fun onPostExecute(result: String?) { 
} 
} 
    fun ConvertStreamToString(inputStream: InputStream): String{ 
    val bufferReader = BufferedReader(InputStreamReader(inputStream)) 
    var line ="" 
    var allString="" 
    try{ 
     do{ 
      line=bufferReader.readLine() 
      if (line!=null){ 
       allString+=line 
      } 
     } while (line!=null) 
     inputStream.close() 
    } catch (e:Exception){} 
    return allString 
} 

這裏是更新的logcat:

9月8日至18日:51:21.697 2660-3883/com.google.android.gms W/DriveInitializer:背景初始化線程結束

08-18 09:52:00.405 1369-1409 /? W/audio_hw_generic:不提供足夠的數據來HAL,預期位置772788,只寫772560

9月8日至18日:52:00.433 3297-3320/com.companyname.emitless2 d/EGL_emulation:eglMakeCurrent:0x7ebacc5cc6e0:版本2 0 (TINFO 0x7ebadbdb9d00)

08-18 09:52:00.441 3297-3320/com.companyname.emitless2 d/EGL_emulation:eglMakeCurrent:0x7ebacc5cc6e0:第二版0(TINFO 0x7ebadbdb9d00)

08-18 9點52分:00.590 3297-3894/com.companyname.emitless2 D/APP_TEST:https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins= 22.29129833333333,113.94699999999999 & destinations = AsiaWorldExpo & mode = driving & key =「REMOVED」

08-18 09:52:00.590 3297-3297/com.companyname.emitless2 D /測試:{「destination_addresses」:[「香港赤臘角長榮路」] ,「origin_addresses」:[「香港東涌宜東道」],「行」:[{「elements」:[{「distance」:{「text」:「5.9 km」,「value」:5948 },「duration」:{「text」:「9分鐘」,「value」:541},「status」:「OK」}]}],「status」:「OK」}

08-18 09:52:01.286 3297-3894/com.companyname.emitless2 d/APP_TEST:https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins= 22.29129833333333,113.94699999999999 &目的地=亞洲國際博覽館&模式=行走&鍵= 「已刪除」

08-18 09:52:01.287 3297-3297/com.companyname.emitless2 D/Testing:{「destination_addresses」:[「香港赤臘角長榮路」],「origin_addresses」:[ 「行」:[{「elements」:[{「distance」:{「text」:「5.9 km」,「value」:5948},「duration」: {「text」:「9分鐘」,「值」:541},「狀態」:「確定」}]}],「狀態」:「確定」}

08-18 09:52:01.865 3297 -3894/com.companyname.emitless2 d/APP_TEST:https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins= 22.29129833333333,113.94699999999999 &目的地=亞洲國際博覽館&模式=騎車&鍵= 「已刪除」

08-18 09:52:01.865 3297-3297/com.companyname.emitless2 D/Testing:{「destination_addresses」:[「香港赤Kok角長榮路」],「origin_addresses」:[「Yi香港東涌東路「],」行「:[{」elements「:[{」distance「:{」text「:」5。9公里「,」值「:5948},」持續時間「:{」文本「:」9分鐘「,」值「:541},」狀態「:」確定「}]}],」狀態「:」 「}

9月8日至18日:52:02.409 1672年至1672年/ system_process W /窗口管理:試圖除去不存在的令牌:[email protected]

9月8日至18日:52:02.495 3297-3320/com.companyname.emitless2 d/EGL_emulation:eglMakeCurrent:0x7ebacc5cc6e0:第二版0(TINFO 0x7ebadbdb9d00)

9月8日至18日:52:02.540 3297-3320/com.companyname.emitless2 d/EGL_emulation:eglMakeCurrent :0x7ebacc5cc6e0:ver 2 0(tinfo 0x7ebadbdb9d00)

08-18 09:52:03.632 1369-1410 /? W/audio_hw_generic:沒有提供足夠的數據到HAL,預期位置1080247,只寫927360

08-18 09:52:04.458 1672-1672/system_process W/WindowManager:嘗試刪除不存在的令牌:android.os .Binder @ f7c5953

+1

我已經從您的代碼中刪除了API密鑰。無論如何都要考慮它的泄漏,所以我建議禁用它並獲得一個新的。您幾乎不應該在StackOverflow或其他公共平臺上發佈API密鑰。 –

+0

非常感謝,粗心 –

+0

可以請您提供** LogCat **嗎? –

回答

2

您正在每次迭代中創建一個名爲inList的ArrayList類型的新實例。因此,當您創建新實例並向其中添加項目時,該項目始終爲0.通過將循環外部的arrayList實例創建移動來修復它。

override fun doInBackground(vararg p0: String?): String { 
try { 
    val inString = ArrayList<String>() 
    for (i in 0 until p0.size) { 
     val url = URL(p0[i]) 
     val urlConnect = url.openConnection() as HttpURLConnection 
     urlConnect.connectTimeout = 7000   
     inString.add(ConvertStreamToString(urlConnect.inputStream)) 
     publishProgress(inString[i]) 
     try { 
      Thread.sleep(500) 
     } catch (e: InterruptedException) {} 
    } 
}catch (e:Exception){} 
return " " 
} 
+0

超級漂亮! –

+0

好的。但仍然不會將jsonObject [1]輸出到jsonObject [5]。請檢查更新的帖子和一切。我已經按照你的建議進行了修改。感謝您的時間。 –