0

I have developed an app to communicate with my own server and published it. However, sometimes the app force closes. I know there is no bug in the code because the app works properly most of the time, but sometimes it is waiting for an answer from the server forever. I think this is due to the fact that so many people are using the app, and the app refreshes every 1 second or so, so this heavy traffic causes the server to take a large amount of time to respond. So how do you take care of such a use case? should I have a use case where if the server does not respond after some time you just stop the app and throw a message saying that the server is not responding or something like that?

tshepang
  • 12,111
  • 21
  • 91
  • 136
user522559
  • 239
  • 5
  • 13
  • 4
    "I know there is no bug in the code..." are famous last words. – Zoot Jan 13 '11 at 21:51
  • 1
    The "force close" is because of an uncaught exception. Check the logcat output and find out what type of exception it is and where it's being thrown. Things should become a lot clearer. – Dan Dyer Jan 13 '11 at 22:01
  • 1
    If your app works most of the time, and fails some of the time, there is a bug in the code. – Cheryl Simon Jan 13 '11 at 22:37

2 Answers2

2

Right now, your main application is timing out due to server load. If you put your connection details in a thread, you will be able to avoid having that main thread time out. You can check for updated data from the connection thread (through some shared object) and then present a message to the user if the data has stopped.

Haphazard
  • 10,900
  • 6
  • 43
  • 55
  • what details are you referring to specifically? – user522559 Jan 13 '11 at 22:03
  • The Android OS knows when your app is waiting too long for a connection and will show the "Force Close or Wait" dialog box after a certain threshold. If you put your server-connection code in a thread and have it write to a queue of some sort, your main app can read from that queue. – Haphazard Jan 13 '11 at 22:08
1

It sounds like you have your server communication code within your main Activity. Any code running in this Activity will be run in the main UI thread. If your code sends a request to your server, and is then waiting for a response, the main UI thread is blocked until your server responds. The Android OS recognises that the UI thread has effectively hung, and kills your app.

What you need to do is to separate out the UI code in your Activity from the server communication code. You should move this into an AsyncTask or a new Thread/Handler combination. This will allow the UI to remain responsive even when your server is under load.

Community
  • 1
  • 1
dave.c
  • 10,910
  • 5
  • 39
  • 62