Reputation: 24012
In my application i am using a PHP web server.
In the main activity, i make a connection to the server using Username and Password. Taking these values, the server starts my session.
So to maintain the session i use the same HTTP Client through out the Application.
public static HttpClient httpclient = new DefaultHttpClient();
But the problem is, most of the time if i want to fetch data from the server i use background threads to get data and then update the UI thread. So when the client i allocated in the background and user tries to make someother connection by clicking on a Button on the UI. It gives me this Exception
Invalid use of SingleClientConnManager: connection still allocated.
I am catching this Exception, so the application wont crash, but the session is getting destroyed. And hence i end up showing 'Null' in some View.
Can anyone please help me how to maintain sessions in a better way and how to avoid this SingleClientConnManager Exception.
Thank You
I thought of this approach:
when the application crashes or gives a singleClientConnManager exception (in either cases the session is lost) using java.lang.Thread.UncaughtExceptionHandler
i make a connection to the server again, using the username and password stored using Shared Preferences (this will start the session again) and then restart the current Activity. I did't try this, but i want to know is it a good practice?
Upvotes: 0
Views: 151
Reputation: 64399
You should try and avoid that exception, not catch it and work around it! The suggested approach does NOT look like good practice to me.
The problem seems to me that you have only a single connection, but the user can start 2. If that is the case, you should be able to handle it, or stop the user from doing so.
One option would be: recognize that you already have a connection (do not rely on the exception for this), and then start a new one. What I mean is that in this case you don't rely on the connection throwing an exception, but you check if there is allready one by 'normal' means (some variable for instance). You can go from there with what sounds like a bit of a hack (restarting the activity and all), but it is better then waiting for an exception.
Another option might be is stopping the user from pressing the button while the other request is still going on, but depending on your application that might be a user-interface fail. That depends on what the user sees and what's going on I guess. You can do this by using a spinner, disabling the buttons in all but form, there are nummerous ways to accomplish this.
Lastly, you might want to 'queue' the request and only send it to the server once the precious request is done. This way you can use the connection without exceptions. What I mean is that you keep everything as it is, but the action after pressing the second button is to remember you have to do the action, and after the first action returns, then check if there is anything else to do.
Do not rely on exceptions for the flow of your code!
Upvotes: 1