WebView没有'Access-Control-Allow-Origin'标题存在[ANDROID]

时间:2016-11-21 06:06:28

标签: android webview

Bellow是我的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");

Bellow是我的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

但它也没有用。我还能做些什么?

1 个答案:

答案 0 :(得分:-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 Loader:

 /**
 * 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);
    }
}