android异步任务,Android:运行4个异步任务(适用于Rest API),在完成所有任务后等待并启动另一个活动[重复]...

公孙宏畅
2023-12-01

这个问题在这里已有答案:

我有异步任务的问题 . 我试图运行4个异步任务,从last.fm rest api获取数据 .

My task points:

异步或并行运行4个任务(计数无关紧要,但必须大于1)

从last.fm rest api中获取数据(用于从last.fm api获取数据使用de.umass.lastfm java库)

等待完成所有任务

使用提取的数据启动另一个活动

调用异步任务的函数:

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_animation)

initViewElements();

mArtistName = intent.getStringExtra("ARTIST_NAME");

Log.i(TAG, mArtistName);

setAnimationData();

getArtistData(mArtistName);

startActivity(Intent(this, DetailsActivity::class.java))

}

功能在哪里介绍我的任务

 
 

private fun getArtistData(name: String) {

val getArtistDataAsync = object : AsyncTask() {

override fun doInBackground(vararg args: String?): Artist? {

return Artist.getInfo(args[0], "26cc2ebf6da38bc646733f661bfc6268");

}

override fun onPostExecute(result: Artist?) {

super.onPostExecute(result)

CommonUtilities.artistInstance = result as Artist;

}

}

val getTopTracksAsync = object : AsyncTask>(){

override fun doInBackground(vararg args: String?): ArrayList? {

return Artist.getTopTracks(args[0], "26cc2ebf6da38bc646733f661bfc6268") as ArrayList;

}

override fun onPostExecute(result: ArrayList?) {

super.onPostExecute(result)

CommonUtilities.trackListInstance = result;

}

}

val getSimilarBandsAsync = object : AsyncTask>(){

override fun doInBackground(vararg args: String?): ArrayList? {

return Artist.getSimilar(args[0], "26cc2ebf6da38bc646733f661bfc6268") as ArrayList?

}

override fun onPostExecute(result: ArrayList?) {

super.onPostExecute(result)

CommonUtilities.artistListInstance = result; //Instance Messenger Pattern

}

}

val getTopAlbumsAsync = object : AsyncTask>(){

override fun doInBackground(vararg p0: String?): ArrayList? {

return Artist.getTopAlbums(p0[0],"26cc2ebf6da38bc646733f661bfc6268") as ArrayList?;

}

override fun onPostExecute(result: ArrayList?) {

super.onPostExecute(result)

CommonUtilities.albumListInstance = result; //Instance Messenger Pattern

}

}

getArtistDataAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, name)

getSimilarBandsAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, name)

getTopTracksAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, name)

getTopAlbumsAsync.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, name)

}

堆栈跟踪:

 
 

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4

Process: pw.osin.musicexpert, PID: 17334

java.lang.RuntimeException: An error occured while executing doInBackground()

at android.os.AsyncTask$3.done(AsyncTask.java)

at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)

at java.util.concurrent.FutureTask.setException(FutureTask.java:222)

at java.util.concurrent.FutureTask.run(FutureTask.java:242)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at java.lang.Thread.run(Thread.java:841)

Caused by: de.umass.lastfm.CallException: Caching/Reloading failed

at de.umass.lastfm.Caller.call(Caller.java:246)

at de.umass.lastfm.Caller.call(Caller.java:189)

at de.umass.lastfm.Caller.call(Caller.java:185)

at de.umass.lastfm.Artist.getSimilar(Artist.java:144)

at de.umass.lastfm.Artist.getSimilar(Artist.java:132)

at pw.osin.musicexpert.activities.AnimationActivity$getArtistData$getSimilarBandsAsync$1.doInBackground(AnimationActivity.kt:120)

at pw.osin.musicexpert.activities.AnimationActivity$getArtistData$getSimilarBandsAsync$1.doInBackground(AnimationActivity.kt:117)

at android.os.AsyncTask$2.call(AsyncTask.java)

at java.util.concurrent.FutureTask.run(FutureTask.java:237)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

at java.lang.Thread.run(Thread.java:841)

02-17 05:49:05.636 17334-17432/pw.osin.musicexpert W/dalvikvm: threadid=13: thread exiting with uncaught exception (group=0x4179ed58)

那我怎么能解决这个错误呢?

等待任务如何完成?

哪里需要调用startActivity方法?

我使用的是Kotlin,但任务方法与java类似 . 如果你能告诉我如何使用java编写我的代码也会很棒 .

谢谢

 类似资料: