我想通过HTTP发布从我的Android移动应用向服务器发送XML消息。

我按照以下步骤使用HttpUrlConnection进行了尝试:

URL url = new URL(vURL);

HttpUrlConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);

// Adding headers (code removed)

conn.setRequestProperty("Content-Type", "text/xml; charset=utf-16");
OutputStream out = new BufferedOutputStream(conn.getOutputStream());

// Adding XML message to the connection output stream
// I have removed exception handling to improve readability for posting it here
out.write(pReq.getBytes()); // here pReq is the XML message in String
out.close();

conn.connect();


一旦获得响应,流读取部分就以这种方式完成:

BufferedReader in = null;
StringBuffer sb;
String result = null;

    try {

        InputStreamReader   isr = new InputStreamReader(is);
            // Just in case, I've also tried:
            // new InputStreamReader(is, "UTF-16");
            // new InputStreamReader(is, "UTF-16LE");
            // new InputStreamReader(is, "UTF-16BE");
            // new InputStreamReader(is, "UTF-8");

        in = new BufferedReader(isr);

        sb = new StringBuffer("");
        String line = "";

        while ((line = in.readLine()) != null)
            sb.append(line);

        in.close();

        result = sb.toString();

    } catch (Exception e) {

        e.printStackTrace();
    }


现在,我得到的结果字符串采用某种无法读取的格式/编码。

当我尝试使用HttpClient进行相同操作时,它可以正常工作。这是我在HttpClient.execute调用后收到HttpResponse之后的流式阅读部分:

BufferedReader in = null;
    InputStream         is;
    StringBuffer sb;
    String decompbuff = null;

    try {

        is = pResponse.getEntity().getContent();
        InputStreamReader   isr = new InputStreamReader(is);
        in = new BufferedReader(isr);

        // Prepare the String buffer
        sb = new StringBuffer("");

        String line = "";

        while ((line = in.readLine()) != null)
            sb.append(line);

        in.close();

         // gZip decompression of response. Note: message was compressed before
         // posting it via HttpClient (Posting code is not mentioned here)
        decompbuff = Decompress(sb.toString());

    } catch (Exception e) {

        e.printStackTrace();
    }

    return decompbuff;


在理解该问题方面将获得一些帮助。

最佳答案

一个(严重)问题可能是您忽略了输入和输出的编码。

输入项

conn.setRequestProperty("Content-Type", "text/xml; charset=utf-16");
OutputStream out = new BufferedOutputStream(conn.getOutputStream());

// Adding XML message to the connection output stream
// I have removed exception handling to improve readability for posting it here
out.write(pReq.getBytes()); // <-- you use standard platform encoding
out.close();


更好:

out.write(pReq.getBytes("UTF-16"));


输出量

您可能忽略了压缩,压缩看起来最好像这样(取自DavidWebb):

static InputStream wrapStream(String contentEncoding, InputStream inputStream)
        throws IOException {
    if (contentEncoding == null || "identity".equalsIgnoreCase(contentEncoding)) {
        return inputStream;
    }
    if ("gzip".equalsIgnoreCase(contentEncoding)) {
        return new GZIPInputStream(inputStream);
    }
    if ("deflate".equalsIgnoreCase(contentEncoding)) {
        return new InflaterInputStream(inputStream, new Inflater(false), 512);
    }
    throw new RuntimeException("unsupported content-encoding: " + contentEncoding);
}

// ...

    InputStream is = wrapStream(conn.getContentEncoding(), is);
    InputStreamReader isr = new InputStreamReader(is, "UTF-16");

    in = new BufferedReader(isr);

    sb = new StringBuffer("");
    String line = "";

    while ((line = in.readLine()) != null)
        sb.append(line); // <-- you're swallowing linefeeds!

    in.close();
    result = sb.toString();


最好让XML-Parser直接使用InputStream。不要创建JAVA字符串,而是让解析器扫描字节。它将自动检测XML的编码。

通常,仍然可能存在问题,因为我们不知道您使用哪种类型的UTF-16。可以是BigEndian或LittleEndian。这就是为什么我问您是否真的需要UTF-16的原因。如果您不必使用某些亚洲语言,则UTF-8应该更高效且更易于使用。

因此,我不能保证为您提供的“解决方案”可以正常工作-您必须对UTF-16 BE / LE稍作调整,并祝您好运和耐心。

另一点注意:在上面的示例中,您首先构造String,然后Decompress构造它。那是错误的顺序。流已压缩(gzip,deflate),必须先解压缩。然后,您将得到String。

10-07 19:12
查看更多