2

我使用 Java 6 将 XML 数据发送到服务器。

XMLWriter 类中的方法 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 类的完整代码。
Web 服务在此 url 上运行:http://wafriv.de/tatoo_webservice/index.php
发送到 Web 服务的 XML 示例可以在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;
}
4

1 回答 1

0

The problem is solved. The finally caused the method writeToURL to return null.

于 2013-03-01T10:35:09.773 回答