Android的Java的 - 获取来自网页(报警系统)纯文本 [英] Android Java - Get plain text from webpage (alert system)

查看:160
本文介绍了Android的Java的 - 获取来自网页(报警系统)纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事情应该是很简单的是什么,但是我没有什么没有的Java / Android体验。

What I want to do should be very simple, however I have little to none Java/Android experience.

我在Eclipse中的WebView设置。我想网页去 http://example.com/mypage.php 。在该页面中会可以是文本OK或提示。

I have a WebView setup in Eclipse. I want the webpage to go to http://example.com/mypage.php. On that page will be either the text 'OK' or 'ALERT'.

我需要知道如何检查页的那些话。这将是所有的页面上。

I need to know how to check the page for those words. That will be all that's on the page.

任何帮助吗?谢谢!

推荐答案

有没有简单的方法做你想要什么,但它可以做到的。首先,定义是这样的东西的接口:

There's no straightforward way to do what you want, though it can be done. First, define an interface that goes something like:

public interface WebInterfaceDelegate {
    public void receivedHtml(String html);
}

然后,定义要暴露给JavaScript运行在你的WebView类,沿着线的东西:

Then, define a class that you want to expose to the JavaScript runtime in your WebView, something along the lines of:

public static class WebViewJavaScriptInterface {
    private WebInterfaceDelegate delegate;

    public WebViewJavaScriptInterface(WebInterfaceDelegate delegate) {
        this.delegate = delegate;
    }

    public void htmlLoaded(String html) {
        if (delegate != null) {
            delegate.receivedHtml(html);
        }
    }
}

然后,让你的活动实现你上面定义,就像接口:

Then, make your Activity implement the interface you defined above, like:

public class AndroidWebViewActivity extends Activity implements WebInterfaceDelegate {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //...
        WebView webView = (WebView)findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new WebViewJavaScriptInterface(this), "AndroidAPI");

        webView.loadUrl("http://your.url.com");
    }

    //...

    public void receivedHtml(String html) {
        //inspect the HTML here
        System.out.println("Got html:  " + html);
    }
}

最后,添加一些JavaScript到你的页面是这样:

Finally, add some JavaScript to your page that goes like:

<script>
    window.onload = function() {
        AndroidAPI.htmlLoaded(document.body.innerHTML);
    }
</script>

所以,你可以做的一切,或者你可以使用像 URL.openStream()来直接连接到目标网页并阅读标记过的插座而不是的WebView 的关闭。要做到这一点很简单,只要:

So you can do all that, or you can just use something like URL.openStream() to connect directly to your target webpage and read the markup off of the socket instead of off of the WebView. To do this is as simple as:

InputStream response = new URL("http://example.com/mypage.php").openStream();
//read the data from the response into a buffer, then inspect the buffer

您可以在这里找到一些额外的信息:

You can find some additional information here:

http://docs.oracle.com/ JavaSE的/ 6 /文档/ API / JAVA / NET / URL.html

这篇关于Android的Java的 - 获取来自网页(报警系统)纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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