我的应用中的WebView未加载HTTPS URL

时间:2018-03-14 14:57:32

标签: android android-layout android-fragments https

当我尝试在WebView中加载网址时,它只会显示一个空白屏幕。如果我加载https://www.google.comhttps://www.facebook.com,则表示正常。

package com.example.hp.cccapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        WebView webb=(WebView)findViewById(R.id.web1);
        webb.setWebViewClient(new WebViewClient());
        //webb.loadUrl("https://www.google.com/");
        webb.loadUrl("https://192.168.2.29/ccc/");

    }
}

任何人都可以建议我如何执行此操作,以便我的WebView可以处理HTTPS网址?

2 个答案:

答案 0 :(得分:2)

我与您分享对我有用的解决方案,它给了我juste访问网页的权限:

import android.net.http.SslError;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.GeolocationPermissions;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState)
{

    WebView webView;

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setBuiltInZoomControls(true);

    webView.getSettings().setGeolocationEnabled(true);
    webView.setWebViewClient(new WebViewClient()
    {

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler 
       handler, SslError error)
        {
            handler.proceed();
        }
    });

        webView.loadUrl("https://192.168.2.29/ccc/");

        webView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, 
            GeolocationPermissions.Callback callback)
            {
                callback.invoke(origin,true,false);
            }
        });
    }

}

答案 1 :(得分:1)

尝试添加setJavascriptEnabled(true)。并改变`

webb.setWebViewClient(new WebViewClient());

到此webView.setWebChromeClient(new WebChromeClient());

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        WebView webb=(WebView)findViewById(R.id.web1);
        webb.setWebViewClient(new WebViewClient());
        webb.getSettings().setJavaScriptEnabled(true);

        //webb.loadUrl("https://www.google.com/");
        webb.loadUrl("https://192.168.2.29/ccc/");

    }
}