I am trying to design a program that makes multiple API calls on a website (Each "name" has several modes that I have to loop through before I move on to the next name). The problem is, you are limited to calling the API once every second. I thought the handler was the way to go but now I don't think so.
It runs through the loop just fine but I don't think so. I get the following error:
EDIT: Figured out that this was being caused by
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.开发者_Go百科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
When I make this call in my Asynchronous task:
String[] metrics = r.getStringArray(R.array.metric_apicall);
I don't think my code is designed right for what I am trying to do. Can anyone tell me if this is the right approach?...Also, APICallData(MainActivity.this, app, mode).execute(); is an asynchronous task.
//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();
}
I think you should use Timer and TimerTask and exeute it with an interval of 1 sec.
精彩评论