我正在尝试使用Spring for Android从REST服务检索一些数据。
但是我遇到了问题。我也在使用Robospice-因此具有这样的方法:

 public FunkyThingsList loadDataFromNetwork() throws Exception {
        String baseURI =context.getResources().getString(R.string.baseWebServiceURI);
                String uri = baseURI + "/FunkyThings/date/" + dateToLookUp.toString("yyyy-MM-dd");
        RestTemplate restTemplate = getRestTemplate();
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setAccept( Arrays.asList(MediaType.APPLICATION_JSON));
        HttpAuthentication authHeader = new HttpBasicAuthentication(username, password);
        headers.setAuthorization(authHeader);

        // Create the request body as a MultiValueMap
        MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
        HttpMessageConverter<String> stringConverter = new StringHttpMessageConverter();
        FormHttpMessageConverter formConverter = new FormHttpMessageConverter();
        List<HttpMessageConverter<?>> msgConverters = restTemplate.getMessageConverters();
        msgConverters.add(formConverter);
        msgConverters.add(stringConverter);
        restTemplate.setMessageConverters(msgConverters);

        HttpEntity<?> httpEntity = new HttpEntity<Object>(map, headers);
        final ResponseEntity<FunkyThingsList> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity,FunkyThingsList.class);

        return responseEntity.getBody();
    }


不幸的是,这不起作用。我收到以下异常:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.MyProject.DataClassPojos.RoboSpiceArrayLists.FunkyThingsList] and content type [application/json;charset=utf-8]


现在基于我的谷歌搜索,我感觉我需要添加一个消息转换器。我只是不确定我需要哪个消息转换器,或在哪里添加它?

最佳答案

对于默认的JSON HttpMessageConverter,您需要将Jackson 1或Jackson 2添加到您的类路径中。

否则,您可以添加其他一些JSON库并编写自己的HttpMessageConverter来进行反序列化。您将其添加到RestTemplate。您可以使用构造函数或this method

08-04 23:50