last day (15 days later) » 

6:46 PM
0
A: Using the Singleton Pattern to synchronize HTTP Requests

eldjonYou dont need any Singleton to achieve what you aim. Use AsuncTask see doc here. Basically you will need to overrite doInBackGround() method which is executed on a background thread and here you will implement the HTTPRequest delivery. The background thread will wait until it gets the response fr...

 
I've considered this solution, but it seemed overly complicated to me since I need to make many different types of http requests in the app. To do that, wouldn't I need to make an individual async task for each one?
 
i would say that your original solution is definitely more complicated. what so you mean more precisely "different types of http requests"? you dont need necessarily to implement different AsyncTasks. You might provide a common interface for the HTTPRequests
 
yes, even though this way seems more complicated, i wanted to use it because it's supposed to provide a common interface. How would I provide a common interface with async tasks?
 
define your httprequests first
 
before submitting them to the task? sorry if this sounds n00b15H, i'm not very familiar with asyncTasks.
 
6:46 PM
i mean what does your HttpRequest look like that makes it so difficult to be applied with a AsyncTask.
 
i suppose its that the intent service is the interface for the entire server, every request that can be made to it, and so the way I was implementing that in the intent service was by a giant switch case
with certain strings being passed in as the parameters
could that be done in an AsyncTask?
 
of course
you may as well use intentservice
both solutions are feasible and do not require any singleton
if ull send very frequently http requests than the service is better solution because u wont need to create many objects for each request
otherwise AsyncTask perfectly fits ur needs
what type of Http requests ull send?
 
right now its just get and post
but the post doesn't have any args
they all just return strings
but if i am to use an intent service, how can i block the main thread without a singleton
i mean, as my question indicated, i'm not sure how to do it with a singleton either
 
u dont need to block
from what i understand u just need the results to be displayed on the main thread
after each http request is processed
 
yes, but they need to be processed first
 
6:58 PM
its alright . doesnt involve any blocking still
 
the response comes in the form of a JSON string, which contains an object array
i feel like it does, can i give you a bit of background?
 
yeah so u can parse that JSON in the onPostExecute() and do whatever is needed to be done
ok
 
so the information being returned is basically animation frames
its info being published on a gridview
that flashes at a certain rate in front of the user
when a start button is clicked, i need to make the http request, and then get the data and start showing it to the user
if i'm to show it to the user, should the ui thread have to wait for the data to be returned?
 
no
again onPostExecute() runs on ui thread
or main thread whatever u call it
so after u receive those frames you can start animation in that method
implement it inside the activity class as an inner class
 
is there an onPostExecute method for intentServices?
or this method would be exlusive for implementation with ASyncTasks
 
7:04 PM
services are not intended to update ui components
 
i see
so, its somewhat like a state machine
 
asyncTask was designed by android specifically for this type of problems
 
where the Async task is the last state
 
state machine?
 
well, i suppose that's just me trying to relate what we're talking about to familiar terms
 
7:06 PM
i understand
 
but as we spoke about before, this now introduces complexity in needing mulltiple async tasks for different types of requests/ui updates
no longer a common interface to the server
 
u dont need multiple asynctasks
you can provide a switch case block
inside the doInBackground() method the same way u intended to do for the intentService
AsyncTask does everything for u. starts a background thread, executes the task and notifies at the end of execution
believe me its quite straightforward solution
 
I see. Alright, i'll give it a shot
I really appreciate that you took the time to talk to me personally, I find this much more helpful than speaking through the topic system on the main site
 
no problem
 
can i help you out in anyway? by upvoting or rewarding you with rep points? i'm also pretty new to the site, so i'm not sure how i would go about doing that
 
7:11 PM
if it is useful what i suggested u can upvote/accept my answer
 
anything additionally?
 
nothing else
 
alright, will do; thanks :)
 
no problem
good luck
 

last day (15 days later) »