我遇到了无法将 URL 放入字符串生成器的问题。我在这里想要完成的只是获取页面的特定部分。这是我试图从中获取内容的网页:
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");
}
}
我已经尝试了一切,但整个网页仍在显示。我究竟做错了什么?是不是还有什么问题?任何帮助将不胜感激。