Server-Side Development – Handling Lost Internet Connectivity During Server Requests

android-developmentclient-sideserverserver-side

I am sending a huge amount of data to server. Now while I have sent the data and waiting for server response, suddenly my android device gets internet connection lost.
So what I used to do is, showing an alert dialog of connection lost, but at server side the data was already processed and it updated somewhere e.g. on any URL. But my android phone does not know this as it did not get response ever. How to resolve it.
Whether it could be done on server side or on android itself How?
How server would know that android phone is not going to listen the response?
It may be client-server communication optimization perspective.

Best Answer

This is a fairly common problem with asynchronous transactions, and falls into several parts.

  1. How do both sides know that the transaction request has been successfully received?
  2. How do you resend a transaction request that the client believes has not been received properly?
  3. How does the server detect repeat requests from the client when the server successfully received the first request?
  4. How does the client know where to get the transaction results from?

The great thing about HTTP is that its fairly easy to solve all of these issues.

Imagine a URL structure like this:

POST http://my.server.com/application/engine/queue
GET  http://my.server.com/application/engine/results?jobid=43425

Using HTTP post to send a request to the server, using a unique client request id - and have the server respond with the job ID. From a clients perspective, if this response does not occur, then the request needs to be resent. From a servers perspective client request id's need to be cached for a few minutes, in-case the client sends duplicated requests. Duplicated requests are handled simply by returning the same job ID to the client.

The client gets the results of the request from the results URL. This call can be repeated as often as needed to get the results. If its called before the results are available, then the response could be a NO-CONTENT response so the client knows that the server recognises the job id but does not have the content yet. If the job id is not recognised then NOT-FOUND is the appropriate response.

End result is that the client can always make a sensible action when the network is lost and recovered, and likewise the server can always process requests from the client sensibly