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

Can't show bitmap in ImageView
Android move Image with Sensor
onItemCheckedStateChanged called twice
Is there a code converter from Windows 8 Apps to Android? [closed]
What DataStorage can be used to Create Personal Task Manager?
Android: how to forcefully reproduce “OpenGL context loss” issue?
What happens if I tune my lowmemorykiller thresholds to be too low?
Does Repo tool have any versioning commands
VPN to debug android and other devices?
onActivityResult return null getExtra value after return back from permission dialogue
Android Geofence Entry Transition Event Triggered even when Not Inside the Geofence
Android app crashing after Zen Desk integration
Static android shortcuts for multiple flavors?
Android : Drop zone misbehaviour
Error while building android application, Android NDK
Android Cannot create new folder

Categories

HOME
scikit-learn
prism
wakanda
zend-framework
static
entity-framework-core
gembox-spreadsheet
android-json
iis-6
lanczos
minecraft-forge
vimdiff
watson-conversation
mod-security
ycsb
xcodebuild
supervisor
gource
event-sourcing
microsoft-dynamics-nav
wolframalpha
enterprise-miner
symbols
microsoft-ocr
dpdk
pimcore
direct3d11
onload
definitelytyped
moses
firebase-analytics
inversion-of-control
mobx
sql-server-2000
login-script
python-pptx
openerp-7
urlsession
news
contract
clang++
jboss-4.2.x
lampp
magento-2.0.7
sonarlint-eclipse
tf
posixct
corruption
scalding
headless
arules
baidu
django-1.10
ampps
pure-css
stax
reporting-services-2012
webjars
rspec2
ng-grid
clipboard.js
cfchart
aurora
createprocess
ellucian-scribe
beanstalk
grass
jnlua
relativelayout
directdraw
xenomai
oracle-spatial
roadkill-wiki
eoferror
skspritenode
cwrsync
diff3
java-security
delayed-execution
angular-gettext
dsym
android-usb
gtm-oauth2
inappsettingskit
windows-98
server-administration
twisted.web
asplinkbutton
iboutlet
inkcanvas
usertype
ilasm
http-daemon
ruby-1.8
objective-j
instantiationexception
silent
maven-1
hbm2java
java1.4
dsoframer
cardspace

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App