【问题标题】:WebView No 'Access-Control-Allow-Origin' header is present [ANDROID]WebView 不存在“Access-Control-Allow-Origin”标头 [ANDROID]
【发布时间】:2016-11-21 06:06:27
【问题描述】:

下面是我的 webView 设置:

webView = (WebView) findViewById(R.id.webview);

//      webView.clearHistory();
//      webView.getSettings().setLoadWithOverviewMode(true);
//      webView.getSettings().setUseWideViewPort(true);
//      webView.getSettings().setBuiltInZoomControls(true);
//      webView.getSettings().setDomStorageEnabled(true);
//      if (android.os.Build.VERSION.SDK_INT >= 16)
//      {
//          webView.enablecrossdomain41();
//
//          webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
//          webView.getSettings().setAllowFileAccessFromFileURLs(true);
//
//      }
//      else
//      {
//          webView.enablecrossdomain();
//      }


    if (Build.VERSION.SDK_INT >= 16) {
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        webView.getSettings().setAllowFileAccessFromFileURLs(true);
    }else{
        Class<?> clazz = webView.getSettings().getClass();
        Method method = null;
        try {
            method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);
            if (method != null) {
                method.invoke(webView.getSettings(), true);
            }
            method = clazz.getMethod("setAllowFileAccessFromFileURLs", boolean.class);
            if (method != null) {
                method.invoke(webView.getSettings(), true);
            }
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }

    }
    webView.setWebViewClient(new MyWebViewClient());
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setJavaScriptEnabled(true);

//      if (Build.VERSION.SDK_INT >= 11) {
//          webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
//  

}

    webView.loadUrl("http://jobs.bdjobs.com/jobdetails.asp?id=675496&fcatId=-1&ln=1");

下面是我的MyWebViewClient 代码:

private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            progress.setVisibility(View.GONE);
            tv.setVisibility(View.GONE);
            WebActActivity.this.progress.setProgress(100);
            webView.setVisibility(View.VISIBLE);
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            progress.setVisibility(View.VISIBLE);
            tv.setVisibility(View.VISIBLE);
            webView.setVisibility(View.GONE);
            WebActActivity.this.progress.setProgress(0);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            MyLog.e("error1", error.toString());
            handler.proceed();
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            MyLog.e("error2", error.toString());
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            MyLog.e("error3", description);
            MyLog.e("error3", failingUrl);
        }

        @Override
        public void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg) {
            MyLog.e("error4", cancelMsg.toString());
            MyLog.e("error4", continueMsg.toString());
        }
    }

但是 webview 上没有显示任何内容。我检查了我尝试加载的链接在浏览器上运行良好。

控制台说:

I/ActivityManager: Displayed com.pipilika.jobsearch/.activity.WebActActivity: +233ms
W/cr_BindingManager: Cannot call determinedVisibility() - never saw a connection for the pid: 25726
I/chromium: [INFO:CONSOLE(0)] "Access to Font at 'http://www.bdjobs.com/errormessages/404.htm' from origin 'http://www.bdjobs.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://jobs.bdjobs.com' is therefore not allowed access.", source: http://jobs.bdjobs.com/jobdetails.asp?id=675496&fcatId=-1&ln=1 (0)
E/chromium: [ERROR:interface_registry.cc(99)] Failed to locate a binder for interface: autofill::mojom::AutofillDriver
V/FA: Inactivity, disconnecting from the service
I/chromium: [INFO:CONSOLE(0)] "XMLHttpRequest cannot load https://fonts.googleapis.com/css?family=Montserrat:400,700. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://jobs.bdjobs.com' is therefore not allowed access. The response had HTTP status code 405.", source: http://jobs.bdjobs.com/jobdetails.asp?id=675496&fcatId=-1&ln=1 (0)
I/chromium: [INFO:CONSOLE(0)] "XMLHttpRequest cannot load https://fonts.googleapis.com/css?family=Open+Sans:400,600. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://jobs.bdjobs.com' is therefore not allowed access. The response had HTTP status code 405.", source: http://jobs.bdjobs.com/jobdetails.asp?id=675496&fcatId=-1&ln=1 (0)

我也试过这个答案:android webview - Access-Control-Allow-Origin

但它也不起作用。我还能做什么?

【问题讨论】:

    标签: android webview


    【解决方案1】:

    使用下面的代码,

       mWebview = (WebView) findViewById(R.id.mWebview);
       mWebview.setWebViewClient(new myWebClient());
            mWebview.getSettings().setJavaScriptEnabled(true);
            mWebview.setBackgroundColor(Color.parseColor("#daedf5"));
            mWebview.loadUrl("http://jobs.bdjobs.com/jobdetails.asp?id=675496&fcatId=-1&ln=1");
    

    Webview 加载器:

     /**
     * Load Webview with using Progressbar
     */
    public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }
    
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub
    
            view.loadUrl(url);
            return true;
    
        }
    
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
    
            progressBar.setVisibility(View.GONE);
        }
    }
    

    【讨论】:

    • 但是它加载网站没有任何错误,你是否给了互联网和wifi权限
    • 好的,我会找到解决办法的
    • 我试了一下,网站正在显示,但是当看到 logcat 时显示错误
    猜你喜欢
    • 1970-01-01
    • 2018-10-28
    • 2019-08-15
    • 2017-02-21
    • 2015-09-28
    • 2016-10-30
    • 1970-01-01
    • 2016-10-25
    • 2016-01-11
    相关资源
    最近更新 更多