【问题标题】:Android WebView unable to load URL in one activity but loads in anotherAndroid WebView 无法在一个活动中加载 URL 但在另一个活动中加载
【发布时间】:2014-05-15 10:30:15
【问题描述】:

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

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

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

下面是这两个活动的代码(它们只是在doc.select() 中的一行不同)。 Activity 1 运行良好,在 webview 中显示 html,而 Activity 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);
}

}

【问题讨论】:

    标签: android webview


    【解决方案1】:

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

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

    不同。

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

    【讨论】:

    • 我曾提到,当我在单独的 java 程序中运行它时,jsoup 会将该 div 的输出打印到控制台。该节点存在。
    • Log.i("文章数据提取", tfa_html.substring(0, 30));
    • tfa_html 的 len ("div#mp-tfp" 的节点) >= 30 ?
    • 也许 tfa_html 存在规范字符。例如 '#' '%' ... 将方法 loadData 更改为 other。例如 loadbasexxx
    • 我刚刚添加的支票;如果它
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      相关资源
      最近更新 更多