解析网页视图的HTML内容 - Android电子 [英] Parse HTML contents of Webview - Android

查看:128
本文介绍了解析网页视图的HTML内容 - Android电子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测时,我的WebView加载某个页面,例如,一个不正确的登录页面。我已经使用 onLoadResource shouldOverrideUrlLoading 试过,但我不能让无论是工作,我想更好的办法是,每当web视图开始加载一个页面,如果某个字符串在HTML中找到,那么做任何解析HTML。

I'd like to detect when my webview loads a certain page, for example, an incorrect login page. I've tried using onLoadResource and shouldOverrideUrlLoading, but I can't get either to work, and I'm thinking a better way would to parse the HTML whenever the webview starts loading a page, and if a certain string is found within the HTML, then do whatever.

有没有做到这一点的方法?我已经使用TagSoup试过,但我不知道如何把它与到我的WebView。这里是我的code样子现在:

Is there a method to do this? I've tried using TagSoup, but I have no clue how to relate it into my webview. Here's what my code looks like now:

String fullpost = "pass=" + passwordt + "&user=" + usernamet + "&uuid=" + UUID;

    String url = "mydomain.com";
        mWebview.postUrl(url, EncodingUtils.getBytes(fullpost, "BASE64"));
        mWebview.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView mWebview, String url) {
                String webUrl = mWebview.getUrl();
                    if (webUrl.contains("/loginf")) {
                        MainActivity.this.mWebview.stopLoading();
                        MainActivity.this.setContentView(R.layout.preweb);

                    }
              }
       });

基本上,postUrl是从在布局中的一个按钮,用户点击发起的,而这正是启动的WebView,然后我叫的setContentView 来包含布局web视图。

Basically, the postUrl is initiated from a user click on a button in a layout, and that's what starts the WebView, and then I call setContentView to the layout that contains the webview.

从那里,如果登录信息是正确的,该网页去XXX,如果它是不正确,它进入YYY。所以,我想立即检测(并在每个页面加载从那里出来),如果YYY被加载,那么 // domagic 。希望是有道理的。作为页从网​​址重定向到XXX YYY或者是自动的,而不是由用户发起, shouldOverrideUrlLoading 不起作用,我无法弄清楚如何使用 onLoadResource ,所以我只是完全丧失。

From there, if the login info is correct, the webpage goes to XXX, and if it's incorrect, it goes to YYY. So, I want to detect immediately (and on every page load from there on out), if YYY is loaded, then //domagic. Hope that makes sense. Being the page redirect from url to XXX or YYY is automatic and not initiated by the user, shouldOverrideUrlLoading doesn't work, and I can't figure out how to use onLoadResource, so I'm just completely lost.

我现在的想法是加载在一个单独的线程的一切,然后使用web视图中显示的内容(这样我可以解析HTML),但我不知道如何会工作,甚至如何做到这一点。

My current thought is loading everything in a separate thread and then using the WebView to display the content (that way I can parse the HTML), but I'm not sure how that'd work or even how to do it.

任何人有任何意见或建议?

Anyone have any ideas or suggestions?

推荐答案

我想我读过的方式来获得的WebView内容的文本字符串。然后,你可以使用jsoup来解析它。 [尼,甚至不需要jsoup;只是字符串的indexOf检查】

I think I've read a way to get a text string of a webview's content. Then, you could use jsoup to parse it. [neh, don't even need jsoup; just indexOf string check]

我会建议你考虑做处理登录HTTP客户端。它给你的灵活性,而且似乎更合适的路要走。我已经使用了HTTP GET和POST请求的循环J库。它允许简单的code。对我来说,无论如何,相对Android的新手。下面是我的一些项目code,让你思考。我已经离开了的东西,如进度条和cookie管理。

I'll suggest that you do consider handling the login with an HTTP client. It gives you flexibility, and seems the more proper way to go. I've been using the loopj library for HTTP get and post requests. It allows for simpler code. For me, anyway, a relative Android newbie. Here's some code from my project, to get you thinking. I've left out stuff like progress bar, and cookie management.

private void loginFool() {
    String urlString = "http://www.example.com/login/";

    // username & password
    RequestParams params = new RequestParams();
    params.put("username", username.getText().toString());
    params.put("password", password.getText().toString());

    // send the request
    loopjClient.post(urlString, params, new TextHttpResponseHandler() {

        @Override
        public void onStart() {
            // called before request is started
            //System.err.println("Starting...");
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            // called when response HTTP status is "200 OK"

            // see if 'success' was a failed login...
            int idx = responseString.indexOf("Please try again!");
            if(idx > -1) {
                makeMyToast("Sorry, login failed!");
            }
            // or actual success-ful login
            else {

               // manage cookies here 

               // put extractData in separate thread 
                final String responseStr = responseString;
                new Thread(new Runnable() {
                    public void run(){

                        extractData(responseStr);
                        selectData(defaultPrefs.getInt("xyz_display_section", 0));

                        // start the next activity
                        Intent intent = new Intent(MainActivity.this, PageViewActivity.class);
                        startActivity(intent);
                        finish();

                    }
                }).start();

            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            // called when response HTTP status is "4XX" (eg. 401, 403, 404)

            makeMyToast("Whoops, network error!");
        }

        @Override
        public void onFinish() {
            // done
        }

    });
}

您可以看到,在响应处理程序的回调的onSuccess,我可以测试一个字符串,看看是否登录失败,并在onFailure处回调,我给一个网络错误消息。

You can see that, in the response handler's onSuccess callback, I can test for a string, to see if the login failed, and, in the onFailure callback, I give a network error message.

我没有足够的经验来了解%的Web服务器的这种类型的登录后的作品上。

I'm not experienced enough to know what percent of web servers this type of post login works on.

该循环J客户端接收并管理的cookie。如果你将通过一个web视图要访问的网站页面,你需要从循环J复制客户端的cookie,到web视图。我鹅卵石code从一些网上的帖子,要做到这一点:

The loopj client receives and manages cookies. If you will be accessing pages from the site via a webview you need to copy cookies from the loopj client, over to the webview. I cobbled code from a few online posts, to do that:

// get cookies from the generic http session, and copy them to the webview
CookieSyncManager.createInstance(getApplicationContext());
CookieManager.getInstance().removeAllCookie();
CookieManager cookieManager = CookieManager.getInstance();

List<Cookie> cookies = xyzCookieStore.getCookies();
for (Cookie eachCookie : cookies) {
    String cookieString = eachCookie.getName() + "=" + eachCookie.getValue();
    cookieManager.setCookie("http://www.example.com", cookieString);
    //System.err.println(">>>>> " + "cookie: " + cookieString);
}
CookieSyncManager.getInstance().sync();
// holy crap, it worked; I am automatically logged in, in the webview

编辑:而且,我应该已经包括了类变量的定义和初始化:

And, I should have included the class variable definitions and initializations:

private AsyncHttpClient loopjClient = new AsyncHttpClient();
private PersistentCookieStore xyzCookieStore;

xyzCookieStore = new PersistentCookieStore(this);
loopjClient.setCookieStore(Utility.xyzCookieStore);

这篇关于解析网页视图的HTML内容 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆