Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uri problems

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 )

like image 283
Tanner Avatar asked May 25 '11 18:05

Tanner


1 Answers

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();
    }
like image 177
nibuen Avatar answered Sep 18 '22 23:09

nibuen