webview如何自动登录保存登录信息详情

在Linux下,Android模拟器里使用SD卡
http://www.eoeandroid.com/thread-201250-1-1.html

关于GridView布局和界面的跳转
http://www.eoeandroid.com/thread-201252-1-1.html

Android瀑布流加载图片效果实例
http://www.eoeandroid.com/thread-176638-1-1.html

 

这个是自己总结出来的经验,用户名密码登录验证后保存Session

// 你的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

        //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);

                }

        }

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

如果想保存用户信息,还是老方法SharedPreferences

原文链接:http://www.eoeandroid.com/thread-199811-1-1.html

 

 

你可能感兴趣的:(webView)