last day (18 days later) » 

11:37 AM
1
A: Android queue for API requests

kris larsonThis is an interesting problem, because in my apps I tend to use a dedicated AsyncTask for each different type of server request. So when I was reading this question, I was asking myself: how would I implement something like this when each task is a stand-alone component that doesn't know about ...

 
Kris, big thanks to you, but i need to figure out some positions 1) What is REQUEST_GAP_MILLIS 2) In my AsyncTask i should call getNextRequestDelay(), check if it equals or not to zero, and then sleep or not sleep my thread, right?
 
REQUEST_GAP_MILLIS would be the minimum number of milliseconds allowed between requests, so for example if you are only allowed 3 requests per second it would be 333. Also, your understanding of how to use it is correct; get the value from the dispenser and if its zero request immediately, if non-zero then call Thread,sleep() with that value before requesting.
Also keep in mind that you will need to do things like keep a reference to any executing task and cancel it if the activity is stopped.
 
Okay, i set REQUEST_GAP_MILLIS to 333, sLastRequestTime is initializatin as 0 on app run, but getNextRequestDelay is always giving back 0 and i still receiving "Too much requests..."
 
Like REQUEST_GAP_MILLIS in all uppercase implying a final/constant value, beginning the sLastRequestTime with an "s" was meant to imply that it is a static value, so that it retains its value in between calls. So whenever the dispenser method is called, it should have access to the timestamp of the previous call.
 
Yes, sLastRequestTime is static, and in debugger i see it keeping previous result
@krislarson , okay, i made it work, but when i use Thread.sleep in AsyncTask it freezing all app
is there any way to dont freeze app?
 
11:42 AM
I was afraid of something like that because Android tends to reuse threads. Let me check something...
 
Sure
 
I keep forgetting about the message queue. Try creating a Runnable with your request logic then creating a Handler and calling postDelayed() on the Handler with your Runnable.
 
so Runnable inside AsyncTask or insted of AsyncTask?
 
Ooh. The whole AsyncTask has to wait, I see your issue, give me a second...
 
Well, i just added Handler with runnble inside asynctask, and i really dont receiving "Too many requests..."
btw, maybe its jsut accident, but taking of ram and cpu time is less now
 
11:58 AM
Well, you should be able to call the dispenser method on the UI thread, I guess I would start the whole thing by getting that delay so if you have to call postDelayed(), then the Runnable.run() method would execute the AsyncTask. So if put the AsyncTask.execute in the Runnable, that should work okay.
 
okay, i will. but b4 ill go to delete all this garbage from queue i tryed to use
 
I updated the answer to mention Handler.postDelayed() instead of Thread.sleep()
Hey, I gotta run, I'll check in a little while to see how it's going...
 
 
9 hours later…
9:22 PM
Hey Kris, idk why, but your code always return 0, in any cases
 
 
2 hours later…
11:02 PM
Maybe you can post your code so I can look at it
 
11:23 PM
Also, I can't find any documentation on VK as far as API throttling, so I can't tell if the API throttling is requests per second, or per hour or per day.
 

  last day (18 days later) »