美文网首页
Android客户端修改网页

Android客户端修改网页

作者: 2b75747cf703 | 来源:发表于2017-04-18 23:18 被阅读71次

https://jsoup.org/

activity_jsoup_html.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

JSoupHtmlActivity.java

package com.androidstudio.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class JSoupHtmlActivity extends Activity {

    private static final String DEFAULT_URL = "http://www.xxx.com/xxx.html";

    private WebView webView;

    private String currentUrl;
    private String historyUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jsoup_html);
        webView = (WebView) findViewById(R.id.webView);

        //webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                loadUrl(url);
                return true;
            }
        });

        currentUrl = DEFAULT_URL;
        loadUrl(DEFAULT_URL);
    }

    public void onBackPressed()
    {
        if(webView.canGoBack())
        {
            webView.goBack();
            return;
        }

        super.onBackPressed();
    }

    public void loadUrl(final String url)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Document document = null;
                try {
                    document = Jsoup.connect(url).get();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if(document == null)
                {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(JSoupHtmlActivity.this, "网络异常,请稍后再试", Toast.LENGTH_LONG).show();
                        }
                    });
                    return;
                }

                Elements elements = document.getElementsByClass("nav_bar");

                if(elements.size() == 1)
                    elements.get(0).remove();

                if(url.contains("/arttypehtml/"))
                {
                    while (true)
                    {
                        Element top_box = document.getElementById("top_box");
                        if(top_box == null)
                            break;

                        top_box.remove();
                    }
                }
                else
                {
                    Element top_box = document.getElementById("top_box");
                    if(top_box != null)
                        top_box.remove();

                    top_box = document.getElementById("top_box");
                    for(int i=0;i<top_box.children().size();i++)
                    {
                        Element child = top_box.child(i);
                        String classAttr = child.attr("class");
                        String typeAttr = child.attr("type");
                        if(!classAttr.equals("wrap mt20") && !typeAttr.equals("text/javascript"))
                            child.remove();
                    }
                }

                Element footer_box = document.getElementById("footer_box");

                if(footer_box != null && footer_box.nextElementSibling() != null)
                    footer_box.nextElementSibling().remove();

                final Document finalDocument = document;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        historyUrl = currentUrl;
                        currentUrl = url;

                        webView.loadDataWithBaseURL(url, finalDocument.toString(), "text/html", "utf-8", historyUrl);
                    }
                });
            }
        }).start();
    }
}

相关文章

网友评论

      本文标题:Android客户端修改网页

      本文链接:https://www.haomeiwen.com/subject/qjzqzttx.html