Android Defaul Http Client
In my android app i have to get response from the php server. So i used DefaultHttpClient in that i have to maintain session cookies for subsequent requests for that i avoided creation of new instance for DefaultHttpClient using singleton pattern.But the problem is when i switch off and on the phone app creates new instance for DefaultHttpClient so my session c开发者_Go百科ookies get destroyed.How to maintain DefaultHttClient even when phone restarts?
Thanks.
I suggest you save your cookie data into a persistent storage ( like SharedPreferences / SQLite / or a cookie file ).
Here is a link to more info on Android Storage
You can do this in your onPause()
method.
Then you can get the data in your onResume()
method.
And of course also check for local cookies in your onCreate()
I recommend using the Android Asynchronous Http Client library, which automatically handles persistent cookie storage to android SharedPreferences
.
See the documentation here:
http://loopj.com/android-async-http/#persistent-cookie-storage-with-persistentcookiestore
精彩评论