有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用url作为StringReader的输入源

对于这个愚蠢的问题,我深表歉意,但我试图在下面的代码中使用一个联机的xml文件

String uri =
 "http://www.myserver.com/xml?month=Jan";

        URL url = new URL(uri);
        HttpURLConnection connection =
            (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/xml");

        InputStream xml1 = connection.getInputStream();

InputSource xml = new InputSource(new StringReader(xml1));

在不得不提问之前,我已经尝试过研究答案,但无论我如何尝试让它起作用,我都会得到一个错误,即“构造函数StringReader(InputStream)未定义”

感谢您提供的任何帮助


共 (1) 个答案

  1. # 1 楼答案

    试一试

    InputSource xml = new InputSource(xml1);
    

    正如错误消息所说,^{}没有采用InputStream实例的构造函数