Vipul Narkhede
Vipul Narkhede

Reputation: 133

Maintaining user session in Android

I am trying to make social network application for Android. My question is how to maintain user session when user logs in?

Please help me to find the solution for the above question.

Upvotes: 4

Views: 2930

Answers (5)

Zar E Ahmer
Zar E Ahmer

Reputation: 34370

Create session using SharedPreferences.

public class Session {

    private SharedPreferences prefs;

    public Session(Context cntx) {
        // TODO Auto-generated constructor stub
        prefs = PreferenceManager.getDefaultSharedPreferences(cntx);
    }

    public void setusename(String usename) {
        prefs.edit().putString("usename", usename).commit();

    }

    public String getusename() {
        String usename = prefs.getString("usename","");
        return usename;
    }
}

now after making this class when u want to use this use like this make object og this class like

private Session session;//global variable 
session = new Session(cntx); //in oncreate 
//and now we set sharedpreference then use this like

session.setusename("USERNAME");
now when ever u want to get username then same work for session object and call this

session.getusename();

best of luck :) same for password

Upvotes: 0

user3407138
user3407138

Reputation: 15

I have the similar problem on my android client side when I am trying to send that session id ,the server side is creating a new session...but what you check at android client side that you are not creating the DefaulthttpClient twice... create the httpclient just once say main activity and pass the objects in other activity ...... dont create second HttpClient

Upvotes: 0

Protostome
Protostome

Reputation: 6039

I use DefaultHttpClient with HttpRequestInterceptor and HttpResponseInterceptor. Something similar to this:

public class HTTPClients {

    private static DefaultHttpClient _defaultClient;
    private static String session_id;
    private static HTTPClients _me;
    private HTTPClients() {

    }
    public static DefaultHttpClient getDefaultHttpClient(){
        if ( _defaultClient == null ) {
            _defaultClient = new DefaultHttpClient();
            _me = new HTTPClients();
            _defaultClient.addResponseInterceptor(_me.new SessionKeeper());
            _defaultClient.addRequestInterceptor(_me.new SessionAdder());
        }
        return _defaultClient;
    }

    private class SessionAdder implements HttpRequestInterceptor {

        @Override
        public void process(HttpRequest request, HttpContext context)
                throws HttpException, IOException {
            Log.d("SessionKeeper", "Adding session with the following string: " + session_id);
            if ( session_id != null ) {
                request.setHeader("Cookie", session_id);
            }
        }

    }

    private class SessionKeeper implements HttpResponseInterceptor {

        @Override
        public void process(HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            Header[] headers = response.getHeaders("Set-Cookie");
            if ( headers != null && headers.length == 1 ){
                Log.d("SessionKeeper", "Keeping session with the following string: " + headers[0].getValue());
                session_id = headers[0].getValue();
            }
        }

    }
}

Upvotes: 0

Hades
Hades

Reputation: 3936

http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/

Please look at the above link. It is detailed pretty well.

Use a singleton to maintain the user session.

Upvotes: 0

Aalok Sharma
Aalok Sharma

Reputation: 1025

try

public class Session {
private static String sessionId;
private static String userRole;

public static void setSessionId(String sessionId) {
    Session.sessionId = sessionId;
}

public static String getSessionId() {
    return sessionId;
}

}

Use this class and import it in every other activity. You can define your own functions to maintain your specific session data

Upvotes: 8

Related Questions