简体   繁体   English

从Webview提取文本

[英]Extract text from a Webview

I basically want to extract text from inside a webpage that is already loaded into the Webview of the app. 我基本上想从已经加载到应用程序Webview的网页中提取文本。

Just as a trial, I built an app to count the occurrence of the Rupee symbol(₹) with the help of method from this post . 正如一个试验,我建立一个应用程序来算卢比符号(₹)与法的从帮助的发生这个职位

See the screenshot: App's Screenshot 查看屏幕快照: App的屏幕截图

But I am not able to make it work. 但是我无法使其工作。 The TextView should show the number of '₹' symbols on the webpage but it stays unchanged. TextView应该在网页上显示“₹”符号的数量,但保持不变。

I am a noob at Android and would highly appritiate any help :) 我是Android的菜鸟,非常感谢您的帮助:)

This is my code: (MainActivity) 这是我的代码:(MainActivity)

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    static TextView count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        count = (TextView) findViewById(R.id.textView);

    /* An instance of this class will be registered as a JavaScript interface */
        class MyJavaScriptInterface {
            @JavascriptInterface
            @SuppressWarnings("unused")
            public void processHTML(String html) {
                // process the html as needed by the app

                int occ = 0;

                for(int i = 0 ; i < html.length() ; i++)
                    if(html.charAt(i) == '₹')
                        occ++;

                MainActivity.count.setText(occ);
            }
        }

        final WebView browser = (WebView) findViewById(R.id.browser);

        /* JavaScript must be enabled if you want it to work, obviously */
        browser.getSettings().setJavaScriptEnabled(true);

        /* Register a new JavaScript interface called HTMLOUT */
        browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");

        /* WebViewClient must be set BEFORE calling loadUrl! */
        browser.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {

                /* This call inject JavaScript into the page which just finished loading. */
                browser.loadUrl("javascript:HTMLOUT.processHTML(document.documentElement.outerHTML);");

            }
        });

        /* load a web page */
        browser.loadUrl("https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=buy+chocolate");
    }
}

Got it to work. 得到它的工作。 Two small problems: 两个小问题:

  1. You're making a UI call in the function called by JavaScript. 您正在用JavaScript调用的函数进行UI调用。 That's not allowed. 那是不允许的。 Replace MainActivity.count.setText(occ); 替换MainActivity.count.setText(occ); with

     runOnUiThread(new Runnable() { @Override public void run() { count.setText(String.valueOf(occ)); } }); 
  2. This code already fixes the 2nd issue: calling setText(int) expects a resource ID, thus you need to convert to a String first. 此代码已经解决了第二个问题:调用setText(int)需要一个资源ID,因此您需要先转换为String。

(You need to also remove static from your declaration of count ) (您还需要从count声明中删除static

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM