做android项目难免遇到这种情况,需使用webview加载网页,并且获取cookie。或者需要设置自己的请求cookie蔡翁正常加载网页。下面分别介绍一下android 中 webview的使用方法。

1.获取url中的cookie:

    方法一:我们需要自己写一个cookie管理类:MyWebViewClient

public class MyWebViewClient extends WebViewClient {    String url;    WebView view;    Activity activity;    TextView textView;    public MyWebViewClient(Activity activity)    {      //  this.textView =textView;        this.activity =activity;    }    public boolean shouldOverrideUrlLoading(WebView view, String url) {        view.loadUrl(url);        return true;    }    public void onPageFinished(WebView view, String url) {        CookieManager cookieManager = CookieManager.getInstance();        String CookieStr = cookieManager.getCookie(url);        Common.cookie = CookieStr;        Toast.makeText(activity,CookieStr,Toast.LENGTH_SHORT).show();        super.onPageFinished(view, url);    }}

在主Activity方法中调用:

public class MainActivity extends Activity {    private WebView mWebView;    private MainActivity self;    private String url ="https://www.baidu.com";//带cookie的页面url    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        self = MainActivity.this;        mWebView = (WebView) findViewById(R.id.webview_forshow);        mWebView.getSettings().setAppCacheEnabled(false);        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);        mWebView.getSettings().setDatabaseEnabled(false);        mWebView.getSettings().setDomStorageEnabled(false );        mWebView.getSettings().setGeolocationEnabled(false);//        mWebView.getSettings().setPluginsEnabled(false);        mWebView.getSettings().setSaveFormData(false);        mWebView.getSettings().setSavePassword(false);        mWebView.getSettings().setJavaScriptEnabled(true); ///------- 设置javascript 可用        mWebView.loadUrl(url);        mWebView.setWebChromeClient(new WebChromeClient()        {            @Override            public void onProgressChanged(WebView view, int newProgress)            {                super.onProgressChanged(view, newProgress);                view.requestFocus();            }        });        mWebView.setWebViewClient(new MyWebViewClient(self));          }}

布局文件:

<?xml version="1.0" encoding="utf-8"?>    

    方法二

CookieManager cookieManager = CookieManager.getInstance();String cookieStr  = cookieManager.getCookie(url);

注意:android需要连接网络的时候,要给予网络请求权限:

     
2.获取url中设置请求cookie:

   在Load url之前调用 setCookies 方法:

/** * 设置Cookie * * @param url */private void setCookies(String url) {    if (!TextUtils.isEmpty(strCookies)) {        String arrayCookies[] = strCookies.split(";");        if (arrayCookies != null && arrayCookies.length > 0) {            for (String cookie : arrayCookies) {               // synCookies(url, cookie);                synCookies(this, url, cookie);            }        }    }}/** * 同步Cookie * * @param url * @param cookie 格式:uid=21233 如需设置多个,需要多次调用 */@TargetApi(Build.VERSION_CODES.LOLLIPOP)private void synCookies(String url, String cookie) {    try {        CookieManager cookieManager = CookieManager.getInstance();        cookieManager.setAcceptCookie(true);        cookieManager.setCookie(url, cookie);//cookies是在HttpClient中获得的cookie        cookieManager.flush();    }catch (Exception e){        ToastUtil.showMessage(getString(com.neusoft.phone.xinhua.newsedit.R.string.no_support));    }}

/** * 设置Cookie * * @param context * @param url * @param cookie  格式:uid=21233 如需设置多个,需要多次调用 */public void synCookies(Context context, String url, String cookie) {    CookieSyncManager.createInstance(context);    CookieManager cookieManager = CookieManager.getInstance();    cookieManager.setAcceptCookie(true);    cookieManager.setCookie(url, cookie+";Domain=hotspot.******;Path=/");//cookies格式自定义    CookieSyncManager.getInstance().sync();}/** * 清除Cookie * * @param context */public static void removeCookie(Context context) {    CookieSyncManager.createInstance(context);    CookieManager cookieManager = CookieManager.getInstance();    cookieManager.removeAllCookie();    CookieSyncManager.getInstance().sync();}


更多相关文章

  1. MPAndroidChart的一些问题解决方法
  2. 解决IE apk变成zip:Android 手机应用程序文件下载服务器 配置解决
  3. Android EditText TextWatcher 回调方法参数说明
  4. android Webview加载url空白,但浏览器能打开
  5. android WebView, WebChromeClient和WebViewClient加载网页基本
  6. Android生成界面的两种方法:setContentView和inflate的区别
  7. Android 使用Canvas在图片上绘制文字的方法
  8. Android 项目快速更改包名的方法

随机推荐

  1. Others
  2. Android(安卓)中的两端对齐实例详解
  3. android获取屏幕相关信息
  4. Android(安卓)Bluetooth使用详解
  5. android 学习视频汇总
  6. 插画人物的光影怎么画?零基础绘画光影教程
  7. Linux-安装ifconfig
  8. 如何练习画线稿?初学者画线稿步骤
  9. 解决:Failed to download metadata for re
  10. PHP最新的插件化后台管理系统真的蛮好用