ishi Prinsloo
ishi Prinsloo

Reputation: 63

Error while trying to load a URL in webview - android

The URL, I'm trying to load in a webview in one of my activities shows blank because of an SSL error.

I have tried working with the network security configuration XML folder, but I'm not sure I understand what I'm doing. Any help would be greatly appreciated.

While debugging, when I load Google.com as the URL in the webview, the page loads fine. Then, when I try to search for the particular site, it's there, but when I click on it I get an SSL error in Android Studio's run log.

public class About_ALC extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about__alc);
    final WebView webview = (WebView) findViewById(R.id.web_about_alc);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setLoadWithOverviewMode(true);
    webview.getSettings().setUseWideViewPort(true);
    webview.setWebViewClient(new WebViewClient());
    webview.setWebChromeClient(new WebChromeClient());
    webview.loadUrl("https://andela.com/alc/");
}

the error message I receive in Android studio's is:

"E/chromium: [ERROR:ssl_client_socket_impl.cc(947)] handshake failed; returned -1, SSL error code 1, net_error -202"

Upvotes: 6

Views: 5351

Answers (3)

Carlos Galindo
Carlos Galindo

Reputation: 131

I share my way, without extends

webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
   @Override
   public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
      //super.onReceivedSslError(view, handler, error);
      handler.proceed();//skip ssl error
   }
});

webView.loadUrl(URL);

Edit:

Play Store may take it as vulnerability error, some solution if anybody needs is, show the error dialog to user. This is to no accept the error automatically and the responsability is the user, like that

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(msg);
builder.setPositiveButton("continue", (dialog, which) -> handler.proceed());
builder.setNegativeButton("cancel", (dialog, which) -> handler.cancel());
final AlertDialog dialog = builder.create();
dialog.show();

Upvotes: 0

yogesh
yogesh

Reputation: 41

Play store may reject your build if you use this code

@Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // When an error occurs, ignore and go on
    }

Upvotes: 0

OGB
OGB

Reputation: 674

You need to extend the WebViewClient to allow loading despite ssl errors as follows.

    // A new webclient that ignore ssl errors
    private class IgnoreSSLErrorWebViewClient extends WebViewClient {
        // You can all the class anything

    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // When an error occurs, ignore and go on
    }
}

The site has issues with ssl certificate verification. The default WebViewClient stops loading when there is an ssl verification error. This extension will instruct it to disregard the error and proceed.

So change the line webview.setWebViewClient(new WebViewClient());

to

webview.setWebViewClient(new IgnoreSSLErrortWebViewClient());

this should work fine for you now.

Upvotes: 8

Related Questions