Webview没有从网站获取具体内容

时间:2013-07-03 01:02:09

标签: android

我遇到了一个无法从网站获取特定内容的问题。

这是网站:

http://www.google.com/finance/company_news?q=CURRENCY%3AUSD&ei=hYbQUcC3LJS80QHnDA

以下是我的代码的一部分:

public class WebClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        StringBuilder builder = new StringBuilder("");
        builder.append("javascript:document.getElementById('gb')    .style.visibility= 'hidden';");
        builder.append("javascript:document.getElementById('gb')    .style.display   = 'none'  ;");
        builder.append("javascript:document.getElementById('appbar').style.visibility= 'hidden';");
        builder.append("javascript:document.getElementById('appbar').style.display   = 'none'  ;");
        builder.append("javascript:document.getElementById('gf-nav').style.visibility= 'hidden';");
        builder.append("javascript:document.getElementById('gf-nav').style.display   = 'none'  ;");
        view.loadUrl(url);
    }
    }

private void AddListenerOnButton() {
    spinner = (Spinner) getView().findViewById(R.id.spinner);
    final WebView webView = (WebView) getView().findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setRenderPriority(RenderPriority.HIGH);
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            String text1 = spinner.getSelectedItem().toString().trim();
            WebClient wc = new WebClient();
            if (text1.equals("US Dollar - USD")) {
                wc.onPageFinished(webView, "http://www.google.com/finance/company_news?q=CURRENCY%3AUSD&ei=hYbQUcC3LJS80QHnDA");
                wc.shouldOverrideUrlLoading(webView, "http://www.google.com/finance/company_news?q=CURRENCY%3AUSD&ei=hYbQUcC3LJS80QHnDA");
            }               
        }

webview不是获取特定内容,而是返回其所有内容。这样:

@Override
    public void onPageFinished(WebView view, String url) {
        StringBuilder builder = new StringBuilder("");
        builder.append("javascript:document.getElementById('gb')    .style.visibility= 'hidden';");
        builder.append("javascript:document.getElementById('gb')    .style.display   = 'none'  ;");
        builder.append("javascript:document.getElementById('appbar').style.visibility= 'hidden';");
        builder.append("javascript:document.getElementById('appbar').style.display   = 'none'  ;");
        builder.append("javascript:document.getElementById('gf-nav').style.visibility= 'hidden';");
        builder.append("javascript:document.getElementById('gf-nav').style.display   = 'none'  ;");
        view.loadUrl(url);
    }
    }

应该只是从谷歌获取信息,但它没有奏效。这是我想要的网站的一部分:

http://oi44.tinypic.com/2ilnhwi.jpg

这里发生了什么?我该怎么办才能修复它?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

两个问题

  1. 从未使用过StringBuilder结果
  2. “javascript:”前缀schould只能在loadUrl()调用中使用一次。

  3. public void onPageFinished(WebView view, String url) {
        StringBuilder builder = new StringBuilder("");
        builder.append("javascript:document.getElementById('gb')    .style.visibility= 'hidden';");
        builder.append("document.getElementById('gb')    .style.display   = 'none'  ;");
        builder.append("document.getElementById('appbar').style.visibility= 'hidden';");
        builder.append("document.getElementById('appbar').style.display   = 'none'  ;");
        builder.append("document.getElementById('gf-nav').style.visibility= 'hidden';");
        builder.append("document.getElementById('gf-nav').style.display   = 'none'  ;");
        view.loadUrl(builder.toString());
    }