我将从我的安卓应用向服务器发送一个POST请求。该服务器使用Spring框架开发。服务器接收到了请求,但是我发送的参数为空/为null(在日志中显示)。
用于发送POST请求的代码如下:
我也尝试将HttpParams设置为以下内容,但仍然失败了:
有什么想法我错过了什么吗?
用于发送POST请求的代码如下:
DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
String postMessage = "json String";
HttpPost postMethod=new HttpPost("http://ip:port/event/eventlogs/logs");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("json", postMessage));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
hc.execute(postMethod,res);
我也尝试将HttpParams设置为以下内容,但仍然失败了:
HttpParams params = new BasicHttpParams();
params.setParameter("json", postMessage);
postMethod.setParams(params);
接收到此请求的服务器端代码如下:
@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@ModelAttribute("json") String json) {
logger.debug("Received POST request:" + json);
return null;
}
我正在记录的日志消息显示:
Received POST request:
有什么想法我错过了什么吗?