I have a ListView and on click on an item, a new Activity is called by delivering am intent. All the activities have a single webview and load a different url or different parts in the same url (different div elements of the same url).

The problem I am facing is that only one of the Activity loads the url in the webview, the rest show a blank white webview. Can this be a problem with Jsoup??

Also note that when I run only the Jsoup code to fetch data and output to console, it works; but not when I use it within the activity.

Below is code of both the activities (they just differ in a single line in doc.select()). Activity 1 works perfectly fine and displays the html in the webview, while Activity 2 shows a blank screen.

Activity 1:

public class Article extends Activity {

ProgressDialog mProgressDialog;
WebView wv;

private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;

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

    new getHtml().execute();
}

private class getHtml extends AsyncTask<String, Void, Integer> {
    Elements tfa;

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Article.this);
        mProgressDialog.setTitle("Wikipedia");
        mProgressDialog.setMessage("Loading today's featured article...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {
            // Connect to the web site
            Document doc = Jsoup.connect(MainActivity.url).get();

            tfa = doc.select("div#mp-tfa");
            return SUCCESS;
        } catch (UnknownHostException e) {
            Log.e("Unknown Host Exception", "Network error", e);
            return NETWORK_ERROR;
        } catch (IOException e) {
            Log.e("IO Exception", "Failed to load HTML", e);
            return ERROR;
        } catch (Exception e) {
            Log.e("Exception", "An exception occured", e);
            return ERROR;
        }

    }

    @Override
    protected void onPostExecute(Integer result) {

        if (result == 2) {
            Toast.makeText(
                    getApplicationContext(),
                    "Network connection error. Check your internet connection and try again.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 3) {
            Toast.makeText(getApplicationContext(),
                    "Unknown error. Failed to load wikipedia.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 1) {
            wv.setWebViewClient(new WebViewClient());

            String tfa_html = tfa.outerHtml();
            tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
                    "a href=\"http://en.wikipedia.org/wiki/");
            tfa_html = tfa_html.replaceFirst(
                    "src=\"//upload.wikimedia.org/",
                    "src=\"http://upload.wikimedia.org/");
            wv.loadData(tfa_html, "text/html", null);
        }

        mProgressDialog.dismiss();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
        wv.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}

Activity 2:

public class Article extends Activity {

ProgressDialog mProgressDialog;
WebView wv;

private static final int SUCCESS = 1;
private static final int NETWORK_ERROR = 2;
private static final int ERROR = 3;

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

    new getHtml().execute();
}

private class getHtml extends AsyncTask<String, Void, Integer> {
    Elements tfa;

    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Article.this);
        mProgressDialog.setTitle("Wikipedia");
        mProgressDialog.setMessage("Loading today's featured article...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        try {
            // Connect to the web site
            Document doc = Jsoup.connect(MainActivity.url).get();

            tfa = doc.select("div#mp-tfp");
            return SUCCESS;
        } catch (UnknownHostException e) {
            Log.e("Unknown Host Exception", "Network error", e);
            return NETWORK_ERROR;
        } catch (IOException e) {
            Log.e("IO Exception", "Failed to load HTML", e);
            return ERROR;
        } catch (Exception e) {
            Log.e("Exception", "An exception occured", e);
            return ERROR;
        }

    }

    @Override
    protected void onPostExecute(Integer result) {

        if (result == 2) {
            Toast.makeText(
                    getApplicationContext(),
                    "Network connection error. Check your internet connection and try again.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 3) {
            Toast.makeText(getApplicationContext(),
                    "Unknown error. Failed to load wikipedia.",
                    Toast.LENGTH_LONG).show();
        } else if (result == 1) {
            wv.setWebViewClient(new WebViewClient());

            String tfa_html = tfa.outerHtml();
            tfa_html = tfa_html.replaceAll("a href=\"/wiki/",
                    "a href=\"http://en.wikipedia.org/wiki/");
            tfa_html = tfa_html.replaceFirst(
                    "src=\"//upload.wikimedia.org/",
                    "src=\"http://upload.wikimedia.org/");
            wv.loadData(tfa_html, "text/html", null);
        }

        mProgressDialog.dismiss();
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
        wv.goBack();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}
有帮助吗?

解决方案 2

Figured out the problem, the problem was the page stripped content off when loaded from a mobile browser/app. That was the reason of this problem. Just for information, the URL was http://en.wikipedia.org/wiki/Main_Page which redirected to the mobile version of wikipedia.

The solution I found out was either to fetch data from separate wiki pages instead of picking from the main page or use http://en.wikipedia.org/w/index.php?title=Main_Page as the URL, as this doesn't redirect to the mobile version.

其他提示

activity 1: ... tfa = doc.select("div#mp-tfa"); ...

activity 2: ... tfa = doc.select("div#mp-tfp"); ...

is differrent.

maybe the response of MainActivity.url is not the node of "div#mp-tfp". only exist the node of "div#mp-tfa".

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top