android


How to save data extracted from cookies to shared preference in android?


My code is as follows :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient(){
public void onPageFinished(WebView view, String url){
String cookies = CookieManager.getInstance().getCookie(url);
try {
SharedPreferences settings = getSharedPreferences("Tokens", 0);
SharedPreferences.Editor editor = settings.edit();
int index1 = cookies.indexOf("access_token=");
int index2 = cookies.indexOf("expires_in=");
int index3 = cookies.indexOf("refresh_token=");
editor.putString("access_token", cookies.substring(index1 + 13, index2 - 2));
editor.putString("expires_in", cookies.substring(index2 + 11, index3 - 2));
editor.putString("refresh_token", cookies.substring(index3 + 14));
editor.commit();
}
catch(Exception ex)
{
Log.i("MyActivity","in exception 1"+ ex.getMessage());
}
Log.i("MyActivity",cookies);
}});
myWebView.loadUrl("https://pocketvstsdev.azurewebsites.net/api/VstsOAuth/Initiate");
try {
SharedPreferences settings = getSharedPreferences("Tokens", 0);
Log.i("MyActivity", settings.getString("access_token", null) + " " + settings.getString("expires_in", null) + " " + settings.getString("refresh_token", null));
}
catch(Exception ex)
{
Log.i("MyActivity","in exception 2" + ex.getMessage());
}
}
I am trying to hit a url, which passes tokens in cookies after successful login. I wish to save the tokens passed in the cookies in shared preferences.
If the cookies do not consist of the required fields, then I want to restart the main activity.
Using the code given above, exception is occurring and tokens are not getting stored into shared preferences.

Related Links

What is TextView Gravity by default?
User Mockito to unit test a function doing async task in my case
add and re-use long numbers to/from android resources
HttpURLConnection GET request not returning anything
[SOLVED]Error: module “QtQuick” is not installed appears when I deploy qt quick application on Andorid
get 3d objects name by touching using min3d android
Retrofit with String response
Unable to get the current count from sqlite database in android using Group By clause
CWAC-CAM2 : CameraActivity confirmation requires and action bar
Android Studio inital gradle build takes a long time
What should I do or instead getActivity()
Enabling ProGuard causes failing signed APK generation?
Android facebook library sromku get photos returns null
Error in Manifest merging
why i am getting An established connection was aborted by the software in your host machine in android studio [duplicate]
Android : ListView not appearing or not showing in Android

Categories

HOME
deep-learning
rdbms
character-encoding
powerpoint
camera
jwplayer
sitecore
gradient
vscode-extensions
alsa
web-hosting
azure-container-service
performancepoint
accessibility
spin
pattern-matching
g++
distribution
comsol
qsqlquery
do-while
internationalization
package.json
boost-icl
react-toolbox
postscript
visual-studio-extensions
pimcore
dynamically-generated
xunit
siri
google-cloud-print
corpus
cppreference
iis-express
tizen-tv
calculation
dst
mailkit
yowsup
fiware-cygnus
xajax
git-rewrite-history
metawidget
parser-generator
contract
servermanager
xbmc
insight
pybrain
filenet
redbeard-ios
file-uri
avspeechsynthesizer
xdoclet
android-wake-lock
qiime
omniauth-facebook
qbxml
rewrite
rate-limiting
code-documentation
rapidsvn
iiop
entry-point
human-computer-interface
personalization
live555
renderer
font-awesome-4.0.0
http-status-code-410
sqlclr
wimax
dajaxice
window-decoration
rmdir
cloudfiles
mtu
commerceserver2007
ruby-1.8
updatesourcetrigger
tui

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App