简体   繁体   English

重新启动应用程序后,如何使用HttpClient保持登录状态以进行后续操作?

[英]How to use HttpClient to keep login state for furture process after the app was restarted?

My app use HttpPost to login a discuss so I can get some pages that must need to be loged in used the same httpClient instance . 我的应用程序使用HttpPost登录讨论,因此我可以获取一些必须使用同一httpClient实例登录的页面。 But if my app was quit and restart again after sometime. 但是,如果我的应用程序退出并在一段时间后重新启动。 Can I use the httpClient response that keep me the state that I have loged in? 我可以使用使我保持登录状态的httpClient响应吗? If can't,what should I do when the app quit? 如果不能,退出应用程序该怎么办? save the cookie? 保存cookie? Or can I save the httpClient instance? 还是可以保存httpClient实例? How to do that? 怎么做? Thank you! 谢谢!

What you need to do is save the user's data to the PreferenceManager when the user login for the first time and when the app quit and then restarted you can then check the PreferenceManager if it have some data or not before you do some action. 您需要做的是,当用户首次登录时以及在应用退出并重新启动时,将用户数据保存到PreferenceManager ,然后可以在执行某些操作之前检查PreferenceManager是否包含一些数据。

click here for more info about PreferenceManager and how to do it. 单击此处了解有关PreferenceManager及其操作方法的更多信息。

I think the best solution is save your session cookie (probably JSESSIONID). 我认为最好的解决方案是保存您的会话cookie(可能是JSESSIONID)。

When your app goes down just write in some file (like session.txt) or even serialize some class with this information. 当您的应用程序关闭时,只需写入一些文件(例如session.txt),甚至使用此信息序列化某些类。 And when your app starts, check if this file exists, get the session id from it, delete the file, and set the session cookie in your HttpClient. 然后在您的应用启动时,检查该文件是否存在,从中获取会话ID,删除该文件,然后在HttpClient中设置会话cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM