So Im trying to create a url to use httpget on, to download the page source. But Im having problems every time I run the app it says i have an illegal character in the string/uri. Here's the code Ive tried.
String Search = "http://www.lala.com/";
also,
HttpGet request = new HttpGet("http://www.lala.com/");
and every time i try,
Uri search = new Uri("http://www.lala.com/");
I get "Cannot instantiate Uri".
Im not really sure what im doing wrong, also this is the code to get 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)
{
}
Thanks for the help! ~ Tanner.
(lala.com is not the site I am using btw :P )
That is because Uri is an abstract class and can't be instantiated. Use this instead:
Uri search = Uri.parse("http://www.lala.com/");
If using HttpGet you will need to use the class java.net.URI instead of the android Uri. A quick example:
try {
URI search = new URI("http://www.lala.com/");
new HttpGet(search);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With