从 Android 应用程序接收服务器(Spring 框架)上的 POST 请求
我正在从 Android 应用程序向服务器发送 POST 请求。服务器采用Spring框架开发。服务器收到请求,但我发送的参数为空/空(在日志中显示)。
用于发送 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:
我缺少什么想法这里 ?
I am sending a POST request to a Server from my Android Application. The Server is developed using Spring Framework. The request is received by the server but the parameter that I was sending is null/empty (shown in the log).
The code used to send POST request is:
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);
I have also tried to set HttpParams as follows but it has also failed:
HttpParams params = new BasicHttpParams();
params.setParameter("json", postMessage);
postMethod.setParams(params);
The code on Server side that received this request is:
@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@ModelAttribute("json") String json) {
logger.debug("Received POST request:" + json);
return null;
}
The Logger message that I am logging shows:
Received POST request:
Any ideas what I am missing here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许 Spring 没有将您的
POST
正文转换为Model
。如果是这种情况,它将不知道您的Model
上的属性json
是什么,因为没有Model
!看看 关于映射请求正文的 Spring 文档。
您应该能够使用 Springs
MessageConverter
实现来完成您想要的操作。具体来说,看一下FormHttpMessageConverter
,它将表单数据与MultiValueMap
相互转换。将此行添加到您的 xml 配置中应该默认启用
FormHttpMessageConverter
:Perhaps Spring isn't converting your
POST
body into aModel
. If that is the case, it will not know what the attributejson
is on yourModel
because there is noModel
!Take a look at the Spring Documentation regarding mapping the request body.
You should be able to use Springs
MessageConverter
implementations to do what you want. Specifically, take a look at theFormHttpMessageConverter
, which converts form data to/from aMultiValueMap<String, String>
.Adding this line to your xml configuration should enable the
FormHttpMessageConverter
by default:我已经使用了 RequestParam 注释,它对我有用。现在服务器上的代码如下:
I have used the RequestParam Annotation and it works for me. Now the code on server is as follows:
我认为您需要从客户端添加内容类型标头。 JSON 的 MessageConverter 会自行注册一些它会接受的内容类型,其中一个是 application/json。
如果您使用未由任何 MessageConvert 处理的内容类型发送,它将不起作用。
尝试添加“Content-type: application/json”作为标题。
I think you need add a content-type header from the client. The MessageConverter for JSON registers it self with a couple of content-type it will accept, one is application/json.
If you send with a content-type not handled by any MessageConvert it won't work.
Try adding "Content-type: application/json" as a header.