我想從我的Android應用程序使用resttemplate
從我的web應用程序MyApp
運行在服務器服務器172.16.xx.15
上的項目列表。 一切工作正常,當我不喜歡在Android的REST調用類拋出異常
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);
上面的代碼時,我對上面的網址匿名訪問的正常工作。但我在服務器端使用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);
此代碼也有效,但會引發錯誤和應用程序崩潰。
java.lang.ClassCastException:org.springframework.http.ResponseEntity 不能轉換爲java.util.List的
請在這種情況下幫助。
感謝您的幫助。還有一件事。如何認證用戶意味着真或假。 – ashok
如果這有幫助請標記爲已接受) – Rizwan