1

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.

ahmed_khan_89
  • 2,755
  • 26
  • 49
  • What does "does not work" mean? What does the URL look like? Does it start with `http://` or `https://`? – Nachi May 23 '14 at 08:36
  • `Web page not available` . I think you gave me the solution, my URL is `www....` , I will try to add `http://` before loading it in the `WebView`. I test it and come back – ahmed_khan_89 May 23 '14 at 08:44
  • you can add it as answer , and I will accept it, for people who has the same problem... – ahmed_khan_89 May 23 '14 at 08:58

2 Answers2

1

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

Nachi
  • 4,218
  • 2
  • 37
  • 58
1

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;
}
ahmed_khan_89
  • 2,755
  • 26
  • 49