0

In my application I am working a lot with server communication and the deal is like this:

At first I have to make a call to a server to get a unique ID (GUID, or I think it's called UUID in java). after that I can perform other operations with the server while I have to send the received Id with my operation. the problem is that this session is valid for only 5 minutes of inactivity after that I need to get a new Id from the server.

So if the user quits the application and wasn't active then I just send him back to the log in screen to authenticate again and to receive a new unique Id from the server when he gets back to my application by overriding the onResume method in all of my Activities:

How to start Login Activity if user wasn't active for 5 minutes:

The Question: But what if the user using the application for 5 minutes and in this time he never makes any call to the server, his Id becomes invalid and the next his call to the server returns id invalid message.

So I could do some thing like this (pseudo-code):

class SessionInvalidAwareMethod(AsyncTask task)
{
    AsyncTask currentAsyncTask = task;
    AsyncTask authenticateAsyncTask;

    public SomeResult executeTask()
    {
        result = executeCurrentAsyncTask();
        if (result = sessionInvalid)
        { 
            executeAuthenticationTask()
            result = executeCurrentAsyncTask();
        }
        return result;
    }
}

The Problem: But I would like to avoid the call to the server and getting the session invalid message. So I hold Date parameter in my application class that hold for me the last time I go my id from the server. how could I use it to decide depending on how much time has passed if I should run the current AsyncTask or run the authenticateAsyncTask first and only after it's finished I should run the current AsyncTask (My problem is to do the second step in a synchronous way)?

can some one explain to me what is the right way to handle this kind of situations overall?

Thanks for your help friends.

4

1 回答 1

1

I would make the server handle the session using cookies. It will have a login/logout calls in its API. The client will use the login/logout calls accoding to the app needs (i.e. Activity timeout). The client can get the login status from the cookie.

If you don't have control over the server sessions timeout, you can make a keep-alive service that will make an empty call to the server every <5 min and start/stop it from the app according to the app needs.

于 2013-04-01T08:45:36.737 回答