我有一个使用for循环填充的列表。我在具有此块的方法中运行此代码。我需要有关如何返回myReportLocations及其值的帮助
private fun getReportCoordinates(): ArrayList<HashMap<String, String>>{
var reportLocations : ArrayList<HashMap<String, String>> = ArrayList()
var myReportLocations : ArrayList<HashMap<String, String>> = ArrayList()
Fuel.get("http://kite.pythonanywhere.com/api/v1/progress").header(Headers.COOKIE, cookie).responseJson { request: Request, response: Response, result: Result<FuelJson, FuelError> ->
//incident_count = (count_occurrences('{', response.body().toString()) - 1).toString()
for (i in 0..result.get().array().length() -1){
//myReportLat = result.get().array().getJSONObject(i)["latitude"].toString()
//myReportLong= result.get().array().getJSONObject(i)["longitude"].toString()
var reportCoord: HashMap<String, String> = HashMap()
//println("The coordinates are: " + myReportLat+ " : " + myReportLong)
reportCoord.put("LocationID", i.toString())
reportCoord.put("Latitude", result.get().array().getJSONObject(i)["latitude"].toString())
reportCoord.put("Longitude", result.get().array().getJSONObject(i)["longitude"].toString())
reportCoord.put("LocationName", "Progress Report")
reportLocations.add(reportCoord)
}
/*for (x in 0..reportLocations.size -1){
println("This is the " + x + "th coordinate " + reportLocations.get(x))
}*/
myReportLocations.addAll(reportLocations)//everything is stored here
}
myReportLocations//there nothing stored
return myReportLocations
}
答案 0 :(得分:1)
以下是您的解决方案:
private fun getReportCoordinates(result: (list:ArrayList<HashMap<String, String>> -> Unit) {
var reportLocations : ArrayList<HashMap<String, String>> = ArrayList()
var myReportLocations : ArrayList<HashMap<String, String>> = ArrayList()
Fuel.get("http://kite.pythonanywhere.com/api/v1/progress").header(Headers.COOKIE, cookie).responseJson { request: Request, response: Response, result: Result<FuelJson, FuelError> ->
//incident_count = (count_occurrences('{', response.body().toString()) - 1).toString()
for (i in 0..result.get().array().length() -1){
//myReportLat = result.get().array().getJSONObject(i)["latitude"].toString()
//myReportLong= result.get().array().getJSONObject(i)["longitude"].toString()
var reportCoord: HashMap<String, String> = HashMap()
//println("The coordinates are: " + myReportLat+ " : " + myReportLong)
reportCoord.put("LocationID", i.toString())
reportCoord.put("Latitude", result.get().array().getJSONObject(i)["latitude"].toString())
reportCoord.put("Longitude", result.get().array().getJSONObject(i)["longitude"].toString())
reportCoord.put("LocationName", "Progress Report")
reportLocations.add(reportCoord)
}
/*for (x in 0..reportLocations.size -1){
println("This is the " + x + "th coordinate " + reportLocations.get(x))
}*/
result(myReportLocations)
}
}
答案 1 :(得分:0)
这是一个异步调用。因此,列表将在返回后进行更新。 您要做的就是将其转换为阻塞的网络呼叫。 (尽管我会使用其他线程,否则如果您在主线程上执行此操作,则您的应用程序将冻结)
尝试:
df %>%
mutate(k2 = x[as.character(k)])
}