hfitzwater
hfitzwater

Reputation: 544

Get HTML as string

How can I connect to a website and grab the HTML into a string? I would like to do this behind the scenes of my application. I want to parse this information in a later screen.

Upvotes: 0

Views: 426

Answers (2)

Vit Khudenko
Vit Khudenko

Reputation: 28418

As a starting point check the RIM documentation on HttpConnection (scroll to "Example using HttpConnection").

The example reads the response as a byte array, but it can be easily changed to read a String if you are OK in Java SE.

Another point is to use a proper transport (BIS, BES, TCP, WiFi, etc. - it should be usable on the particular device). For transport detection you can check this.

Upvotes: 3

Alp
Alp

Reputation: 29739

public static String getContentsFrom(String urlString) throws IOException {
    URL url = new URL(urlString);
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String inputLine;
    String content = "";
    while ((inputLine = in.readLine()) != null) {
        content += inputLine;
    }
    in.close();
    return content;
}

Upvotes: 0

Related Questions