我正在尝试从客户端向服务器发送一些json数据:
这是一段客户端应用代码:
JSONStringer json = new JSONStringer().object()
.key("content").value("Hi there!").endObject();
StringEntity entity = new StringEntity("JSON: " + json.toString());
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
如何在serever-app代码中检索此数据?
例如,如果我使用NameValuePair而不是StringEntity,我将在服务器代码中使用req.getParameter("content");
检索它。但是使用JSON的StringEntity呢?
更新: 好的,我明白了,这是解决方案(没有解析):
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
BufferedReader reader = req.getReader();
StringBuilder sb = new StringBuilder();
String line = reader.readLine();
while (line != null) {
sb.append(line + "\n");
line = reader.readLine();
}
reader.close();
String data = sb.toString();
}