我正在尝试对 url 进行 http 请求。我认为它会在响应正文中使用位图进行响应,但出现以下错误:
03-13 22:54:36.106: W/System.err(26547): org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [android.graphics.Bitmap] and content type [image/jpg;charset=utf-8]
03-13 22:54:36.106: W/System.err(26547): at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:79)
03-13 22:54:36.116: W/System.err(26547): at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:655)
03-13 22:54:36.116: W/System.err(26547): at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:641)
03-13 22:54:36.116: W/System.err(26547): at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:484)
03-13 22:54:36.116: W/System.err(26547): at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:439)
03-13 22:54:36.116: W/System.err(26547): at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:415)
03-13 22:54:36.116: W/System.err(26547): at com.hera.ooshopping.AsyncPicLoading.getFileFromServer(AsyncPicLoading.java:174)
03-13 22:54:36.116: W/System.err(26547): at com.hera.ooshopping.AsyncPicLoading.doInBackground(AsyncPicLoading.java:65)
03-13 22:54:36.116: W/System.err(26547): at com.hera.ooshopping.AsyncPicLoading.doInBackground(AsyncPicLoading.java:1)
从错误看来,响应是 JPG(第一行),但我不太确定。我可以解析对字符串的响应,这会创建一个带有奇怪字符的巨大字符串,而不是 base64。
我如何将我的响应体解析为位图或图像什么是可用的?我真的不明白。我的请求代码如下:
HttpBasicAuthentication authHeader = new HttpBasicAuthentication(user.getUsername(), user.getPassword());
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setAuthorization(authHeader);
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
System.out.println("entity:" + requestEntity.toString());
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
restTemplate.getMessageConverters().add(new ResourceHttpMessageConverter());
try {
// GET Request
ResponseEntity<Bitmap> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Bitmap.class);
return response.getBody();
} catch (Exception e) {
e.printStackTrace();
return null;
}