Handle multiple asynchronous responses - android

I am a web developer and writing services for android. The android developer, whom with I am working, is sending multiple async calls to the server, and asking me to send a service code with in the response of each service, as he will able to decide about a particular response belongs to which service that he sent before.
Is'nt there a way in android to keep track the responses of multiple async calls? Why should the server tell to android that which service is served.

There is no need for the server to send any code . He could send each http request using IntentService with a key. On each response from your server, he puts the key in a bundle, compare it and do what he wants.
See this code : https://github.com/JCERTIFLab/jcertif-android-2013/blob/master/src/com/jcertif/android/fragments/InitialisationFragment.java

I think their is no need of service code to keep track of the response-request pair, because if 5 threads sends the 5 different request, then, all of them receive their responses only.
Order of retrieval of response may vary.
Lets assume a condition,
ThreadA sends requestA.
ThreadB sends requestB.
Then,
ThreadA receives responseA only.
ThreadB receives responseB only.
Order variation:
ThreadB response may come before ThreadA.

Related

Is there a way to stop receiving data after service request via volley?

My intention is to stop receiving data from the server after the user has moved away from the activity which has made the service request. The motivation is to reduce the unnecessary bandwidth consumption as the user has navigated away from the activity and hence, the data is no longer required.
As far as I know, in Volley, it is only possible to cancel a request if it's in the request queue and not if it has already been sent.
So, is there any way to refuse the data being sent to the phone from the server or else, change the priority of the data acceptance to a lower level?
You can cancel the Volley Request which means you will not receive the response. When you are initiating Volley Request the request tag and cancel it via tag.
Hope this helps.

Android push notification with Google Cloud Messaging and Google App Engine as backend

I want to push notification from GAE backend to Android application. I've successfully implemented communication from this link: https://github.com/GoogleCloudPlatform/gradle-appengine-templates/tree/master/GcmEndpoints
But, I'm confused about logic here.
In this example, we have executed client registration to GCM, but via AsyncTask. I want to notify users when some code finishes execution on GAE backend, but without need to call some AsyncTask from Android. All I need is to receive message from GAE when this code finishes it's execution. Is this possible? I just don't know how workflow should be here.
Also, what needs to be done on client side, so that it will "listen" for new messages? Some service task which always runs in background? Or is this already done with public class GcmIntentService extends IntentService from this example, and I just need to identify message type?
If someone could explain this workflow to me, and some code examples would be nice too.
You should really go through the GCM guide, getting GCM running is not hard but it requires a series of steps that cannot be skipped.
In this example, we have executed client registration to GCM, but via AsyncTask. I want to notify users when some code finishes execution on GAE backend, but without need to call some AsyncTask from Android
A complete GCM implementation requires the client to register to GCM servers in order to get its unique ID and YOUR server to store that ID in order to contact that client directly, thats why you need at least one connection to send over this data to your server. you could get around this by using a new feature called topics, that way as soon as the client is registered it could suscribe to a topic and your server wouldn't need to know the specific GCM keys for any of your clients. I wouldn't recommend this last approach as it's not as scalable and will disable any 1 on 1 communication with your server.
Also, what needs to be done on client side, so that it will "listen" for new messages? Some service task which always runs in background? Or is this already done with public class GcmIntentService extends IntentService from this example, and I just need to identify message type?
You need to setup a broadcast reciever to handle incoming messages to your app (all detailed in the guide) from there you get the raw messages and you can do whatever you want with it, includng running it through an intent service as you state or processing it directly on the reciever (not recommended). Yes, you can discriminate message types and execute the logic accordingly.

Android - sending data to a web service

I'm about to write an application that sends data to a server with a post request. These requests can fail and if they do I want them to be sent when the connection is back online.
What is the best way to implement this behavior?
EDIT
I've read some articles and come up with the following idea. I register a BroadcastReceiver in the manifest and tell it to listen for android.net.conn.CONNECTIVITY_CHANGE and android.net.wifi.WIFI_STATE_CHANGED. When the request can't be send I store the request. If the connection becomes alive then I will send the cached requests.
What is the best way to implement this behavior?
Make Volley requests from a bound Service, and send the result back to the Activity if it is still in the foreground.
EDIT:
won't this be a problem if the user decides to close the application?
That's exactly why you need to use a Service. If you do it from an Activity, the request continues even when the Activity is dismissed. If your Volley request is expressed as an anonymous class instance, it continues to hold an implicit reference to the outer Activity class, which leads to a memory leak / exception.
Check your Internet Connection continuously by using Handler or service ,if it is sends the data to the server otherwise dont send it.

Android Service and Http request

I wrote an application with behavior like this: "Android client will run a service to send a request HTTP POST to server, server will return data in JSON and delete it from Database".
Problem I got here is: When i start LoadDataActivity (this activity will start service "GetMessage" to receive data froLm server). If i start LoadDataActivity and finish this activity immediately (Service GetMessage will stop too) , that's mean I sent the HTTP POST to server but I have not receive the JSON result yet, while server deleted it in db, so i lost this data.
Is there anyway to help me fix it, but dont have to keep service still running when finish activity?
What you're doing is a bad idea and you need to change some architecture here.
Just because you sent an HTTP request to a server does not mean that the client will get the response. YOu could lose internet connection. A router can break. Someone can trip over a wire and pull it out. The server could crash after deleting the data but before sending it. It's a bad idea.
If you want to do this, you ought to do 1 of 2 things:
1)Have the client send a 2nd request to the server telling it its ok to delete the data now. Do not do it in one request.
2)Use Lazy Deletion. Rather than actually delete the data, add a column "IsDeleted" to the db. Its default should be 0. After sending the data, set it to 1 rather than deleting it. Then change all queries to the table to only return rows where IsDeleted is 0.
Or 3) 1 and 2 combined. This is the best answer.
As for your direct question- no, there's no way to force the HTTP result to come before an activity is finished. So this needs to be done in a service that is started, not bound. An IntentService would work well, so you could have it automatically terminate itself when the request is done, and it would just start a new instance if another request is pending.

Asynchronous HTTP response Android

I am trying to do the following and am looking into other people's experiences:
I want to have an HTTP server running on Android that receives GET requests from a client. From there, the server must deliver the query parameters to a Service that will do some processing (sometimes quite heavy). When the service is done, it sends back the data to the server that will then return the response.
Of course, the startService(intent) call is asynchronous so I'm wondering how to tell the server to wait for the processing to be done before sending back the response. At the moment, the inter process messaging is done with the Messenger and Message classes.
What sort of design would be able to achieve that?
Use a bound service (one that implements onBind()) and a transact call on the returned IBinder object. Or write your own AIDL to describe the service's synchronous interface.
You want to bind to your service on app startup, before the HTTP requests start coming, since the bind operation itself is async.
By the way, why does the functionality have to be in the service? If the service is in the same app, can you move it directly to the HTTP server class?

Categories

Resources