[Android实例] 关于webview如何自动登录保存登录信息
2014-10-10 11:05
936 查看
用户名密码登录验证后保存Session
?
[java]
view plaincopy
// 你的URL
HttpPost httppost = new HttpPost(Constants.ServerUrl.WEB_URL + url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Your DATA
nameValuePairs.add(new BasicNameValuePair("UserName", map
.get("UserName")));
nameValuePairs.add(new BasicNameValuePair("Password", map
.get("Password")));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response;
response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
// 获得响应
String result = EntityUtils.toString(response.getEntity());
// SharedPreferences preferences = context.getSharedPreferences(
// "login", Context.MODE_PRIVATE);
// Editor editor = preferences.edit();
// String handlers = "";
String cookie = "";
Date sessionTime = new Date();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (!cookies.isEmpty()) {
for (int i = 0; i < cookies.size(); i++) {
cookie = cookies.get(i).getName() + "="
+ cookies.get(i).getValue() + ";domain="
+ cookies.get(i).getDomain();
;
sessionTime = cookies.get(i).getExpiryDate();
}
}
//保存session
PersistentConfig config = new PersistentConfig(context);
config.setCookie(cookie);
config.setSessionTime(sessionTime.getTime());
// for (int i = 0; i < response.getAllHeaders().length; i++) {
// if (response.getAllHeaders()<i>.toString().contains(
// "set-cookie")) {
// handlers += response.getAllHeaders()<i>.toString();
// break;
// }
// }
//
// editor.putString("cookies",
// response.getHeaders("Set-Cookie")[0].toString());
// editor.commit();
return result;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}</i></i>
如何在Webview请求网站时使用Session
[java]
view plaincopy
//webview初始化的时候调用
main_view = (WebView) findViewById(R.id.main_webview);
new WebViewTask().execute();
private class WebViewTask extends AsyncTask<Void, Void, Boolean> {
String sessionCookie;
CookieManager cookieManager;
@Override
protected void onPreExecute() {
CookieSyncManager.createInstance(MainActivity.this);
cookieManager = CookieManager.getInstance();
sessionCookie = new PersistentConfig(getApplicationContext())
.getCookieString();
if (sessionCookie != null) {
// delete old cookies
cookieManager.removeSessionCookie();
}
super.onPreExecute();
}
protected Boolean doInBackground(Void... param) {
// this is very important - THIS IS THE HACK
SystemClock.sleep(1000);
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (sessionCookie != null) {
cookieManager.setCookie(Constants.ServerUrl.WEB_URL,
sessionCookie);
CookieSyncManager.getInstance().sync();
}
main_view.setScrollbarFadingEnabled(true);
main_view.getSettings().setPluginsEnabled(true);
main_view.getSettings().setPluginState(PluginState.ON);
main_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings webSettings = main_view.getSettings();
webSettings.setJavaScriptEnabled(true);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
// 设置支持缩放
webSettings.setBuiltInZoomControls(true);
main_view.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onLoadResource(WebView view, String url) {
// history();
}
});
main_view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//Change
}
});
ShowUrlView(Constants.ServerUrl.INDEX);
}
}
原文地址:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=199811
?
[java]
view plaincopy
// 你的URL
HttpPost httppost = new HttpPost(Constants.ServerUrl.WEB_URL + url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// Your DATA
nameValuePairs.add(new BasicNameValuePair("UserName", map
.get("UserName")));
nameValuePairs.add(new BasicNameValuePair("Password", map
.get("Password")));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response;
response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200) {
// 获得响应
String result = EntityUtils.toString(response.getEntity());
// SharedPreferences preferences = context.getSharedPreferences(
// "login", Context.MODE_PRIVATE);
// Editor editor = preferences.edit();
// String handlers = "";
String cookie = "";
Date sessionTime = new Date();
List<Cookie> cookies = httpclient.getCookieStore().getCookies();
if (!cookies.isEmpty()) {
for (int i = 0; i < cookies.size(); i++) {
cookie = cookies.get(i).getName() + "="
+ cookies.get(i).getValue() + ";domain="
+ cookies.get(i).getDomain();
;
sessionTime = cookies.get(i).getExpiryDate();
}
}
//保存session
PersistentConfig config = new PersistentConfig(context);
config.setCookie(cookie);
config.setSessionTime(sessionTime.getTime());
// for (int i = 0; i < response.getAllHeaders().length; i++) {
// if (response.getAllHeaders()<i>.toString().contains(
// "set-cookie")) {
// handlers += response.getAllHeaders()<i>.toString();
// break;
// }
// }
//
// editor.putString("cookies",
// response.getHeaders("Set-Cookie")[0].toString());
// editor.commit();
return result;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}</i></i>
如何在Webview请求网站时使用Session
[java]
view plaincopy
//webview初始化的时候调用
main_view = (WebView) findViewById(R.id.main_webview);
new WebViewTask().execute();
private class WebViewTask extends AsyncTask<Void, Void, Boolean> {
String sessionCookie;
CookieManager cookieManager;
@Override
protected void onPreExecute() {
CookieSyncManager.createInstance(MainActivity.this);
cookieManager = CookieManager.getInstance();
sessionCookie = new PersistentConfig(getApplicationContext())
.getCookieString();
if (sessionCookie != null) {
// delete old cookies
cookieManager.removeSessionCookie();
}
super.onPreExecute();
}
protected Boolean doInBackground(Void... param) {
// this is very important - THIS IS THE HACK
SystemClock.sleep(1000);
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (sessionCookie != null) {
cookieManager.setCookie(Constants.ServerUrl.WEB_URL,
sessionCookie);
CookieSyncManager.getInstance().sync();
}
main_view.setScrollbarFadingEnabled(true);
main_view.getSettings().setPluginsEnabled(true);
main_view.getSettings().setPluginState(PluginState.ON);
main_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings webSettings = main_view.getSettings();
webSettings.setJavaScriptEnabled(true);
// 设置可以访问文件
webSettings.setAllowFileAccess(true);
// 设置支持缩放
webSettings.setBuiltInZoomControls(true);
main_view.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onLoadResource(WebView view, String url) {
// history();
}
});
main_view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
//Change
}
});
ShowUrlView(Constants.ServerUrl.INDEX);
}
}
原文地址:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=199811
相关文章推荐
- [Android实例] 关于webview如何自动登录保存登录信息
- [Android实例] 关于webview如何自动登录保存登录信息
- 关于webview如何自动登录保存登录信息
- 关于webview如何自动登录保存登录信息
- 【分享】webview如何自动登录保存登录信息
- webview如何自动登录保存登录信息详情
- webview如何自动登录保存登录信息详情
- webview如何自动登录保存登录信息详情
- 关于Android使用Xutils的WebView保存Cookie登录
- 【android-Webview】设置多个cookie,实现webview中保存登录信息
- Android_WebView加载网页保存信息到Cookie
- Android 关于WebView里的页面第三方登录
- Android WebView 新浪微博 OAuth 登录时会记住上次登录信息
- android中的activity布局内含有webview,弹出软键盘如何自动适应屏幕,自动调整
- Android WebView保存Cookie登录
- Android WebView保存Cookie登录
- Android WebView保存Cookie登录
- android webview如何处理自动提取手机号
- Android WebView详解之自动登录
- Andriod中WebView加载登录界面获取Cookie信息并同步保存,使第二次不用登录也可查看个人信息。