Problems with Uri

So, I'm trying to create a URL-address to use httpget on, to download the page source. But I have problems every time I run the app, he says that I have an illegal character in the string / uri. Ive tried the code here.

String Search = "http://www.lala.com/"; 

and

 HttpGet request = new HttpGet("http://www.lala.com/"); 

and every time I try,

 Uri search = new Uri("http://www.lala.com/"); 

I get "Can not create a Uri instance."

I'm not sure what I'm doing wrong, and this is the code for the page source.

 try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://www.lala.com/"); HttpResponse response = client.execute(request); InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while((line = reader.readLine()) != null) { str.append(line); } in.close(); html = str.toString(); } catch (IOException e1) { } "); try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://www.lala.com/"); HttpResponse response = client.execute(request); InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while((line = reader.readLine()) != null) { str.append(line); } in.close(); html = str.toString(); } catch (IOException e1) { } (); try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://www.lala.com/"); HttpResponse response = client.execute(request); InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while((line = reader.readLine()) != null) { str.append(line); } in.close(); html = str.toString(); } catch (IOException e1) { } )! = null) try { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://www.lala.com/"); HttpResponse response = client.execute(request); InputStream in = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder str = new StringBuilder(); String line = null; while((line = reader.readLine()) != null) { str.append(line); } in.close(); html = str.toString(); } catch (IOException e1) { } 

Thanks for the help! ~ Tanner.

(Lala.com is not a site that uses btw: P)

+4
source share
2 answers

This is because Uri is an abstract class and can not be created. Use this instead:

 Uri search = Uri.parse("http://www.lala.com/"); 

If you use HttpGet, you will need to use the java.net.URI class instead of android Uri. Quick example:

 try { URI search = new URI("http://www.lala.com/"); new HttpGet(search); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } "); try { URI search = new URI("http://www.lala.com/"); new HttpGet(search); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
+6
source

Check your import. Perhaps this is due to the conflict between org.eclipse.emf.common.util.URI and java.net.URI. I mean, you can do search = new the Uri the Uri ( " http://www.lala.com/ ") with a second, but first, you will see this error message you.

0
source

All Articles