在Android中维护用户会话

时间:2012-05-15 12:08:59

标签: android social-networking

我正在尝试为Android制作社交网络应用程序。我的问题是如何在用户登录时维护用户会话?

请帮我找到上述问题的解决方案。

5 个答案:

答案 0 :(得分:8)

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;
}

}

使用此类并将其导入其他所有活动。您可以定义自己的功能来维护特定的会话数据

答案 1 :(得分:0)

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

请看上面的链接。它很详细。

使用单例来维护用户会话。

答案 2 :(得分:0)

我将DefaultHttpClientHttpRequestInterceptorHttpResponseInterceptor一起使用。 类似的东西:

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();
            }
        }

    }
}

答案 3 :(得分:0)

我在Android客户端遇到类似的问题,当我尝试发送该会话ID时,服务器端正在创建一个新会话...但是你在Android客户端检查你没有创建两次DefaulthttpClient ...创建httpclient只需说一次主活动并在其他活动中传递对象......不要创建第二个HttpClient

答案 4 :(得分:0)

使用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;
    }
}

现在在制作这个类之后,当你想要使用这样的用法时,使这个类像对象一样

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();

祝你好运:)密码相同