generated TinyURL doesn't work on Android WebView

1.2k Views Asked by At

I am working in a registration module of an application, using a web service returning a generated TinyURL corresponding to the new user. This TinyUrl gives to the user the access to the platform, by WebView.

Problem : The WebView works very well with any URL but not with the TinyURL . The TinyURL works well on other browsers. Did I miss something?

Java definition and configuration of the WebView :

WebView browser = (WebView) findViewById(R.id.wvBrowser);
WebSettings webSettings = browser.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setAllowContentAccess(true);
    browser.setWebViewClient(new WebViewClient());
    browser.loadUrl(myTinyUrl);

XML definition of the WebView :

<WebView
    android:id="@+id/wvBrowser"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Thanks for your time and help. Every suggestion is welcome.

2

There are 2 best solutions below

0
On BEST ANSWER

WebView URLs should start with either http:// or https://. A URL starting with www... typically shows the standard Android 404 page.

0
On

You can use this method to fix URL errors, like http:// or www..., before loading it :

/**
 * fix the URL by adding missing "www." and "http://"
 * 
 * @param url
 * @return fixed url
 */
public static String fixUrl(String url) {
    if (!(url == null || url.length() == 0)){
        if (!url.startsWith("www.") && !url.startsWith("http://")) {
            url = "www." + url;
        }
        if (!url.startsWith("http://")) {
            url = "http://" + url;
        }
    }
    return url;
}