带有http loadUrl的Android WebView显示空白/空白页面

时间:2015-05-22 08:00:11

标签: android webview

我正在开发Android应用程序。我想用WebView显示网站。但我不这样做。空白/空白页面正在打开。其他网站显示,但为什么这不显示? 我的代码如下 请帮我, 感谢。

public class MainActivity extends ActionBarActivity{
    private WebView ourWebSite;
    private ProgressDialog pd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initViews();
        setProgressDialog();
    }

    private void initViews(){
        ourWebSite = (WebView) findViewById(R.id.ada_web_site);
        ourWebSite.getSettings().setJavaScriptEnabled(true);
        ourWebSite.setWebViewClient(new WebSiteWebViewClient());
        ourWebSite.loadUrl("http://fahrikayahantaksi.com/");
    }

    private void setProgressDialog(){
        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage(getResources().getString(R.string.loading));
        pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pd.show();
    }

    private class WebSiteWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            if (!pd.isShowing()) {
                pd.show();
            }

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            System.out.println("on finish");
            if (pd.isShowing()) {
                pd.dismiss();
            }

        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed(); // Ignore SSL certificate errors
        }
    }
}

2 个答案:

答案 0 :(得分:2)

我认为在你的代码中你错过了这个suppressLint

  

@SuppressLint(" SetJavaScriptEnabled&#34)

尝试像这样更改你的代码..通过这种方式,我可以加载你的[页面]

private WebView mWebview=null ;

 @SuppressLint("SetJavaScriptEnabled")
   @Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    mWebview  = new WebView(this);

    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

    final Activity activity = this;

    mWebview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });



    mWebview.loadUrl("http://fahrikayahantaksi.com/");
    setContentView(mWebview );

}

别忘了在你的清单中添加互联网许可!!

uses-permission android:name="android.permission.INTERNET" />

答案 1 :(得分:0)

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_webview);

    String url = getIntent().getStringExtra("url");
    webView = new WebView(this);
    webView.getSettings().setJavaScriptEnabled(true);
    final Activity activity = this;
    webView.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });
    webView.loadUrl("http://docs.google.com/viewer?embedded=true&url=" + url);
    setContentView(webView );
}

我使用谷歌文档打开pdf。