0

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 . 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? If can't,what should I do when the app quit? save the cookie? Or can I save the httpClient instance? How to do that? Thank you!

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
leizh007
  • 71
  • 1
  • 7
  • Is that the Client that restarts or the Server? If the Client restarts you only need t save the cookies on the client side and reuse them (set cookie expiry to far future). If Server restarts you need to save sessions (in DB?) and restore them when Server starts. – Germann Arlington Jun 06 '14 at 12:48
  • the Client restart.So when the app quit.I save the cookie.And when the app start, I use the cookie I saved to set HttpClient ?so I can use the httpClient for further purposes? – leizh007 Jun 06 '14 at 12:57
  • Thanks a lot.Could you give me some information about how to get cookie from httpclient and how to save the cookie and when i get the cookie from file, how to set the httpclient used the cookie.Thank you.I searched that question but a got little useful information. – leizh007 Jun 06 '14 at 13:07

2 Answers2

0

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.

click here for more info about PreferenceManager and how to do it.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

I think the best solution is save your session cookie (probably JSESSIONID).

When your app goes down just write in some file (like session.txt) or even serialize some class with this information. 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.

Tiago Engel
  • 3,533
  • 1
  • 17
  • 22
  • Ok,thank you.So when the app quit.I save the cookie.And when the app start, I use the cookie I saved to set HttpClient ?so I can use the httpClient for further purposes? – leizh007 Jun 06 '14 at 12:54
  • Yes, here is an answer how you should configure a cookie in HttpClient http://stackoverflow.com/questions/4166129/apache-httpclient-4-0-3-how-do-i-set-cookie-with-sessionid-for-post-request – Tiago Engel Jun 06 '14 at 14:13