在StringBuilder(WebView)中放置URL的位置

时间:2013-07-04 00:43:39

标签: android

我遇到的问题是我无法将URL放在stringbuilder中。我在这里要完成的是获取页面的特定部分。这是我试图获取内容的网页:

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

以下是我想在WebView中显示的网站部分:

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

这是我的代码:

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("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());
    }
    }

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().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")) {
                //webView.loadUrl("http://www.google.com/finance/company_news?q=CURRENCY%3AUSD&ei=hYbQUcC3LJS80QHnDA");
                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");
            }
}

我已尝试过所有内容,但整个网页仍然显示。我究竟做错了什么?这是其他错误吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我当前的代码包含在一个完整的function()包装器中,工作正常,也许你可以尝试一下:

builder.append("javascript:(function() {");
builder.append("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'  ;");
builder.append("})()");

编辑:忘记(function(){})

末尾的几个括号