Ali
Ali

Reputation: 267067

Maintaining cookies between HTTP requests sent by Android Apps

I'm building an Android app to provide the functionality of a website on android phones.

People are required to login, and they remain logged in using cookies, allowing them to do things under their own account. All the code for logging in/authentication has been done for the website, and I'm now writing an Android app to do the same thing.

My question is, if the Android app sends a HTTP request to the PHP server, and the server in return sets some cookies (to show that the user has logged on), would these cookies remain set on the Android app during all future HTTP requests to the server?

Or, do I need to work out a new authentication scheme for android, e.g passing a special token to the app on user login, and the app providing this token during all future requests to authenticate the user?

Upvotes: 4

Views: 2216

Answers (3)

Dazzy_G
Dazzy_G

Reputation: 819

You should use the CookieManager and CookieStore To save Cookies.

CookieManager will store cookies of every incoming HTTP response into CookieStore, and retrieve cookies for every outgoing HTTP request.Expired HttpCookies should be removed from this store by themselves.

You would use this with the HttpURLConnection Class.

Upvotes: 3

subodh
subodh

Reputation: 6158

once you logged in put inside the SharedPreferences

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("userName", "Rock");
editor.putString("token", "token@123");

get from the SharedPreferences

SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String username = sharedPreferences.getString("userName", "");
String token = sharedPreferences.getString("token", "");

Upvotes: 1

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

Once You logined in App,you should save token in SharedPrefrences.And you can use in each Http Request later.Even when app is Restart you have that token.So SharedPredPrefrence is better for that.

Upvotes: 3

Related Questions