【问题标题】:Get HTML as string以字符串形式获取 HTML
【发布时间】:2011-03-26 13:22:29
【问题描述】:

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

【问题讨论】:

    标签: blackberry java-me


    【解决方案1】:

    首先检查RIM documentation on HttpConnection(滚动到“使用 HttpConnection 的示例”)。

    该示例将响应读取为字节数组,但如果您在 Java SE 中没问题,可以轻松将其更改为读取字符串。

    另一点是使用适当的传输(BIS、BES、TCP、WiFi 等 - 它应该可以在特定设备上使用)。对于传输检测,您可以查看this

    【讨论】:

    • 另外,"new String(byte[])" 是字节数组的一个选项——让你更加灵活,因为 byte[] 可以是一个非常实用的数据结构
    • @Arhimed - 如何将其更改为字符串?
    • @Christopher:最简单的方法是通过new String(byte[] data)new String(byte[] data, String encoding)
    【解决方案2】:
    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;
    }
    

    【讨论】:

    • java.net.URL 不受支持。
    猜你喜欢
    • 2011-07-08
    • 1970-01-01
    • 2016-12-22
    • 2016-02-20
    • 2011-10-25
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    相关资源
    最近更新 更多