Polite timeouts when waiting for server response?

Put your problem here if it does not fit any of the other categories.

Polite timeouts when waiting for server response?

Postby nick_wa » Wed Mar 24, 2010 1:51 am

We are building a real time traffic analysis and reporting GPS application for our 4th year University project and I am in need of a little insight. Basically we are sending an object (which we serialize) via HTTP POST to our server and wait for the server to respond. When it responds we do some stuff with the response, etc., etc.

Problem is that sometimes the server will timeout and not respond or the response for whatever reason, won't make it back and the app will hang while waiting and eventually force a close on the application after a certain length of time.

Everything as is works fine when the server responds, it is the times that it doesn't that the application crashes

Here is the relevant lines...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //This is where we send data to server and wait for response, sendRetResponse will return us or object which we will cast and use as an HTTPResponse object
  3.  
  4. getResponse(sData.sendRetResponse(sObj.serializeObjReg(mRoad)));
  5.  
  6.  
  7.  
  8. private void getResponse(HttpResponse response){
  9.  
  10.         if (response.containsHeader("rStart")){
  11.  
  12.                 try {
  13.  
  14.                                 InputStream IS = response.getEntity().getContent();
  15.  
  16.                                 ObjectInputStream obj = new ObjectInputStream(IS);
  17.  
  18.                                 ridResponse serializedResponse = (ridResponse) obj.readObject();
  19.  
  20.                                 obj.close();
  21.  
  22.                                 final String theDist = serializedResponse.getRouteID(1).desc;
  23.  
  24. //etc. etc. etc.
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Does anybody have any ideas how I can rework this to only wait x seconds and then retry for example or at least do something politely so the application does not crash? Thanks in advance, really appreciate any help with this as deadline is approaching quickly!
nick_wa
Junior Developer
Junior Developer
 
Posts: 15
Joined: Tue Mar 09, 2010 9:51 pm

Top

Re: Polite timeouts when waiting for server response?

Postby bher20 » Wed Mar 24, 2010 5:57 pm

nick_wa wrote:We are building a real time traffic analysis and reporting GPS application for our 4th year University project and I am in need of a little insight. Basically we are sending an object (which we serialize) via HTTP POST to our server and wait for the server to respond. When it responds we do some stuff with the response, etc., etc.

Problem is that sometimes the server will timeout and not respond or the response for whatever reason, won't make it back and the app will hang while waiting and eventually force a close on the application after a certain length of time.

Everything as is works fine when the server responds, it is the times that it doesn't that the application crashes

Here is the relevant lines...

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //This is where we send data to server and wait for response, sendRetResponse will return us or object which we will cast and use as an HTTPResponse object
  2. getResponse(sData.sendRetResponse(sObj.serializeObjReg(mRoad)));
  3.  
  4. private void getResponse(HttpResponse response){
  5.         if (response.containsHeader("rStart")){
  6.                 try {
  7.                                 InputStream IS = response.getEntity().getContent();
  8.                                 ObjectInputStream obj = new ObjectInputStream(IS);
  9.                                 ridResponse serializedResponse = (ridResponse) obj.readObject();
  10.                                 obj.close();
  11.                                 final String theDist = serializedResponse.getRouteID(1).desc;
  12. //etc. etc. etc.
  13.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Does anybody have any ideas how I can rework this to only wait x seconds and then retry for example or at least do something politely so the application does not crash? Thanks in advance, really appreciate any help with this as deadline is approaching quickly!



I dont know if this will help but, you can put the code that listens for the server response in a separate thread, and have just have the thread continuously listening for the server to send something. Thats how I deal with getting server responses in my app, but I use sockets.

Android Threads

-Brad
bher20
Freshman
Freshman
 
Posts: 4
Joined: Wed Mar 24, 2010 5:06 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests