【问题标题】:Receiving POST Request on Server (Spring Framework) from Android Application从 Android 应用程序接收服务器(Spring 框架)上的 POST 请求
【发布时间】:2011-10-10 14:18:39
【问题描述】:

我正在从我的 Android 应用程序向服务器发送 POST 请求。服务器是使用 Spring Framework 开发的。服务器接收到请求,但我发送的参数为空/空(显示在日志中)。

用于发送 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);

收到此请求的Server端代码为:

@RequestMapping(value = "/eventlogs/logs", method = RequestMethod.POST)
public String logs(@ModelAttribute("json") String json) {

    logger.debug("Received POST request:" + json);

    return null;
}

我正在记录的 Logger 消息显示:

Received POST request:

任何想法我在这里缺少什么?

【问题讨论】:

    标签: android spring post spring-mvc


    【解决方案1】:

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

    看看Spring Documentation regarding mapping the request body

    您应该能够使用 Springs MessageConverter 实现来做您想做的事情。具体来说,看看FormHttpMessageConverter,它将表单数据转换为MultiValueMap&lt;String, String&gt;/从MultiValueMap&lt;String, String&gt;

    @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/>
    

    【讨论】:

    • 感谢@nicholas.hauschild 的回复。我尝试了 RequestBody 注释,但它返回给我 HTTP 错误 415 不支持的媒体类型 (checkupdown.com/status/E415.html)。之后,我用 RequestParam 注释替换了 RequestBody 注释并且它可以工作。现在我在服务器上获取 POST 请求参数。
    【解决方案2】:

    我使用了 RequestParam 注解,它对我有用。现在服务器端的代码如下:

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

    【讨论】:

    • 并在客户端上将参数指定为请求的实体。将它们指定为 setParams 仍然不起作用。
    • 我也有同样的问题。当我按照你说的做时,我得到 org.springframework.web.bind.MissingServletRequestParameterException: Required Integer parameter 'channel' is not present。我正在从具有基本名称值对的 android 客户端发送参数。你能帮忙吗?
    • @Emilla 是否有可能您的参数可能会丢失,然后将其标记为 required = false。看到这个答案stackoverflow.com/a/3466851/867475
    【解决方案3】:

    我认为您需要从客户端添加内容类型标头。 JSON 的 MessageConverter 使用它可以接受的几个内容类型注册它自己,一个是 application/json。

    如果您使用任何 MessageConvert 未处理的内容类型发送,它将无法正常工作。

    尝试添加“Content-type: application/json”作为标题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多