已解决: 我在进行并发修改时遇到问题,我认为这是由于在完成任务之前调用了过滤器,最终对我有用的是添加了对外部的调用将值复制到copyonwritearraylist的onPostExecute中的方法。
所以我有一个AsyncTask,它解析网站中的json并在完成后初始化列表。
inner class GetSchoolAsync : AsyncTask<String, String, String>() {
lateinit var list: MutableList<Info>
override fun onPreExecute() {
// Before doInBackground
}
override fun doInBackground(vararg urls: String?): String {
var urlConnection: HttpURLConnection? = null
try {
val url = URL(urls[0])
urlConnection = url.openConnection() as HttpURLConnection
urlConnection.connectTimeout = CONNECTON_TIMEOUT_MILLISECONDS
urlConnection.readTimeout = CONNECTON_TIMEOUT_MILLISECONDS
val inString = urlConnection.inputStream.bufferedReader().readText()
publishProgress(inString)
} catch (ex: Exception) {
println("HttpURLConnection exception" + ex)
} finally {
if (urlConnection != null) {
urlConnection.disconnect()
}
}
return " "
}
override fun onProgressUpdate(vararg values: String?) {
try {
var list = mutableListOf<Info>()
var data = Gson().fromJson(values[0], Read::class.java)
for(item in data.schools){
list.add(item)
// println(item)
}
this.list = list
} catch (ex: Exception) {
println("JSON parsing exception" + ex.printStackTrace())
}
}
override fun onPostExecute(result: String?) {
// Done
schoolList = this.list
}
}
它在mainActivity OnCreate中与另一个呈现地图的异步函数一起执行。学校名单是主要活动中的私人Lateinit变量。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
GetSchoolAsync().execute("https://code.org/schools.json")
mapFragment.getMapAsync(this)
}
我需要知道json部分何时完成以及列表是否已初始化,以便可以将标记添加到地图。每当我尝试在调试器中检查Async任务的状态时,它总是显示其未决或正在运行。我何时可以确定完成?
答案 0 :(得分:0)
您最好在案例中使用Interface
interface ReturnListener {
fun result(result:String)
}
inner class GetSchoolAsync : AsyncTask<Object, String, String>() {
lateinit var list: MutableList<Info>
override fun onPreExecute() {
// Before doInBackground
}
override fun doInBackground(vararg urls: Object?): String {
var urlConnection: HttpURLConnection? = null
var listener: ReturnListener? = null
try {
val url = URL(urls[0] as String)
listener = urls[1] as ReturnListener
urlConnection = url.openConnection() as HttpURLConnection
urlConnection.connectTimeout = CONNECTON_TIMEOUT_MILLISECONDS
urlConnection.readTimeout = CONNECTON_TIMEOUT_MILLISECONDS
val inString = urlConnection.inputStream.bufferedReader().readText()
publishProgress(inString)
} catch (ex: Exception) {
println("HttpURLConnection exception" + ex)
} finally {
if (urlConnection != null) {
urlConnection.disconnect()
}
}
return " "
}
override fun onProgressUpdate(vararg values: String?) {
try {
var list = mutableListOf<Info>()
var data = Gson().fromJson(values[0], Read::class.java)
for(item in data.schools){
list.add(item)
// println(item)
}
this.list = list
} catch (ex: Exception) {
println("JSON parsing exception" + ex.printStackTrace())
}
}
override fun onPostExecute(result: String?) {
// Done
schoolList = this.list
if (listener!=null)
listener.result(result)
}
并在OnCreate下:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
GetSchoolAsync().execute("https://code.org/schools.json", object: ReturnListener{
override fun result(result:String) {
//ToDo: .......
}
})
mapFragment.getMapAsync(this)
}