Jhorra
Jhorra

Reputation: 6321

Android: Keeping session alive on remote server between Activities

I have another application built in iOS, and when the user logs into the app the server logs them in on that session. In the iOS app the session is kept alive as long as normal sessions and all requests from the app see the person still logged in.

I assumed this would be the same with the Android app, but after logging in I switch to the next activity, then send a request to the server to populate the page. The result I'm getting back is saying the user isn't logged in.

Is there a difference here that I'm not understanding that is not keeping my session on the server alive?

I have a base class that all my Activities extend, and inside the base class I have this protected class. I use it to send all the requests.

protected class API extends AsyncTask <Void, Void, String>
{
    public String RequestName;
    public String RequestType;
    public String RequestUrl;
    public List<NameValuePair> RequestParameters;

    public API(String Name, String Type, String Url, List<NameValuePair> params)
    {
        RequestName = Name;
        RequestType = Type;
        RequestUrl = Url;
        RequestParameters = params;
    }

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException 
    {
        InputStream in = entity.getContent();
        StringBuffer out = new StringBuffer();
        int n = 1;
        while (n>0) 
        {
            byte[] b = new byte[4096];
            n =  in.read(b);
            if (n>0) 
                out.append(new String(b, 0, n));
        }
        return out.toString();
    }

    @Override
    protected String doInBackground(Void... params) 
    {


        HttpClient httpClient = new DefaultHttpClient();
        HttpContext localContext = new BasicHttpContext();

        String text = null;

        if(RequestType == "post")
        {
            HttpPost p = new HttpPost("http://www.ehrx.com/api/" + RequestUrl);

            try 
            {
                p.setEntity(new UrlEncodedFormEntity(RequestParameters));
                HttpResponse response = httpClient.execute(p, localContext);
                HttpEntity entity = response.getEntity();
                text = getASCIIContentFromEntity(entity);
            } 
            catch (Exception e) 
            {
                return e.getLocalizedMessage();
            }
        }
        else
        {
            HttpGet httpGet = new HttpGet("http://www.ehrx.com/api/" + RequestUrl);

            try 
            {
                HttpResponse response = httpClient.execute(httpGet, localContext);
                HttpEntity entity = response.getEntity();
                text = getASCIIContentFromEntity(entity);
            } 
            catch (Exception e) 
            {
                return e.getLocalizedMessage();
            }
        }



        return text;
    }

    protected void onPostExecute(String results) 
    {
        HandleResult(results, RequestName);

    }
}

Upvotes: 1

Views: 2991

Answers (2)

frankelot
frankelot

Reputation: 14409

This can be easily fixed using a singleton class (you should use the same HTTP client throughout all your http requests).

Upvotes: 0

digitaljoel
digitaljoel

Reputation: 26574

I would guess the session is still alive on the server but you are losing your session cookie in the android app. It looks like you are creating a new client and a new context for every request. You will want to set the cookie store for the context so it can use the session cookie from your previous requests where you have authenticated. The answer to How do I manage cookies with HttpClient in Android and/or Java? should give you more information.

Upvotes: 5

Related Questions