尝试使用HttpURLConnection发出JSON POST请求,构建JSONObject并将其写入输出流的代码在此处:
Iterator dataload = data.entrySet().iterator();
JSONObject sendData = new JSONObject();
while (dataload.hasNext()) {
thisEntry = (Entry) dataload.next();
Object key = thisEntry.getKey();
Object value = thisEntry.getValue();
sendData.put(key.toString(), value.toString());
}
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8;");
connection.setUseCaches (false);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(sendData.toString());
wr.flush();
wr.close();
这显示在wireshark中:
http://i.imgur.com/bSbcPTA.png(无法发布图片,信誉不足)
正如您所看到的,内容被放入基于行的文本数据"字段,而不是存在但空的JSON字段。
谁能告诉我为什么会这样?