Java 来自android的REST调用中的类强制转换异常

Java 来自android的REST调用中的类强制转换异常,java,android,Java,Android,我正在尝试使用restemplate从我的android应用程序中获取在服务器172.16.xx.15上运行的web应用程序MyApp中的项目列表。 只要我喜欢,一切都很好 String url="http://172.16.xx.15:8080/MyApp/GetAllItem"; RestTemplate restTemplate=new RestTemplate(); restTemplate.getMessageConverters().add(new MappingJackson2H

我正在尝试使用
restemplate
从我的android应用程序中获取在服务器
172.16.xx.15
上运行的web应用程序
MyApp
中的项目列表。 只要我喜欢,一切都很好

String url="http://172.16.xx.15:8080/MyApp/GetAllItem";

RestTemplate restTemplate=new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());

List<LinkedHashMap> items=restTemplate.getForObject(url, List.class);
stringurl=”http://172.16.xx.15:8080/MyApp/GetAllItem";
RestTemplate RestTemplate=新RestTemplate();
restemplate.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
List items=restemplate.getForObject(url,List.class);
当我匿名访问上面的url时,上面的代码可以正常工作。但我在服务器端使用spring安全性,在访问此项目列表之前,我需要对用户进行身份验证。因此,我试图通过身份验证获得相同的项目列表。我正在使用以下代码:

String username="test"
String password="test"
HttpAuthentication authHeader=new HttpBasicAuthentication(username, password)  
HttpHeaders requestHeaders=new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON);
 RestTemplate restTemplate=new RestTemplate();
    restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
 List<LinkedHashMap> items=(List<LinkedHashMap>)restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), List.class);
String username=“测试”
字符串password=“test”
HttpAuthentication authHeader=新的HttpBasicAuthentication(用户名、密码)
HttpHeaders requestHeaders=新的HttpHeaders();
setAuthorization(authHeader);
setAccept(Collections.singletonList(MediaType.APPLICATION\ujson);
RestTemplate RestTemplate=新RestTemplate();
restemplate.getMessageConverters().add(新映射Jackson2HttpMessageConverter());
List items=(List)restemplate.exchange(url,HttpMethod.GET,新的HttpEntity(requestHeaders),List.class);
这段代码也可以工作,但会引发错误和应用程序崩溃

java.lang.ClassCastException:org.springframework.http.ResponseEntity 无法强制转换为java.util.List


请在这种情况下提供帮助。

您直接将exchange的响应(即ResponseEntity)投射到您的收藏中

你需要先把它收起来

ReaponseEntity<List<LinkedHashMap>> response

谢谢你的帮助。还有一件事。如何认证用户意味着正确或错误。
response.getBody()
 List<LinkedHashMap> items=(List<LinkedHashMap>)restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), List.class);
ResponseEntity<<LinkedHashMap>> entity = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), List.class);
List<LinkedHashMap> items = entity.getBody()