Android WebView无法在一个活动中加载URL,但在另一个活动中加载

时间:2014-05-15 10:30:15

标签: android webview

我有一个ListView,点击一个项目,通过提供我的意图调用一个新的Activity。所有活动都有一个webview,并在同一个url中加载不同的url或不同的部分(同一个url的不同div元素)。

我面临的问题是,只有一个Activity在webview中加载了url,其余的显示了一个空白的webview。这可能是Jsoup的问题吗?

另请注意,当我只运行Jsoup代码来获取数据并输出到控制台时,它可以工作;但是当我在活动中使用它时不会。

下面是两个活动的代码(它们只是doc.select()中的一行不同)。活动1完美地运行并在webview中显示html,而活动2显示空白屏幕。

活动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);
}

}

活动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 个答案:

答案 0 :(得分:0)

活动1: ...  tfa = doc.select(“div#mp-tfa”); ...

活动2: ... tfa = doc.select(“div#mp-tfp”); ...

是不同的。

也许MainActivity.url的响应不是“div#mp-tfp”的节点。只存在“div#mp-tfa”的节点。

答案 1 :(得分:0)

找出问题,问题是从移动浏览器/应用程序加载页面剥离的内容。这就是这个问题的原因。仅供参考,网址为http://en.wikipedia.org/wiki/Main_Page,重定向到维基百科的移动版本。

我发现的解决方案是从单独的Wiki页面获取数据,而不是从主页面中选择或使用http://en.wikipedia.org/w/index.php?title=Main_Page作为URL,因为这不会重定向到移动版本。