用于android的spring中的HTTP状态代码

时间:2014-10-18 22:37:57

标签: android spring rest

嗨我想在网络出现问题时获取HTTP状态代码。

我使用try catch但我只能接收消息。我如何只采取代码。

还有别的东西。如果出现问题,会自动调用任何事件吗?我可以覆盖它吗?

这是我的asyncTask代码

 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;
    }

谢谢!

0 个答案:

没有答案