Android保存用户会话 [英] Android save user session

查看:89
本文介绍了Android保存用户会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,我试图保存登录会话并在每次向服务器发送请求时将其发送,我的Web服务是PHP(Zend Framework 2).

I'm new in android I'm trying to save login session and send it with every time I send request to server, My web service is PHP (Zend framework 2).

此代码可识别用户:

$auth = Zend_Auth::getInstance();
$IdentityObj = $auth->getIdentity();

可以保存会话并发送它,就像我使用网络浏览器一样,而无需更改网络服务.

It's possible save session and send it like I use web browser without make any change in my web service.

推荐答案

是的.您只需使用 SharedPreferences 来存储会话.

Yes, it is. You just have to store the session using SharedPreferences.

想法是,登录后,我想您会返回某种会话ID或其他任何内容.您只需要存储它.然后,在后续执行中,您只需在执行任何请求之前获取该会话即可.

The idea is, after you login, I guess you return some kind of session id or whatever. You just need to store it. Then, in subsequents executions, you just fetch that session before doing any request.

SharedPreferences preferences = 
    getSharedPreferences("com.blabla.yourapp", Context.MODE_PRIVATE);

//Save it
preferences.edit().putString("session", <yoursessiontoken>).commit();

//Fetch it
// The second parameter is the default value.
preferences.getString("session", ""); 

无论如何,最好阅读文档以真正了解自己在做什么.

Anyway is better that you read the documentation to really understand what you are doing.

这篇关于Android保存用户会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆