我使用Java 6将XML数据发送到服务器。
XMLWriter类的Method writeToURL使用URLConnection将XML数据发送到我的Web服务。 XML已成功传输并存储在数据库中,但不是接收下面提到的响应,而是仅获取null。也没有例外。
结果应该有以下XML或包含它的InputStream:
<?xml version="1.0" encoding="UTF-8"?>
<message>
<type>response</type>
<messageID>k17436fxOD3y1ywhCX48</messageID>
<status>successful</status>
</message>
或
<?xml version="1.0" encoding="UTF-8"?>
<message>
<type>response</type>
<messageID>k17436fxOD3y1ywhCX48</messageID>
<status>alreadyExisting</status>
</message>
可以找到完整的XML类代码here
网络服务在此网址上运行:http://wafriv.de/tatoo_webservice/index.php
可以在http://paste.geekosphere.org/p5bf
任何帮助都将不胜感激。
[编辑]
我遇到问题的代码是来自XMLWriter的方法writeToURL
这是:
try
{
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setRequestProperty("accept-charset", charset);
connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
String query = "";
if (additionalParams != null)
{
for (Map.Entry<String, Object> entry : additionalParams.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
query += String.format("%s=%s&", URLEncoder.encode(key, charset), URLEncoder.encode(value.toString(), charset));
}
}
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
DOMSource source = new DOMSource(this.document);
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
query += String.format("%s=%s", URLEncoder.encode("message", charset), URLEncoder.encode(sw.toString(), charset));
OutputStreamWriter writer = null;
InputStream response;
try
{
writer = new OutputStreamWriter(connection.getOutputStream(), charset);
writer.write(query); // Write POST query string (if any needed).
}
finally
{
if (writer != null)
{
try
{
writer.close();
}
catch (IOException ex)
{
this.exceptionList.add(ex);
}
}
}
response = connection.getInputStream();
return response;
}
catch (TransformerConfigurationException ex)
{
this.exceptionList.add(ex);
}
catch (TransformerException ex)
{
this.exceptionList.add(ex);
}
catch (MalformedURLException ex)
{
this.exceptionList.add(ex);
}
catch (IOException ex)
{
this.exceptionList.add(ex);
}
catch (Exception ex)
{
this.exceptionList.add(ex);
}
finally
{
this.printExceptions();
return null;
}
答案 0 :(得分:0)
问题解决了。 finally
导致方法writeToURL返回null。