【发布时间】:2014-02-19 09:31:00
【问题描述】:
我想通过 HTTP post 从我的 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;
感谢您对理解问题的帮助。
【问题讨论】:
-
您能向我们展示您的
writestream (out);实现吗?你要关闭流吗?你真的有UTF16吗?小端还是大端?有/没有 BOM?响应如何?你能告诉我们使用 HttpClient 的代码吗? -
回答您的问题:我在写入后关闭流。我将在几分钟后发布与 HttpClient 一起使用的代码。我在“结果”字符串中得到的响应看起来像“中文字符”:)。我不明白你的意思 - “你真的有 UTF 16 ...”
标签: android encoding httpclient httpurlconnection