android自动登录简书,关于Webview如何自动登录保存登录信息
[Android实例] 关于Webview如何自动登录保存登录信息用户名密码登录验证后保存Session// 你的URLHttpPost httppost = new HttpPost(Constants.ServerUrl.WEB_URL + url);try {List nameValuePairs = new ArrayList(2);// Your DATAnameValuePairs.a
[Android实例] 关于Webview如何自动登录保存登录信息
用户名密码登录验证后保存Session
// 你的URL
HttpPost httppost = new HttpPost(Constants.ServerUrl.WEB_URL + url);
try {
List nameValuePairs = new ArrayList(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 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().toString().contains(
// "set-cookie")) {
// handlers += response.getAllHeaders().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();
}
如何在Webview请求网站时使用Session
//webview初始化的时候调用
main_view = (WebView) findViewById(R.id.main_webview);
new WebViewTask().execute();
private class WebViewTask extends AsyncTask {
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);
}
}
更多推荐
所有评论(0)