gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 04:42:22 25 4
gpt4 key购买 nike

我需要从 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+" ");
}
}

最佳答案

试试这个对我有用的

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

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);

关于android - 读取 HTML 加载的表单 webview Android 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5740428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com