-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

6
  • What else does the error say? Commented Aug 2, 2014 at 15:32
  • Did you really write hpttp:? This indeed gives a MalformedURLException Commented Aug 2, 2014 at 15:45
  • Sorry @Henry, no, that was a typo Commented Aug 2, 2014 at 15:46
  • 1
    Again, post the full stack trace with the full error message. Commented Aug 2, 2014 at 15:48
  • 3
    Or is it a compiler error? Commented Aug 2, 2014 at 15:48

2 Answers 2

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); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Copying and pasting the code still fails in the same manner I describe above, until I do the throws MalformedURLExpcetion, URISyntaxException
0

Check this

Uri uri = Uri.parse("http://www.google.com"); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.