Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android版spring中的HTTP状态代码_Android_Spring_Rest - Fatal编程技术网

android版spring中的HTTP状态代码

android版spring中的HTTP状态代码,android,spring,rest,Android,Spring,Rest,嗨,我想在网络出现问题时使用HTTP状态码 我使用try-catch,但我只能接受消息。我怎么能只拿密码 还有别的。如果出现问题,是否会自动调用任何事件?卡尼能超越它吗 这是我的异步任务代码 protected HistoricalWeatherResponse doInBackground(String... params) { HistoricalWeatherResponse response = new HistoricalWeatherResponse();

嗨,我想在网络出现问题时使用HTTP状态码

我使用try-catch,但我只能接受消息。我怎么能只拿密码

还有别的。如果出现问题,是否会自动调用任何事件?卡尼能超越它吗

这是我的异步任务代码

 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枚举,该枚举具有处理状态代码的方法。