gpt4 book ai didi

java - 使用 java 和 wunderground api 获取天气数据

转载 作者:行者123 更新时间:2023-11-30 04:08:36 25 4
gpt4 key购买 nike

我正在尝试使用 java 获取一些天气数据。我正在使用以下 java api 从 wunderground.com 获取数据

https://code.google.com/p/wunderground-core/

他们在网站上提供的示例代码适用于(德国多特蒙德)。但是,当我将 key 从多特蒙德更改为美国的波士顿时,出现空指针错误。知道我可能做错了什么吗?请尝试一下并留下评论/建议。谢谢!

代码:

import de.mbenning.weather.wunderground.api.domain.DataSet;
import de.mbenning.weather.wunderground.api.domain.WeatherStation;
import de.mbenning.weather.wunderground.api.domain.WeatherStations;
import de.mbenning.weather.wunderground.impl.services.HttpDataReaderService;


public class weather {

public static void main(String[] args)
{

// create a instance of a wunderground data reader
HttpDataReaderService dataReader = new HttpDataReaderService();

// select a wunderground weather station (ID "INORDRHE72" = Dortmund-Mengede)
WeatherStation weatherStation = WeatherStations.ALL.get("INORDRHE72");
// KMABOSTO22 is the ID for Boston South end
//WeatherStation weatherStation = WeatherStations.ALL.get("KMABOSTO32");

// set selected weather station to data reader
dataReader.setWeatherStation(weatherStation);

// get current (last) weather data set from selected station
DataSet current = dataReader.getCurrentData();

// print selected weather station ID
System.out.println(weatherStation.getStationId());

// print city, state and country of weather station
System.out.println(weatherStation.getCity() + " " + weatherStation.getState() + " " + weatherStation.getCountry());

//`enter code here` print datetime of measure and temperature ...
System.out.println(current.getDateTime() + " " + current.getTemperature());
}

}

最佳答案

查看 Wunderground API 的源代码。

svn checkout http://wunderground-core.googlecode.com/svn/trunk/ wunderground-core-read-only

de.mbenning.weather.wunderground.api.domain包中,有一个名为WeatherStations的类。在那里您将找到可以在代码中调用的所有气象站的内容。目前只有几个:

public static final Map<String, WeatherStation> ALL = new HashMap<String, WeatherStation>();
static {
ALL.put("INRWKLEV2", INRWKLEV2_KLEVE);
ALL.put("INORDRHE110", INORDRHE110_GOCH);
ALL.put("IDRENTHE48", IDRENTHE48_COEVORDEN);
ALL.put("IZEELAND13", IZEELAND13_GOES);
ALL.put("INORDRHE72", INORDRHE72_DORTMUND);
ALL.put("INOORDBR35", INOORDBR35_BOXMEER);
};

所有其他的都不起作用。

关于java - 使用 java 和 wunderground api 获取天气数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20177515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com