-4

I'm trying to create a URI from a URL for my Android application.

I've found the answer here How to create a Uri from a URL? but sadly, I am getting an exception

java.net.MalformedURLException

My code is

URL connection_url = new URL("http://www.google.com"); // exception on this line
URI uri = url.toURI();

I am importing the following

import java.net.URI;
import java.net.URL;

I'm not sure what I've done wrong

Edit

Updated from www.google.com to http://www.google.com - same error

Community
  • 1
  • 1
MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120

2 Answers2

1

"www.google.com" not a valid URL as it misses the protocol part. Try something like:

URL connection_url = new URL("http://www.google.com");

For example this works without problems:

public static void main(String[] args) throws MalformedURLException, URISyntaxException {
    URL connection_url = new URL("http://www.google.com");
    URI uri = connection_url.toURI();
    System.out.println(uri);
}
Henry
  • 42,982
  • 7
  • 68
  • 84
  • Copying and pasting the code still fails in the same manner I describe above, until I do the throws MalformedURLExpcetion, URISyntaxException – MyDaftQuestions Aug 02 '14 at 15:46
0

Check this

Uri uri =  Uri.parse("http://www.google.com");
Bishan
  • 15,211
  • 52
  • 164
  • 258