WebView:如何在8.0 WebView中加载.php文件

时间:2018-12-06 07:14:42

标签: android android-webview android-8.0-oreo

我正在尝试在WebView中加载.php文件。在我更新到API级别28后,它在API级别28之前运行正常,但无法正常显示白屏。没有任何迹象表明我尝试了所有选项。

这是代码

我也在清单文件中添加了安全浏览错误。 我尝试在google中搜索,如果有任何帮助,没有任何帮助,在这种情况下请帮助解决。

  String url="https://xxx/xxx/abc.php";
          @Override
        protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    handleSSLHandshake();
    WebView mWebView = (WebView) findViewById(R.id.webView);
   // PackageInfo webViewPackageInfo = null;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     //   webViewPackageInfo = WebView.getCurrentWebViewPackage();
        mWebView.getSettings().setSafeBrowsingEnabled(false);
       // Log.d("MY_APP_TAG", "WebView version: " + webViewPackageInfo.versionName);
    }
    WebViewClientImpl webViewClient = new WebViewClientImpl(this);
    mWebView.setWebViewClient(webViewClient);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.getSettings().setBuiltInZoomControls(false);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl(url);


    if (18 < Build.VERSION.SDK_INT ){
        //18 = JellyBean MR2, KITKAT=19
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    }
}

   public class WebViewClientImpl extends WebViewClient {

    private Activity activity = null;

    public WebViewClientImpl(Activity activity) {
        this.activity = activity;
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView webView, String url) {

        webView.loadUrl(url);
        // Log.i(TAG,url);
        return true;

    }

    @Override
    public void onLoadResource(WebView view, String url) {
        super.onLoadResource(view, url);
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);

    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);


    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);

    }

    @Override
    public void onPageFinished(WebView view, String url) {


    }
}

1 个答案:

答案 0 :(得分:0)

我解决了我的问题。这是证书问题,我刚刚添加了以下代码,现在可以正常使用了

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        return true;
    }
   @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }