有 Java 编程相关的问题?

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

java缓冲读取器从同一台服务器检索到结构稍有不同的XML中的无值和文件结尾

我有两个XML文件,其中一个是我目前从远程源检索的。另一个XML结构稍有不同,会导致以下错误:

[Fatal Error] :1:1: Premature end of file.
Oct 18, 2015 11:33:19 AM jtabs.Jtabs getdata1
SEVERE: null
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
    at jtabs.Jtabs.getdata1(Jtabs.java:576)
    at jtabs.Jtabs.access$000(Jtabs.java:43)
    at jtabs.Jtabs$1.run(Jtabs.java:452)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

我可以从中检索数据的XML文件具有以下结构:

<response>
<responseMetadata>
 <httpCode>200</httpCode>
 <errorType>Ok</errorType>
 <description>Success</description>
 <cappingApplied>No</cappingApplied>
 <cappingLimit>2000</cappingLimit>
 <queryString>EventStart=2015-08-11,EventEnd=2015-09-16,</queryString>
 </responseMetadata>
<responseBody>
 <dataItem>A</dataItem>
<responseList>
<item>
 <A>***</A>
 <B>***</B>
 <C>***</C>

etc etc

我无法从中检索数据的XML文件具有以下结构:

<response>
<responseMetadata>
 <httpCode>200</httpCode>
 <errorType>Ok</errorType>
 <description>Success</description>
 <cappingApplied>No</cappingApplied>
 <cappingLimit>500</cappingLimit>
 <queryString>FromDate=2015-10-18,ToDate=2015-10-19</queryString>
 </responseMetadata>
<responseHeader>
 <recordType>AA</recordType>
 <fileType>BB</fileType>
 </responseHeader>
<responseBody>
<responseList>
<item>
 <A>***</A>
 <B>***</B>
 <C>***</C>

etc etc

检索工作XML文件数据的方法是:

private static String callServer(String uri1, String params1) {
OutputStreamWriter osw = null;
BufferedReader br = null;
HttpURLConnection urlConn = null;
StringBuilder retVal1 = new StringBuilder();
try {
final URL url = new URL(uri1 + "?" + params1);
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setAllowUserInteraction(false);
urlConn.setRequestProperty("Content-Type","text/xsl");
// Get the response
br = new BufferedReader(new InputStreamReader(
urlConn.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
retVal1.append(line).append("\n");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
osw.close();
} catch (Exception ignore) {
}
try {
br.close();
} catch (Exception ignore) {
}
try {
urlConn.disconnect();
} catch (Exception ignore) {
}
}
return retVal1.toString();
}

然而,当我尝试使用上面相同的代码检索XML文件的数据时,这里似乎失败了:

while ((line = br.readLine()) != null) 

并导致上述错误

我在谷歌上搜索了一些建议,其中包括格式不正确的XML,但使用XML记事本时,结构与前面提到的略有不同,我能得到一些帮助吗

我刚刚运行了以下程序

int responseCode = urlConn.getResponseCode();
System.out.println("Response Code : " + responseCode);
for (Entry<String, List<String>> header : urlConn.getHeaderFields().entrySet()) {
System.out.println(header.getKey() + "=" + header.getValue());

}

这带来了一些有趣的结果

控制台显示

Response Code : 200
null=[HTTP/1.1 200 OK]
Server=[nginx/1.6.0]
Content-Disposition=[null]
Connection=[keep-alive]
Content-Length=[0]
Date=[Sun, 18 Oct 2015 16:08:10 GMT]
Content-Type=[text/plain]
[Fatal Error] :1:1: Premature end of file.
Oct 18, 2015 5:08:07 PM jtabs.Jtabs getdata1
SEVERE: null
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
    at jtabs.Jtabs.loadXMLFromString(Jtabs.java:300)
    at jtabs.Jtabs.getdata1(Jtabs.java:582)
    at jtabs.Jtabs.access$000(Jtabs.java:44)
    at jtabs.Jtabs$1.run(Jtabs.java:458)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

这表明内容长度为0。而如果我在工作XML上运行相同的代码,我会得到一个代表数据的长数字

然而,如果我将相同的非工作url字符串放入XML记事本,我会看到我想要的项目,并且数据是可见的

如蒙帮助,将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    解决了-我忽略了urlConn。setRequestMethod(“POST”);应该设置为urlConn。setRequestMethod(“GET”)