从 Android 应用程序接收服务器(Spring 框架)上的 POST 请求

发布于 2024-12-09 11:06:40 字数 1133 浏览 0 评论 0原文

我正在从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

π浅易 2024-12-16 11:06:40

也许 Spring 没有将您的 POST 正文转换为 Model。如果是这种情况,它将不知道您的 Model 上的属性 json 是什么,因为没有 Model

看看 关于映射请求正文的 Spring 文档

您应该能够使用 Springs MessageConverter 实现来完成您想要的操作。具体来说,看一下 FormHttpMessageConverter,它将表单数据与 MultiValueMap 相互转换。

@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@RequestBody Map<String,String> body) {
    logger.debug("Received POST request:" + body.get("json"));

    return null;
}

将此行添加到您的 xml 配置中应该默认启用 FormHttpMessageConverter

<mvc:annotation-driven/>

Perhaps Spring isn't converting your POST body into a Model. If that is the case, it will not know what the attribute json is on your Model because there is no Model!

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 the FormHttpMessageConverter, which converts form data to/from a MultiValueMap<String, String>.

@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@RequestBody Map<String,String> body) {
    logger.debug("Received POST request:" + body.get("json"));

    return null;
}

Adding this line to your xml configuration should enable the FormHttpMessageConverter by default:

<mvc:annotation-driven/>
梦初启 2024-12-16 11:06:40

我已经使用了 RequestParam 注释,它对我有用。现在服务器上的代码如下:

@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@RequestParam("json") String json) {
logger.debug("Received POST request:" + json);

    return null;
}

I have used the RequestParam Annotation and it works for me. Now the code on server is as follows:

@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@RequestParam("json") String json) {
logger.debug("Received POST request:" + json);

    return null;
}
南…巷孤猫 2024-12-16 11:06:40

我认为您需要从客户端添加内容类型标头。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文