1

当我运行这段代码时,我会在 httpResponse 中得到响应。它报告标头中的正确字节数。但身体是空的。当我调用 .getBody() 时,它为空。

标题:

<200 OK,{Date=[Thu, 29 Nov 2012 16:26:06 GMT], Server=[Apache], Vary=[Accept-Encoding], Content-Length=[5072], Keep-Alive=[timeout=10, max=100], Connection=[Keep-Alive], Content-Type=[text/html]}>

我究竟做错了什么?

    String url = new String("http://www.myurl.com/scripts/json/v1/slipmanager.php");

    MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
    formData.add("username", userName);
    formData.add("password", md5(userPassword));
    formData.add("method", "getslips");

    RestTemplate template = new RestTemplate(true);
    template.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    HttpHeaders requestHeaders = new HttpHeaders();

    requestHeaders.setContentType(new MediaType("multipart", "form-data"));
    template.getMessageConverters().add(new StringHttpMessageConverter());

    HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
    ResponseEntity<?> httpResponse = null;
    try
    {
        httpResponse = template.exchange(url, HttpMethod.POST, request, null);
        String tmp = (String) httpResponse.getBody();
                    //THIS IS WHERE THE BODY IS NULL
    }
    catch (Exception e) 
    {
        Log.e("POST", e.getMessage(), e);
    }
4

1 回答 1

3

You asked for a response type "null" so Spring thinks you want to discard the body. If you wanted it to be a String, String.class should work. You don't even need the explicit StringHttpMessageConverter as far as I know.

于 2012-11-30T08:52:28.290 回答