Vid Man
Vid Man

Reputation: 67

Android WebView Jsoup

I'm working on an app in which I would like to display my website but with less div elements.

In this example code, I used the website of stackoverflow.com and have tried to hide the div class hmenus (buttons at the top of the main website).

After running the code, I get a blank / white webview. What I have tried is to make the String html final and call it afterwards to set my final url for the webview.

Code:

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File;

public class MyCustomAsyncTask extends AsyncTask<File, Void, String> {
@Override
protected String doInBackground(File... params) {

    Document doc = Jsoup.connect("http://stackoverflow.com/").get();
    Elements ele = doc.select(":not(#hmenus)");        // ->>>>> Jsoup html
    final String html = ele.toString();

}}

public class MainActivity extends Activity {


    private WebView WebView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView = (WebView) findViewById(webView);
        WebView.setWebChromeClient(new WebChromeClient());
        WebView.getSettings().setJavaScriptEnabled(true);
        WebView.getSettings().setDomStorageEnabled(true);

        final String mime = "text/html";
        final String encoding = "utf-8";
        WebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {

                // ->>>>>>   Change the website --->>> insert call html value
                WebView.loadData(html, mime, encoding);
            }
        });
    }
}

How can I call the value of html final String html = ele.toString(); and cast it to WebView.loadData(html, mime, encoding);?

Upvotes: 2

Views: 3313

Answers (1)

TDG
TDG

Reputation: 6151

You are not using the AsyncTask the right way. You should read this.

Declare the AsyncTask and implement the doInBackground for getting the web page and onPostExecute for displaying the result when it is ready:

private class GetData extends AsyncTask<Void, Void, String> {

// This is run in a background thread
    @Override
    protected String doInBackground(Void... params) {
        try {
            Document doc = Jsoup.connect("https://stackoverflow.com/")
                    .userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0")
                    .get();
            Elements ele = doc.select("#hmenus");
            html = ele.toString();
            return html;
        } catch (Exception e) {
            Log.d("APP", e.toString());
        }            
        return "error";
    }

    // This runs in UI when background thread finishes
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        Log.d("APP", "1");
        WebView.loadData(result, mime, encoding);            
    }
}

As for the MainActivity, it must invoke the AsyuncTask:

public class MainActivity extends Activity {

    private WebView WebView;
    String html = "Loading...";
    final String mime = "text/html";
    final String encoding = "utf-8";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView = (WebView) findViewById(R.id.webview);
        WebView.setWebChromeClient(new WebChromeClient());
        WebView.getSettings().setJavaScriptEnabled(true);
        WebView.getSettings().setDomStorageEnabled(true);
        //Display "Loading..." message while waiting
        WebView.loadData(html, mime, encoding);
        //Invoke the AsyncTask
        new GetData().execute();
    }
}

Don't forget the internet-permission at your manifest.

Upvotes: 1

Related Questions