1

I am trying to be able to view the source code of a webpage after being given a URL in order to parse the text for a certain string which represents and image url.

I found this post which is pretty much what I am after trying to do but can't get it working:

Post

This is my code below.

public String fetchImage() throws ClientProtocolException, IOException {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("www.google.co.uk/images?q=songbird+oasis");
        HttpResponse response = client.execute(request);

        String html = "";
        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();
        return html;
    }

but for some reason it just does not work. It forces me to use a try catch statement in calling the method. Once this works I think it will simple from here using regex to find the string "href="/imgres?imgurl=........jpg" to find the url of a jpg image to then be shown in an image view.

Please tell me if i'm going at this all wrong.

Community
  • 1
  • 1
SamRowley
  • 3,435
  • 7
  • 48
  • 77
  • You are forced to use a try-catch block because the method throws exceptions. Exceptions in Java are checked. Now, what exactly doesn't work? Do you get any kind of exceptions when you run the code? – kgiannakakis Dec 10 '10 at 23:52

1 Answers1

1

First, Google has a search API, which will be a better solution than the scraping you are going through, since the API will be reliable, and your solution will not be.

Second, use the BasicResponseHandler pattern for string responses, as it is much simpler.

Third, saying something "just does not work" is a pretty useless description for a support site like this one. If it crashes, as kgiannakakis pointed out, you will have an exception. Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine the stack trace and find out what the exception is. That will give you some clues for how to solve whatever problem you have.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That's the thing, the app does not crash, I was just after some advice in if this was the correct way to go really. With the Google API how does that relate to and android app? The documentation from the link you gave seems to just give examples in relation to a web page. – SamRowley Dec 11 '10 at 00:14
  • @SamRowley: "the app does not crash" -- then what is wrong? In terms of the Google Image Search API, I just linked to the front door of their site. Here is a link to their JSON API, which "can be used to write image query applications in any language that can handle a JSON-encoded result set with embedded status codes.": http://code.google.com/apis/imagesearch/v1/jsondevguide.html – CommonsWare Dec 11 '10 at 00:31
  • Thank you for the Answer and comments they have been extremely helpful. – SamRowley Dec 11 '10 at 01:14