无法在android webview中加载url

时间:2014-09-16 08:32:33

标签: android webview

我想在webview中加载一个网址“http://connect.mayoclinic.org/discussions”。我尝试了很多方法,但我无法加载它。显示空白页面。我正在使用以下内容。当我使用简单的加载URL删除其他功能,然后它给出空白页面。但是当我在webbrowser上测试它显示网页。它在iphone上的显示也......无法在android webview中显示

this.wb = (WebView) findViewById(R.id.wb);


WebSettings settings = wb.getSettings();
settings.setJavaScriptEnabled(true);

final AlertDialog alertDialog = new AlertDialog.Builder(BrowserViewHealthAlertActivity.this).create();

progressBar = ProgressDialog.show(BrowserViewHealthAlertActivity.this, "", "Loading...");
progressBar.setCancelable(true);
progressBar.setOnKeyListener(new DialogInterface.OnKeyListener() {

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        CommonFunction.startTimer(context);
        if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
            return true; // Pretend we processed it
        }
        if (keyCode == KeyEvent.KEYCODE_MENU && (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS) {
            return true;
        }
        return false; // Any other keys are still
    }
});

wb.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        CommonFunction.startTimer(context);
        view.loadUrl(url);
        return true;
    }

    public void onPageFinished(WebView view, String url) {

        if (progressBar.isShowing()) {
            progressBar.dismiss();
        }
    }

    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

        alertDialog.setTitle("Error");
        alertDialog.setMessage(description);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                return;
            }
        });
        alertDialog.show();
    }
});
wb.loadUrl(link);

2 个答案:

答案 0 :(得分:0)

一个简单的Webview在这里显示加载,直到URL加载为止。

Webview webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {

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

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        // Log.e("in", "onPageStarted" + isInternetPresent);
        if (isInternetPresent) {
            // Log.e("in", "if" + isInternetPresent);
            progressBar.setVisibility(View.VISIBLE);
            noconnection.setVisibility(View.INVISIBLE);
            retry.setVisibility(View.INVISIBLE);
        } else
            noconnection.setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
        // Log.e("in", "onloadresoure");
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        progressBar.setVisibility(View.INVISIBLE);
    }
});
WebSettings webSettings = webview.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
webview.getSettings().setDisplayZoomControls(false);
webSettings.setJavaScriptEnabled(true);
// webview.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webview.setInitialScale(1);

if (isInternetPresent) {
    isInternetPresent = true;
    webview.loadUrl("http://your url");
} else {
    ExitAlertClass.QuizSelectOptionAlert(ctx, "No Internet Connection"); //here goes your alert code
          noconnection.setVisibility(View.VISIBLE); 
    retry.setVisibility(View.VISIBLE);
};

progressBar是一个简单的进步:

ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);

在这里,我正在获得互联网广播接收器的状态。你可以制作那些代码。

答案 1 :(得分:0)

我已尝试过您的网址。它在浏览器中不用担心。 可能是URL本身@asiya的问题。 是不是还给黑页?

使用www.google.com等其他网址检查我的代码。它是我的工作代码。