读取 HTML 加载表单 webview Android 中的元素

发布于 2024-11-02 08:39:54 字数 2102 浏览 0 评论 0原文

我需要从 HTML 文件中读取元素。我从 webview 加载它们,但我还没有找到最好的阅读方式。

代码:content_html

package com.android.webview;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ViewLink extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    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:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");

            /*
            read elements in content_html
            */
        }

    });


    browser.loadUrl("http://vnexpress.net/gl/rss/");
}

final Context myApp = this;
Builder builder;

String content_html;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
    @SuppressWarnings("unused")
    public void showHTML(String html)
    {
       builder = new AlertDialog.Builder(myApp);
            builder.setTitle("HTML")
            .setMessage(html)
            .setPositiveButton(android.R.string.ok, null)
        .setCancelable(false)
        .create()
        .show();
            content_html = html;
            Log.i("html", html+" ");
    }
}

I need to read elements from an HTML file. I loaded them from webview but I haven't found the best way to read.

Code: content_html

package com.android.webview;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ViewLink extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    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:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");

            /*
            read elements in content_html
            */
        }

    });


    browser.loadUrl("http://vnexpress.net/gl/rss/");
}

final Context myApp = this;
Builder builder;

String content_html;

/* An instance of this class will be registered as a JavaScript interface */
class MyJavaScriptInterface
{
    @SuppressWarnings("unused")
    public void showHTML(String html)
    {
       builder = new AlertDialog.Builder(myApp);
            builder.setTitle("HTML")
            .setMessage(html)
            .setPositiveButton(android.R.string.ok, null)
        .setCancelable(false)
        .create()
        .show();
            content_html = html;
            Log.i("html", html+" ");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落在眉间の轻吻 2024-11-09 08:39:54

尝试一下这个对我有用的

class MyJavaScriptInterface  
        {  
            @SuppressWarnings("unused")  
            public void showHTML(String html)  
            {  
                System.out.println("Html...."+html);
            }
        } 

活动

        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setPluginsEnabled(true);
        browser.getSettings().setSupportZoom(true);
        browser.getSettings().setBuiltInZoomControls(true);
        browser.setBackgroundColor(Color.TRANSPARENT);
        browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");  
        browser.setWebViewClient(new WebViewClient() {  
        @Override  
        public void onPageFinished(WebView view, String url)  
        {  
            browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");  
        }  
        });  
        browser.loadUrl(link);  

Try this one that work for me

class MyJavaScriptInterface  
        {  
            @SuppressWarnings("unused")  
            public void showHTML(String html)  
            {  
                System.out.println("Html...."+html);
            }
        } 

In Activity

        browser.getSettings().setJavaScriptEnabled(true);
        browser.getSettings().setPluginsEnabled(true);
        browser.getSettings().setSupportZoom(true);
        browser.getSettings().setBuiltInZoomControls(true);
        browser.setBackgroundColor(Color.TRANSPARENT);
        browser.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");  
        browser.setWebViewClient(new WebViewClient() {  
        @Override  
        public void onPageFinished(WebView view, String url)  
        {  
            browser.loadUrl("javascript:window.HTMLOUT.showHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");  
        }  
        });  
        browser.loadUrl(link);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文