我得到了无法解析的日期BST异常

时间:2015-07-30 19:50:48

标签: java android timezone yahoo-weather-api

我打电话给雅虎天气api并获得了一个无法解析的日期BST例外。

service.refreshWeather("London, United Kingdom");

这是我想获取天气信息的位置。

public void refreshWeather(String location) {

    new AsyncTask<String, Void, Channel>() {
        @Override
        protected Channel doInBackground(String... strings) {

            String location = strings[0];

            Channel channel = loadCache(location);

            if (channel != null) {
                return channel;
            } else {
                channel = new Channel();
            }

            String YQL = String.format("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"%s\") and u='c'", location);

            String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));

            try {
                URL url = new URL(endpoint);

                URLConnection connection = url.openConnection();
                connection.setUseCaches(false);

                InputStream inputStream = connection.getInputStream();

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder result = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                JSONObject data = new JSONObject(result.toString());

                JSONObject queryResults = data.optJSONObject("query");

                int count = queryResults.optInt("count");

                if (count == 0) {
                    error = new LocationWeatherException("No weather information found for " + location);
                    return null;
                }

                JSONObject channelJSON = queryResults.optJSONObject("results").optJSONObject("channel");

                loadMetadata(location, channelJSON);

                channel.populate(channelJSON);

                cacheWeatherData(channel);

                return channel;

            } catch (Exception e) {
                error = e;
            }

            return null;
        }

        @Override
        protected void onPostExecute(Channel channel) {

            if (channel == null && error != null) {
                callback.serviceFailure(error);
            } else {
                callback.serviceSuccess(channel);
            }

        }

    }.execute(location);
}

private void loadMetadata(String location, JSONObject channelJSON) throws ParseException, JSONException {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm aa z", Locale.UK);

    Date lastBuildDate = sdf.parse(channelJSON.optString("lastBuildDate"));

    long ttl = channelJSON.optLong("ttl");

    long expiration = ttl * 60 * 1000 + lastBuildDate.getTime();

    channelJSON.put("expiration", expiration);
    channelJSON.put("requestLocation", location);
}

private void cacheWeatherData(Channel channel) {
    FileOutputStream outputStream;

    try {
        outputStream = context.openFileOutput(CACHED_WEATHER_FILE, Context.MODE_PRIVATE);
        outputStream.write(channel.toJSON().toString().getBytes());
        outputStream.close();

    } catch (IOException e) {
        // IGNORE: file save operation failed
    }
}

但是当我运行应用程序时,我得到了无法解析的日期BST异常(在偏移25处) 有人能帮助我吗?我是一个机器人的初学者。

0 个答案:

没有答案