【发布时间】:2011-09-22 08:47:01
【问题描述】:
我正在尝试用 Java 创建一个 Android 应用程序,我需要将 XML 与其他一些 POST 信息一起发送
它可以工作,当字符串不包含 XML,并且当它包含 XML 时,只附加 XML 的 POST 键,而不是内容
我正在使用以下代码发送 POST 信息
// Check if there is nothing to input
if (input == null)
input = new HashMap<String, String>();
// Add the mobile's ID
input.put("MobileID", ((TelephonyManager)cxt.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId());
// The string for the input
String strInput = "";
// For each input
for (Entry<String, String> ent : input.entrySet()) {
// Check if the string is not empty
if (strInput.length() > 0)
strInput += "&";
// Add to the String
strInput += URLEncoder.encode(ent.getKey(), Encoding.UTF_8.name()) + "=" + URLEncoder.encode(ent.getValue(), Encoding.UTF_8.name());
}
// Open the connection and setup default values
HttpsURLConnection conn = (HttpsURLConnection)url.openConnection();
conn.setRequestMethod("POST");
// Set the RequestProperties
conn.setRequestProperty("Authorization", "Basic " + CtrSettings.getInstance().getAuthentication());
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", strInput.getBytes().length + "");
// Set the booleans for the connection
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// Create the input
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.write(strInput.getBytes());
dos.flush();
dos.close();
// Return the connection
return conn;
谁能告诉我,我做错了什么?
我是否遗漏了有关 Content-Type 的内容?
另一种方法是转义 XML 以在另一个 XML 字符串中使用,但我不希望 PHP 在我转义之前将其识别为 XML(和 XML 标记)(或者它会是什么词?)它
【问题讨论】:
标签: java android xml post content-type