此页面有太多服务器重定向Android

时间:2014-01-18 17:53:39

标签: java android android-webview

我正在尝试创建一个链接到我朋友博客的Android应用程序。它链接到主页很好,但当我按下页面上的链接时,我收到错误“此页面有太多的服务器重定向”。有什么想法吗?

public class WebViewHelper {

    private ProgressDialog mProgressDialog;

    //make it final so it will be accessible to setWebViewClient
    public WebView webview(final Context mContext) {

        // progress dialog
        mProgressDialog = new ProgressDialog(mContext);

        // new webview
        WebView web = new WebView(mContext);

        // web settings
        WebSettings webSettings = web.getSettings();

        // false
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setAppCacheEnabled(false);

        // true
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setUseWideViewPort(true);



        // other
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setRenderPriority(RenderPriority.HIGH);

        web.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                //show the user progress percentage
                mProgressDialog.setMessage("Loading... " + progress + "%");
            }
        });

        web.setWebViewClient(new WebViewClient() {

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

                //if there's an error loading the page, make a toast
                Toast.makeText(mContext, description + ".", Toast.LENGTH_SHORT)
                        .show();

            }

            public void onPageFinished(WebView view, String url) {
                //after loading page, remove loading page
                mProgressDialog.dismiss();
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);

                //on page started, show loading page
                mProgressDialog.setCancelable(false);
                mProgressDialog.setMessage("Loading...");
                mProgressDialog.show();

            }

        });

        // return the web view
        return web;
    }
}

1 个答案:

答案 0 :(得分:2)

您确定要查看的网页没有问题吗?使用本机浏览器可以正常加载吗?

此外,为了强制webview处理在webview上单击的任何链接的加载,您需要覆盖WebViewClient中的shouldOverrideUrlLoading()方法。您的代码中缺少这个。

PS:我想将此作为评论发布,但是因为我没有足够的声誉而将其作为答案发布。