Leo B.
Leo B.

Reputation: 41

Android Webview doesn't open some URLs

I've tried some different ways to create a webview app, but I've a problem with some kind of URLs, for example http://bit.ly/1MV5ov4 (it's just an example, because it has a SPLASH Screen and then it loads the content). This doesn't work, it get stucks on the splash screen of the mobile site and nothing more happens. How to fix?

I'm using a third party code actually, but I've tried in other ways with the same result. Does it has something to do with HTML5 maybe?

public class MainActivity extends Activity {

private WebView mWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);

    // Force links and redirects to open in the WebView instead of in a browser
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setWebViewClient(new ExtendWebViewClient());

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // Use remote resource
    mWebView.loadUrl("http://bit.ly/1MV5ov4");

Upvotes: 4

Views: 2662

Answers (1)

Alexios Karapetsas
Alexios Karapetsas

Reputation: 912

There might be a problem because this website is using https. Please try the following:

private class ExtendWebViewClient extends WebViewClient {

       @Override
       public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
             // Ignore SSL certificate errors
             handler.proceed(); 
       }
}

Then with your webview instance type the following:

mWebView.setWebViewClient(new ExtendWebViewClient());

The onReceivedSslError notify the host application that an SSL error occurred while loading a URL. And by calling proceed() you are overcoming them. However, this can introduce security risks.

Upvotes: 5

Related Questions