8

So from my first screen I am passing a URL to an activity to launch in webview. But when webview is launched , it shows "web page not available - The web page at URL might be temporarily down or it may have moved permanently to a new web address"

But when i launch the same URL in android browser, it works fine. Here is my code for launching that URL in webview

    super.onCreate(savedInstanceState);

    String url = "";
    url = getIntent().getStringExtra("loginURL");
    WebView urlWebView = new WebView(this);
    urlWebView.setWebViewClient(new WebViewClient());

    urlWebView.getSettings().setJavaScriptEnabled(true);
    urlWebView.loadUrl(url);
    this.setContentView(urlWebView);

What am I doing wrong?

yogsma
  • 10,142
  • 31
  • 97
  • 154

2 Answers2

27

I found the issue. The issue was that the URL I was using has https:// and SSL certificate for the URL was self-signed. The solution from Does the Web View on Android support SSL? helped me fixed the issue.

I added below part in my code

import android.net.http.*; //added this import statement

urlWebView.setWebViewClient(new WebViewClient(){

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

Hope this will help other users.

Community
  • 1
  • 1
yogsma
  • 10,142
  • 31
  • 97
  • 154
  • 2
    Beware. Applications or updates using this approach will be rejected in Google Play. See [this post](https://support.google.com/faqs/answer/7071387). – kibitzerCZ Mar 15 '18 at 21:54
  • 1
    @kibitzerCZ - Yup, I am aware of that. Google asks for showing a warning message for using self-signed SSL certificates. – yogsma Apr 13 '18 at 19:20
2

Make sure the url string you're loading has "www" prefixed to it, i.e. "https://www.google.com" and not "https://google.com"

Weirdly enough, this fixed the issue, at least on my end.

M.Ed
  • 969
  • 10
  • 12