2011-03-26 86 views
0

如何连接到网站并将HTML抓取到字符串中?我想在我的应用程序的幕后执行此操作。我想在以后的屏幕中解析这些信息。获取HTML为字符串

回答

3

作为起点,检查RIM documentation on HttpConnection(滚动到“使用HttpConnection的示例”)。

该示例将响应作为字节数组读取,但如果您在Java SE中正常工作,则可以轻松更改它以读取字符串。另一点是使用适当的传输(BIS,BES,TCP,WiFi等 - 它应该可以在特定设备上使用)。对于运输检测,您可以检查this

+0

也不是,“新的字符串(byte [])”是字节数组的选项 - 让你更加灵活,因为byte []可以是一个非常实用的数据结构 – Dan 2011-03-27 15:06:43

+0

@Arhimed - 如何改变它到一个字符串? – Christopher 2012-11-15 18:20:40

+1

@Christopher:最简单的方法是通过'new String(byte [] data)'或'new String(byte [] data,String encoding)''。 – 2012-11-15 20:23:35

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

不支持java.net.URL。 – hfitzwater 2011-03-26 13:54:22

+1

BufferedReader – 2011-03-26 23:39:07