开发者

Get HTML as string

How can I connect to a websit开发者_Go百科e 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.


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.


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜