android版spring中的HTTP状态代码
嗨,我想在网络出现问题时使用HTTP状态码 我使用try-catch,但我只能接受消息。我怎么能只拿密码 还有别的。如果出现问题,是否会自动调用任何事件?卡尼能超越它吗 这是我的异步任务代码android版spring中的HTTP状态代码,android,spring,rest,Android,Spring,Rest,嗨,我想在网络出现问题时使用HTTP状态码 我使用try-catch,但我只能接受消息。我怎么能只拿密码 还有别的。如果出现问题,是否会自动调用任何事件?卡尼能超越它吗 这是我的异步任务代码 protected HistoricalWeatherResponse doInBackground(String... params) { HistoricalWeatherResponse response = new HistoricalWeatherResponse();
protected HistoricalWeatherResponse doInBackground(String... params) {
HistoricalWeatherResponse response = new HistoricalWeatherResponse();
try {
HttpHeaders requestHeaders = new HttpHeaders();
List<MediaType> acceptableMedia = new ArrayList<MediaType>();
acceptableMedia.add(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(acceptableMedia);
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<HistoricalWeatherResponse> responseEntity = restTemplate.exchange("http://api.openweathermap.org/data/2.5/history/city?q="+params[0]+"&cnt=2", HttpMethod.GET, requestEntity, HistoricalWeatherResponse.class);
response = responseEntity.getBody();
response.setSuccess(true);
} catch (Exception e) {
response.setError(e);
response.setSuccess(false);
Log.d("LEE",e.getMessage());
}
return response;
}
谢谢 您使用什么类来执行网络操作?我使用restTemplate,请参见上面添加的示例。我想在catch中获取状态代码。为了自己识别错误,ResponseEntity类中有一个名为getStatusCode的方法,该方法返回HttpStatus枚举,该枚举具有处理状态代码的方法。