关于webview如何自动登录保存登录信息

【转载请注明原作者和原文链接】

转自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=199811&fromuid=568472

用户名密码登录验证后保存Session
01 // 你的URL
02                 HttpPost httppost = newHttpPost(Constants.ServerUrl.WEB_URL + url);
03                 try{
04  
05                         List<NameValuePair> nameValuePairs = newArrayList<NameValuePair>(2);
06  
07                         // Your DATA
08                         nameValuePairs.add(newBasicNameValuePair("UserName", map
09                                         .get("UserName")));
10  
11                         nameValuePairs.add(newBasicNameValuePair("Password", map
12                                         .get("Password")));
13  
14                         httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
15  
16                         HttpResponse response;
17  
18                         response = httpclient.execute(httppost);
19                         if(response.getStatusLine().getStatusCode() == 200) {
20                                 // 获得响应
21                                 String result = EntityUtils.toString(response.getEntity());
22                                 // SharedPreferences preferences = context.getSharedPreferences(
23                                 // "login", Context.MODE_PRIVATE);
24                                 // Editor editor = preferences.edit();
25                                 // String handlers = "";
26                                 String cookie = "";
27                                 Date sessionTime = newDate();
28                                 List<Cookie> cookies = httpclient.getCookieStore().getCookies();
29                                 if(!cookies.isEmpty()) {
30                                         for(inti = 0; i < cookies.size(); i++) {
31                                                 cookie = cookies.get(i).getName() + "="
32                                                                 + cookies.get(i).getValue() + ";domain="
33                                                                 + cookies.get(i).getDomain();
34                                                 ;
35                                                 sessionTime = cookies.get(i).getExpiryDate();
36                                         }
37                                 }
38                                //保存session
39                                 PersistentConfig config = newPersistentConfig(context);
40                                 config.setCookie(cookie);
41  
42                                 config.setSessionTime(sessionTime.getTime());
43                                 // for (int i = 0; i < response.getAllHeaders().length; i++) {
44                                 // if (response.getAllHeaders()<i>.toString().contains(
45                                 // "set-cookie")) {
46                                 // handlers += response.getAllHeaders()<i>.toString();
47                                 // break;
48                                 // }
49                                 // }
50                                 //
51                                 // editor.putString("cookies",
52                                 // response.getHeaders("Set-Cookie")[0].toString());
53                                 // editor.commit();
54                                 returnresult;
55                         }
56  
57                 }catch(ClientProtocolException e) {
58  
59                         // TODO Auto-generated catch block
60  
61                         e.printStackTrace();
62  
63                 }catch(IOException e) {
64  
65                         // TODO Auto-generated catch block
66  
67                         e.printStackTrace();
68  
69                 }</i></i>


如何在Webview请求网站时使用Session
01         //webview初始化的时候调用
02         main_view = (WebView) findViewById(R.id.main_webview);
03         newWebViewTask().execute();
04          
05         privateclass WebViewTask extendsAsyncTask<Void, Void, Boolean> {
06                 String sessionCookie;
07                 CookieManager cookieManager;
08  
09                 @Override
10                 protectedvoid onPreExecute() {
11                         CookieSyncManager.createInstance(MainActivity.this);
12                         cookieManager = CookieManager.getInstance();
13  
14                         sessionCookie = newPersistentConfig(getApplicationContext())
15                                         .getCookieString();
16                         if(sessionCookie != null) {
17                                 // delete old cookies
18                                 cookieManager.removeSessionCookie();
19                         }
20                         super.onPreExecute();
21                 }
22  
23                 protectedBoolean doInBackground(Void... param) {
24                         // this is very important - THIS IS THE HACK
25                         SystemClock.sleep(1000);
26                         returnfalse;
27                 }
28  
29                 @Override
30                 protectedvoid onPostExecute(Boolean result) {
31                         if(sessionCookie != null) {
32                                 cookieManager.setCookie(Constants.ServerUrl.WEB_URL,
33                                                 sessionCookie);
34                                 CookieSyncManager.getInstance().sync();
35                         }
36                         main_view.setScrollbarFadingEnabled(true);
37                         main_view.getSettings().setPluginsEnabled(true);
38                         main_view.getSettings().setPluginState(PluginState.ON);
39                         main_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
40                         WebSettings webSettings = main_view.getSettings();
41                         webSettings.setJavaScriptEnabled(true);
42                         // 设置可以访问文件
43                         webSettings.setAllowFileAccess(true);
44                         // 设置支持缩放
45                         webSettings.setBuiltInZoomControls(true);
46                         main_view.setWebViewClient(newWebViewClient() {
47                                 @Override
48                                 publicvoid onPageStarted(WebView view, String url,
49                                                 Bitmap favicon) {
50                                         // TODO Auto-generated method stub
51                                         super.onPageStarted(view, url, favicon);
52                                 }
53  
54                                 @Override
55                                 publicvoid onLoadResource(WebView view, String url) {
56                                         // history();
57                                 }
58                         });
59                         main_view.setWebChromeClient(newWebChromeClient() {
60                                 publicvoid onProgressChanged(WebView view, intprogress) {
61                                         //Change
62                                 }
63                         });
64                         ShowUrlView(Constants.ServerUrl.INDEX);
65                 }
66         }

这样就可以每次刷新页面的时候带着session一起请求

如果想保存用户信息,还是老方法SharedPreferences
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值