Pages

Friday, 19 July 2013

Android - Convert String to valid URL

Just copy-paste below code and call the function with your string url as param.
private URL ConvertToUrl(String urlStr) {
 try {
  URL url = new URL(urlStr);
  URI uri = new URI(url.getProtocol(), url.getUserInfo(),
    url.getHost(), url.getPort(), url.getPath(),
    url.getQuery(), url.getRef());
  url = uri.toURL();
  return url;
 } catch (Exception e) {
  e.printStackTrace();
 }
 return null;
}

No comments :

Post a Comment