【问题标题】:How to extract XML data to a string via URL如何通过 URL 将 XML 数据提取到字符串
【发布时间】:2018-01-22 05:47:59
【问题描述】:

我想使用 library 将 xml 数据从 URL 转换为 json,但它不处理来自 URL 的 xml ......!只有当它是一个字符串或文件,所以我想将 url 内的数据转换为字符串!

有可能吗?

【问题讨论】:

  • 那么您希望将 url 的服务器返回的数据作为字符串?你做过研究吗?
  • 你有没有试过先把数据下载成字符串再转换?
  • @f1sh 是的,这就是我想要的,我做了很多,在这里尝试了不同类型的解决方案,但没有奏效
  • @Artyom 没想到,我可以关注的任何来源!?
  • @SamZar 您尝试了哪些解决方案?你为什么不发布代码并描述究竟是什么不工作?

标签: java android xml string parsing


【解决方案1】:

这个片段可以帮助你

 new Thread() {
        public void run() {
            URL url = null;
            BufferedReader in = null;
            try {
                url = new URL("your url");

                in = new BufferedReader(
                        new InputStreamReader(
                                url.openStream(),"UTF-8"));//in most cases there is utf 8

                String inputLine;
                StringBuilder builder = new StringBuilder();
                while ((inputLine = in.readLine()) != null)
                    builder.append(inputLine);
                String urlContent = builder.toString();
                // process your received data somehow
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }.start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多