0
我想設計一個程序,使網站上的多個API調用(每個「名稱」有幾個模式,我必須循環,然後再移動到下一個名稱)。問題是,你僅限於每秒調用一次API。我認爲處理程序是要走的路,但現在我不這麼認爲。多個處理程序循環Android
它貫穿循環就好,但我不這麼認爲。我得到以下錯誤:
編輯:想通了,這正在造成
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): FATAL EXCEPTION: AsyncTask #1
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): java.lang.RuntimeException: An error occured while executing doInBackground()
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at android.os.AsyncTask$3.done(AsyncTask.java:266)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.lang.Thread.run(Thread.java:1020)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): Caused by: android.content.res.Resources$NotFoundException: String array resource ID #0x7f050002
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at android.content.res.Resources.getStringArray(Resources.java:459)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at com.companionfree.flurryanalytics.APICallData.doInBackground(APICallData.java:66)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at com.companionfree.flurryanalytics.APICallData.doInBackground(APICallData.java:1)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at android.os.AsyncTask$2.call(AsyncTask.java:252)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-21 22:33:46.760: ERROR/AndroidRuntime(9683): ... 4 more
當我撥打這個電話在我的異步任務:
String[] metrics = r.getStringArray(R.array.metric_apicall);
我不認爲我的代碼是爲我正在嘗試做的而設計的。任何人都可以告訴我,如果這是正確的方法?...此外,APICallData(MainActivity.this,應用程序,模式).execute();是一個異步任務。
//Other code above this irrelevant
mApp = 0;
mMode = 0;
callAPI(mMode, names[mApp]);
mMode++;
while (mApp < names.length) {
while (mMode < metrics.length) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
callAPI(mMode, names[mApp]);
}
}, 1010);
mMode++;
}
mMode = 0;
mApp++;
}
private void callAPI(int mode, String app) {
new APICallData(MainActivity.this, app, mode).execute();
}
這難道不會凍結整個過程嗎?我需要它作爲後臺更新... – easycheese
直接從java文檔.....計時器是線程的工具,用於在後臺線程中安排將來執行的任務。可以安排一次性執行任務,或定期重複執行任務。 對應於每個Timer對象是一個後臺線程,用於執行所有定時器的任務。希望這有助於清除你的疑惑 – CommonMan