如何从一个web视图的html内容?视图、内容、web、html

2023-09-11 10:55:50 作者:饮尽风生

这是从一个web视图获取HTML code最简单的方法是什么? 我试图从计算器和谷歌的几种方法,但无法找到一个确切的方法。请注明确切的方法。

 公共类htmlde codeR延伸活动实现OnClickListener,TextWatcher
{
TextView的txturl;
按钮btgo;
的WebView wvbrowser;
TextView的TXT code;
BT的ImageButton code;
的LinearLayout llayout;
INT flagbt code;
公共无效的onCreate(包savedInstanceState)
{
            super.onCreate(savedInstanceState);
                的setContentView(R.layout.htmlde codeR);

    txturl =(TextView中)findViewById(R.id.txturl);

    btgo =(按钮)findViewById(R.id.btgo);
    btgo.setOnClickListener(本);

    wvbrowser =(web视图)findViewById(R.id.wvbrowser);
    wvbrowser.setWebViewClient(新HelloWebViewClient());
    wvbrowser.getSettings()setJavaScriptEnabled(真)。
    wvbrowser.getSettings()setPluginsEnabled(真)。
    。wvbrowser.getSettings()setJavaScriptCanOpenWindowsAutomatically(真正的);
    wvbrowser.addJavascriptInterface(新MyJavaScriptInterface(),HTMLOUT);
    //wvbrowser.loadUrl("http://www.google.com);
    wvbrowser.loadUrl("javascript:window.HTMLOUT.showHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");


    TXT code =(TextView中)findViewById(R.id.txt code);
    TXT code.addTextChangedListener(本);

    BT code =(的ImageButton)findViewById(R.id.bt code);
    BT code.setOnClickListener(本);

    }

公共无效的onClick(视图v)
{
    如果(btgo == V)
    {
        。字符串URL = txturl.getText()的toString();
        如果(!。txturl.getText()的toString()包含(HTTP://))
        {
            URL =HTTP://+网址;
        }
        wvbrowser.loadUrl(URL);
        //wvbrowser.loadData("<html><head></head><body><div风格=宽度:100像素,高度:100像素;边框:1px的红色固体;'&GT;&LT; / DIV&GT;&LT; /身体GT;&LT; / HTML&gt;中,text / html的,UTF-8);
    }
    否则,如果(BT code == V)
    {
        ViewGroup.LayoutParams params1 = wvbrowser.getLayoutParams();
        ViewGroup.LayoutParams params2 = TXT code.getLayoutParams();
        如果(flagbt code == 1)
        {
            params1.height = 200;
            params2.height = 220;
            flagbt code = 0;
            //txt$c$c.setText(wvbrowser.getContentDescription());
        }
        其他
        {
            params1.height = 420;
            params2.height = 0;
            flagbt code = 1;
        }
        wvbrowser.setLayoutParams(params1);
        TXT code.setLayoutParams(params2);

    }
}

公共类HelloWebViewClient扩展WebViewClient {
    @覆盖
    公共布尔shouldOverrideUrlLoading(web视图查看,字符串URL){

        view.loadUrl(URL);
        返回true;
    }
    / * @覆盖
    公共无效onPageFinished(web视图查看,字符串URL)
    {
        //此调用JavaScript插入刚刚完成加载页面。
        wvbrowser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
    } * /

}
类MyJavaScriptInterface
{
    @燮pressWarnings(未使用)
    公共无效showHTML(字符串HTML)
    {

        TXT code.setText(HTML);
    }
}

公共无效afterTextChanged(编辑S){
    // TODO自动生成方法存根

}

公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
        之后INT){
    // TODO自动生成方法存根

}

公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
    wvbrowser.loadData("<html><div"+txt$c$c.getText().toString()+"></div></html>","text/html","utf-8");

}

}
 

解决方案

其实这个问题有很多答案。这里有2人:

这首几乎是一样的你,我想我们从同一个教程得到它。

 公共类TestActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.webview);
        最后的WebView的WebView =(web视图)findViewById(R.id.browser);
        webview.getSettings()setJavaScriptEnabled(真)。
        webview.addJavascriptInterface(新MyJavaScriptInterface(本),HtmlViewer);

        webview.setWebViewClient(新WebViewClient(){
            @覆盖
            公共无效onPageFinished(web视图查看,字符串URL){
                webview.loadUrl(JavaScript的:window.HtmlViewer.showHTML+
                        ('&LT; HTML&GT;+ document.getElementsByTagName('HTML')[0] .innerHTML +'&LT; / HTML&GT;'););
            }
        });

        webview.loadUrl(http://android-in-action.com/index.php?post/+
                共同的错误 - 和 - 错误 - 和 - 如何到解决,避免他们);
    }

    类MyJavaScriptInterface {

        私人上下文CTX;

        MyJavaScriptInterface(上下文CTX){
            this.ctx = CTX;
        }

        公共无效showHTML(字符串HTML){
            新AlertDialog.Builder(CTX).setTitle(HTML)。setMessage(HTML)
                    .setPositiveButton(android.R.string.ok,空).setCancelable(假).create()显示()。
        }

    }
}
 

这样,你的抓取通过JavaScript的HTML。不是prettiest的方式,但是当你有你的JavaScript界面​​,你可以添加其他的方法来修补它。

的另一种方法是使用HttpClient的像there. cisco 路由器web界面没视图

您还选择的选项取决于,我想,在你打算做与检索到的HTML ...

Which is the simplest method to get html code from a webview? I have tried several methods from stackoverflow and google, but can't find an exact method. Please mention an exact way.

public class htmldecoder extends Activity implements OnClickListener,TextWatcher
{
TextView txturl;
Button btgo;
WebView wvbrowser;
TextView txtcode;
ImageButton btcode;
LinearLayout llayout;
int flagbtcode;
public void onCreate(Bundle savedInstanceState)
{
            super.onCreate(savedInstanceState);
                setContentView(R.layout.htmldecoder);

    txturl=(TextView)findViewById(R.id.txturl);

    btgo=(Button)findViewById(R.id.btgo);
    btgo.setOnClickListener(this);

    wvbrowser=(WebView)findViewById(R.id.wvbrowser);
    wvbrowser.setWebViewClient(new HelloWebViewClient());
    wvbrowser.getSettings().setJavaScriptEnabled(true);
    wvbrowser.getSettings().setPluginsEnabled(true);
    wvbrowser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wvbrowser.addJavascriptInterface(new MyJavaScriptInterface(),"HTMLOUT");
    //wvbrowser.loadUrl("http://www.google.com");
    wvbrowser.loadUrl("javascript:window.HTMLOUT.showHTML('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");


    txtcode=(TextView)findViewById(R.id.txtcode);
    txtcode.addTextChangedListener(this);

    btcode=(ImageButton)findViewById(R.id.btcode);
    btcode.setOnClickListener(this);

    }

public void onClick(View v)
{
    if(btgo==v)
    {
        String url=txturl.getText().toString();
        if(!txturl.getText().toString().contains("http://"))
        {
            url="http://"+url;
        }
        wvbrowser.loadUrl(url);
        //wvbrowser.loadData("<html><head></head><body><div style='width:100px;height:100px;border:1px red solid;'></div></body></html>","text/html","utf-8");
    }
    else if(btcode==v)
    {
        ViewGroup.LayoutParams params1=wvbrowser.getLayoutParams();
        ViewGroup.LayoutParams params2=txtcode.getLayoutParams();
        if(flagbtcode==1)
        {
            params1.height=200;
            params2.height=220;
            flagbtcode=0;
            //txtcode.setText(wvbrowser.getContentDescription());
        }
        else
        {
            params1.height=420;
            params2.height=0;
            flagbtcode=1;
        }
        wvbrowser.setLayoutParams(params1);
        txtcode.setLayoutParams(params2);

    }
}

public class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);
        return true;
    }
    /*@Override
    public void onPageFinished(WebView view, String url)
    {
        // This call inject JavaScript into the page which just finished loading. 
        wvbrowser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
    }*/

}
class MyJavaScriptInterface
{
    @SuppressWarnings("unused")
    public void showHTML(String html)
    {

        txtcode.setText(html);
    }
}

public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

}

public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

public void onTextChanged(CharSequence s, int start, int before, int count) {
    wvbrowser.loadData("<html><div"+txtcode.getText().toString()+"></div></html>","text/html","utf-8");

}

}

解决方案

Actually this question has many answers. Here are 2 of them :

This first is almost the same as yours, I guess we got it from the same tutorial.

public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        final WebView webview = (WebView) findViewById(R.id.browser);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.addJavascriptInterface(new MyJavaScriptInterface(this), "HtmlViewer");

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                webview.loadUrl("javascript:window.HtmlViewer.showHTML" +
                        "('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
            }
        });

        webview.loadUrl("http://android-in-action.com/index.php?post/" +
                "Common-errors-and-bugs-and-how-to-solve-avoid-them");
    }

    class MyJavaScriptInterface {

        private Context ctx;

        MyJavaScriptInterface(Context ctx) {
            this.ctx = ctx;
        }

        public void showHTML(String html) {
            new AlertDialog.Builder(ctx).setTitle("HTML").setMessage(html)
                    .setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
        }

    }
}

This way your grab the html through javascript. Not the prettiest way but when you have your javascript interface, you can add other methods to tinker it.

An other way is using an HttpClient like there.

The option you choose also depends, I think, on what you intend to do with the retrieved html...

 
精彩推荐