2013-11-24 6 views
1

java를 사용하여 날씨 데이터를 가져 오려고합니다. 나는 그들이 자신의 웹 사이트에 제공 예제 코드 (독일 도르트문트) 괜찮 작동 wunderground.comjava 및 wunderground api를 사용하여 기상 데이터 가져 오기

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

에서 데이터를 가져 오는에 대해 다음 자바 API를 사용하고 있습니다. 그러나 미국에서 도르트문트에서 보스톤으로 키를 변경하면 널 포인터 오류가 발생합니다. 내가 뭘 잘못했는지 알기나 해? 그것을 시도하고 의견/조언을 남겨주세요. 감사!

코드 :

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

합니다. 'WeatherStations.ALL'객체의 일부인지 확신 할 수 있습니까? 디버깅을 시도 했습니까? – t0mppa

+0

보스턴에서 어떤 Station ID를 시도 했습니까? – Reda

+0

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

답변

1

체크 아웃 Wunderground API의 소스 코드입니다. de.mbenning.weather.wunderground.api.domain 패키지 에서

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

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

모든 다른 사람이 작동하지 않습니다 은 지금 몇 사람이 있습니다.

1

작동 : WUnderground에 등록 된 모든 기상 관측소를 인스턴스화 할 수 있습니다. 이 생성자 매개 변수로 스테이션 ID를 설정할 수있다 :``null` weatherStation` 경우 당신은`NullPointerException`s을 받고있을 것

WeatherStation aWeatherStation = new WeatherStation("INORDRHE72"); 

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

Double currentTemperature = dataReader.getCurrentData().getTemperature(); 
관련 문제