2013-11-24 92 views
1

我想使用java獲取一些天氣數據。我使用下面的Java API從wunderground.com使用java和wunderground API獲取天氣數據

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

獲取數據,他們給在其網站上的示例代碼工作好了(多特蒙德在德國)。但是,當我在美國將多特蒙德的密鑰更改爲波士頓時,我得到空指針錯誤。任何想法我可能做錯了什麼?請嘗試並留下意見/建議。謝謝!

代碼:

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

} 
+0

你會越來越'NullPointerException's如果'weatherStation'是'null'。你確定它是'WeatherStations.ALL'對象的一部分嗎?你嘗試過調試嗎? – t0mppa

+0

您爲波士頓嘗試了哪個站ID? – Reda

+0

KMABOSTO32 // WeatherStation weatherStation = WeatherStations.ALL.get(「KMABOSTO32」); – Ammar

回答

1

時退房Wunderground API的源代碼。

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

在封裝de.mbenning.weather.wunderground.api.domain有一個叫做電子氣象站類。在那裏您可以找到您可以在代碼中調用的所有氣象站的內容。 現在只有少數的人:

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

所有其他人將無法正常工作。

1

它的工作原理:您可以實例化在WUnderground上註冊的每個氣象站。 這是可能的設置站ID作爲構造器參數:

WeatherStation aWeatherStation = new WeatherStation("INORDRHE72"); 

HttpDataReaderService dataReader = new HttpDataReaderService(); 
dataReader.setWeatherStation(aWeatherStation); 

Double currentTemperature = dataReader.getCurrentData().getTemperature();