用于跟踪登录会话的Android应用程序策略(Android App Strategy for keeping track of a login session)
如果登录成功,我有一些PHP脚本登录并返回带有会话ID的JSON数组。
在我的应用程序中,我想在首页登录并继续浏览正在登录的应用程序。我创建了一个singleton类,它包含从PHP页面的JSON接收的会话ID(以及其他一些字段)。 这个单例对象的字段“session_id”会根据用户的操作进行检查。
如果用户想注销,那么session_id会被设置为空,从而注销。
登录时,我也使用HttpURLConnection库来发布用户名/密码。
这是处理这种情况的足够体面的方法吗?
I have some PHP script that logs in and returns a JSON array with a session ID if the login was successful.
In my app, I want to login at the front page and continue out through the app being logged in. I created a singleton class that holds a session ID (along with a few other fields) received from the JSON from the PHP page. This singleton object's field "session_id" gets checked depending on what the user does.
If the user wants to log out, then the session_id just gets set to null thus logging out.
I also use the HttpURLConnection library to POST the username/password when logging in.
Is this a decent enough approach for handling this situation?
原文:https://stackoverflow.com/questions/7706449
最满意答案
是的,它会工作(如果你追加
()
到c_str
使它实际调用函数),断言将通过。Yes it will work (if you append
()
toc_str
to make it actually call the function) and the assertion will pass.
相关问答
更多-
调用空字符串的c_str(Calling c_str of empty string)[2021-09-20]
是的,它会工作(如果你追加()到c_str使它实际调用函数),断言将通过。 Yes it will work (if you append () to c_str to make it actually call the function) and the assertion will pass. -
你的分析是正确的。 你有什么是未定义的行为 。 这意味着几乎任何事情都可能发生。 在你的情况下,似乎在字符串中使用的内存虽然被取消分配,但在访问它时仍然保存原始内容。 这通常是因为操作系统不清除未分配的内存。 它只是标记为可供将来使用。 这不是C ++语言必须处理的:它实际上是一个操作系统的实现细节。 就C ++而言,所有的“未定义行为”都适用。 Your analysis is correct. What you have is undefined behaviour. This means pretty ...
-
std::get<1>(*it)返回std :: string类型的对象。 此类具有重载operator ==以将std :: string类型的对象与字符数组进行比较。 std::get<1>(*it).c_str()返回一个字符数组。 数组没有比较运算符。 要比较字符数组,您应该使用标准C函数std::strcmp 所以你可以写 if( std::strcmp( std::get<1>(*it).c_str(), "PAUSE" ) == 0 ) 如果你会简单地写作 if(std::get<1>(*i ...
-
为什么c_str()为两个不同的字符串返回相同的值?(Why does c_str() return the same value for two different strings?)[2022-02-28]
代码被破坏。 您正在对立即销毁的临时对象调用c_str() 。 这意味着由c_str()返回的值是无效的。 您需要确保返回的std::string对象至少在您保持由调用c_str()返回的指针时生存。 例如: std::string some_file = load_file("some_file"); std::string another_file = load_file("another_file"); printf("%s", some_file.c_str()); printf("%s", ano ... -
c_str()== NULL不起作用(c_str() == NULL not working)[2022-02-28]
在这种情况下a不为空。 字符串对象有一个内部指针,它不是null ,该指针是c_str返回的内容。 要验证这一点,请尝试 printf("%zu\n", a.c_str()); 你会得到一个真实的地址。 a is not null in this case. The string object has an internal pointer, which is not null, and that pointer is what c_str returns. To verify this, try pri ... -
C ++字符串和C字符串之间的区别(.c_str())[重复](Difference between a C++ string and a C-string ( .c_str() ) [duplicate])[2022-11-09]
C ++字符串和以null结尾的字符序列(C-string).c_str()之间的真正区别是什么? C ++ std::string对象封装: 存储语义(可能是文本)值的char数组 一些实现直接在std::string对象中存储短文本字符串 否则堆内存通常用于存储实际的字符串内容 一个指针(可能通过一些其他控制结构)到字符数组 std::string::size_type变量记录std::string::size_type的大小和容量 可能还有其他的事 实际上, std::string的文本数据 - 无论 ... -
将c_str()存储为char *(Store c_str() as char *)[2023-05-06]
你遇到的问题是c_str()返回一个无法修改的缓冲区( const ),而stem()可能会修改你传入的缓冲区(而不是const )。 您应该复制c_str()的结果以获得可修改的缓冲区。 页面http://www.cplusplus.com/reference/string/string/c_str/提供了有关C ++ 98和11版本的更多信息。 他们建议替换char * b = s.c_str(); 具体如下: char * b = new char [s.length()+1]; std::strcp ... -
你试过以下吗? yPos = strtod(inputString.substr(commaLast,commaCurrent-1).c_str(), NULL); Have you tried the following? yPos = strtod(inputString.substr(commaLast,commaCurrent-1).c_str(), NULL);
-
someFunctionTakingCStrings(path.string().c_str()); 是安全的,因为标准保证匿名临时path.string()的生命周期在函数调用中幸存。 因此, c_str()返回的指针是someFunctionTakingCStrings的有效参数。 const std::string path::string() const是安全的,因为从概念上讲,你返回的是tmp的值副本,尽管在实践中编译器会优化值副本(称为命名返回值优化的过程 )。 类似于const std::st ...
-
c_str()自定义实现/“=”在每个字符串的末尾(c_str() custom implementation / “=” in the end of every string)[2022-04-01]
问题出在这里: temp[data.size() + 1] = '\0'; char数组分配给data.size() + 1个字节,因此索引应该从0到data.size() 。 temp[data.size() + 1]就在数组边界之外。 此代码也可能导致未定义的行为。 它应该是 temp[data.size()] = '\0'; The problem is here: temp[data.size() + 1] = '\0'; The char array is allocated for dat ...