java 什么是android java中的会话变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10750329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is session variable in android java?
提问by Manick
I am self-leaner to android and java.Is there any session variables available in android java? If so please let me know about its purpose and process.If possible with samples.
我对 android 和 java 是自学成才的。android java 中是否有可用的会话变量?如果是这样,请让我知道它的目的和过程。如果可能,请提供样品。
Thanks for your precious time!.
感谢您的宝贵时间!。
回答by Adil Soomro
When talking about session what do you mean by session
在谈论会话时,会话是什么意思
Because if you talk about Session to store login credentials or user information.
因为如果您谈论 Session 来存储登录凭据或用户信息。
It may be a SharedPreferences
.
它可能是一个SharedPreferences
.
and if you talk about temporary data storage, like data downloaded from internet or another application related data, which is intended to be shared throughout the application can be shared using many means like Application
class, static
variable, File
IO, Sqlite
etc.
如果你谈论的临时数据存储,就像从网上或其他应用程序相关的数据下载的数据,其目的是分享整个应用程序可以使用多种方法等共享Application
类,static
变量,File
IO,Sqlite
等等。
回答by Andrew
Android doesn't have session variables that I am aware of. However, if your Android app is communicating with a web server, your app can make use of the PHP session variables on the web server. Here is how:
Android 没有我知道的会话变量。但是,如果您的 Android 应用程序正在与 Web 服务器通信,则您的应用程序可以使用 Web 服务器上的 PHP 会话变量。方法如下:
1) Android app sends user's login credentials to your web server (www.your-web-server.com/login.php)
2) login.php checks the credentials and then calls session_start()
to begin a session.
3) login.php then calls something like this: my_current_session = session_id();
in order to get the session ID of the current session.
4) login.php then sends the session ID (my_current_session
) back to the Android app. I recommended echoing the session ID as a JSON-encoded string (json_encode).
5) The Android app stores the session ID locally. You can stored it in SharedPreferences as some of the other answers have suggested. Or you can store it in a SQLite database locally on the device.
6) You can then include the session ID in the Request Headersof all future GET and POST requests from the Android app to the web server. The PHP running on the web server will recognize the session ID and make the associated session variables available.
1) Android 应用程序将用户的登录凭据发送到您的 Web 服务器 (www.your-web-server.com/login.php)
2) login.php 检查凭据,然后调用session_start()
以开始会话。
3) login.php 然后调用类似这样的东西:my_current_session = session_id();
为了得到当前会话的会话ID。
4) login.php 然后my_current_session
将会话ID ( )发送回 Android 应用程序。我建议将会话 ID 作为 JSON 编码的字符串 (json_encode) 回显。
5) Android 应用程序在本地存储会话 ID。您可以按照其他一些答案的建议将其存储在 SharedPreferences 中。或者您可以将其存储在设备本地的SQLite 数据库中。
6) 然后您可以在请求标头中包含会话 ID从 Android 应用程序到 Web 服务器的所有未来 GET 和 POST 请求。在 Web 服务器上运行的 PHP 将识别会话 ID 并使关联的会话变量可用。
回答by Relsell
why do you need it.. There is application variables that get initialize during application start up. You can also make your singleton class to achive that.
为什么需要它.. 在应用程序启动期间会初始化一些应用程序变量。你也可以让你的单例类来实现这一点。
回答by PravinK
There is no api available to stored values in session rather you can use SharedPreferences, or can have static variables which can stored values which you want to save in session.
会话中没有可用于存储值的 api,而您可以使用SharedPreferences,或者可以使用静态变量来存储要保存在会话中的值。