我正在使用此链接的天气示例:
< https://github.com/vyshane/rex-weather>它工作正常。
我想获取currentweather
的天气图标,但我收到致命异常错误。这是我的代码。
public class WeatherService {
// We are implementing against version 2.5 of the Open Weather Map web service.
private static final String WEB_SERVICE_BASE_URL = "http://api.openweathermap.org/data/2.5";
private static final String API_KEY = "my-key";
private final OpenWeatherMapWebService mWebService;
public WeatherService() {
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestInterceptor.RequestFacade request) {
request.addHeader("Accept", "application/json");
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(WEB_SERVICE_BASE_URL)
.setRequestInterceptor(requestInterceptor)
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
mWebService = restAdapter.create(OpenWeatherMapWebService.class);
}
private interface OpenWeatherMapWebService {
//@GET("/weather?q=Dubai&units=metric&apikey=" + API_KEY)
@GET("/weather?units=metric&apikey=" + API_KEY)
Observable<CurrentWeatherDataEnvelope> fetchCurrentWeather(@Query("lon") double longitude,
@Query("lat") double latitude);
@GET("/forecast/daily?units=metric&cnt=7&apikey=" + API_KEY)
// /@GET("/forecast/daily?q=Dubai&units=metric&cnt=7&apikey=" + API_KEY)
Observable<WeatherForecastListDataEnvelope> fetchWeatherForecasts(
@Query("lon") double longitude, @Query("lat") double latitude);
}
public Observable<CurrentWeather> fetchCurrentWeather(final double longitude,
final double latitude) {
return mWebService.fetchCurrentWeather(longitude, latitude)
.flatMap(new Func1<CurrentWeatherDataEnvelope,
Observable<? extends CurrentWeatherDataEnvelope>>() {
// Error out if the request was not successful.
@Override
public Observable<? extends CurrentWeatherDataEnvelope> call(
final CurrentWeatherDataEnvelope data) {
return data.filterWebServiceErrors();
}
}).map(new Func1<CurrentWeatherDataEnvelope, CurrentWeather>() {
// Parse the result and build a CurrentWeather object.
@Override
public CurrentWeather call(final CurrentWeatherDataEnvelope data) {
/*return new CurrentWeather(data.locationName, data.timestamp,
data.weather.get(0).description, data.main.temp,
data.main.temp_min, data.main.temp_max);*/
return new CurrentWeather(data.locationName,data.id ,data.timestamp,
data.weather.get(0).description, data.main.temp,
data.main.temp_min, data.main.temp_max , data.sys.sunrise,
data.sys.sunset);
}
});
}
public Observable<List<WeatherForecast>> fetchWeatherForecasts(final double longitude,
final double latitude) {
return mWebService.fetchWeatherForecasts(longitude, latitude)
.flatMap(new Func1<WeatherForecastListDataEnvelope,
Observable<? extends WeatherForecastListDataEnvelope>>() {
// Error out if the request was not successful.
@Override
public Observable<? extends WeatherForecastListDataEnvelope> call(
final WeatherForecastListDataEnvelope listData) {
return listData.filterWebServiceErrors();
}
}).map(new Func1<WeatherForecastListDataEnvelope, List<WeatherForecast>>() {
// Parse the result and build a list of WeatherForecast objects.
@Override
public List<WeatherForecast> call(final WeatherForecastListDataEnvelope listData) {
final ArrayList<WeatherForecast> weatherForecasts =
new ArrayList<>();
for (WeatherForecastListDataEnvelope.ForecastDataEnvelope data : listData.list) {
final WeatherForecast weatherForecast = new WeatherForecast(
/*listData.city.name, data.timestamp, data.weather.get(0).description,
data.temp.min, data.temp.max);*/
listData.city.name,data.id, data.timestamp, data.weather.get(0).description,
data.temp.min, data.temp.max, data.sys.sunrise, data.sys.sunset);
weatherForecasts.add(weatherForecast);
}
return weatherForecasts;
}
});
}
/**
* Base class for results returned by the weather web service.
*/
private class WeatherDataEnvelope {
@SerializedName("cod")
private int httpCode;
class Weather {
public String description;
}
public SYS sys;
class SYS
{public long sunrise;
public long sunset;
}
/**
* The web service always returns a HTTP header code of 200 and communicates errors
* through a 'cod' field in the JSON payload of the response body.
*/
public Observable filterWebServiceErrors() {
if (httpCode == 200) {
return Observable.just(this);
} else {
return Observable.error(
new HttpException("There was a problem fetching the weather data."));
}
}
}
/**
* Data structure for current weather results returned by the web service.
*/
private class CurrentWeatherDataEnvelope extends WeatherDataEnvelope {
@SerializedName("name")
public String locationName;
@SerializedName("dt")
public long timestamp;
public ArrayList<Weather> weather;
public Main main;
public int id;
class Main {
public float temp;
public float temp_min;
public float temp_max;
}
}
/**
* Data structure for weather forecast results returned by the web service.
*/
private class WeatherForecastListDataEnvelope extends WeatherDataEnvelope {
public Location city;
public ArrayList<ForecastDataEnvelope> list;
class Location {
public String name;
}
class ForecastDataEnvelope {
@SerializedName("dt")
public long timestamp;
public Temperature temp;
public ArrayList<Weather> weather;
public int id;
public SYS sys;
}
class Temperature {
public float min;
public float max;
}
class sys
{public long sunrise;
public long sunsit;
}
}
}
`
以下是错误消息:
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4953)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: rx.exceptions.OnErrorFailedException: Error occurred when trying to propagate error to Observer.onError
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:194)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:191)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:162)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4953)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: rx.exceptions.CompositeException: 2 exceptions occurred.
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:194)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:191)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:162)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4953)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: rx.exceptions.CompositeException$CompositeExceptionCausalChain: Chain of Causes for CompositeException In Order Received =>
at android.util.Log.getStackTraceString(Log.java:395)
at android.util.Slog.e(Slog.java:151)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:74)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:66)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4953)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at mu.node.rexweather.app.Services.WeatherService$4.call(WeatherService.java:114)
at mu.node.rexweather.app.Services.WeatherService$4.call(WeatherService.java:101)
at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
at rx.internal.operators.OperatorMerge$MergeSubscriber.emitScalar(OperatorMerge.java:477)
at rx.internal.operators.OperatorMerge$MergeSubscriber.tryEmit(OperatorMerge.java:435)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:228)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:142)
at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
at retrofit.RxSupport$2.run(RxSupport.java:56)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:856)
Caused by: rx.exceptions.OnErrorThrowable$OnNextValue: OnError while emitting onNext value: mu.node.rexweather.app.Services.WeatherService$WeatherForecastListDataEnvelope.class
at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:188)
at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:56)
at rx.internal.operators.OperatorMerge$MergeSubscriber.emitScalar(OperatorMerge.java:477)
at rx.internal.operators.OperatorMerge$MergeSubscriber.tryEmit(OperatorMerge.java:435)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:228)
at rx.internal.operators.OperatorMerge$MergeSubscriber.onNext(OperatorMerge.java:142)
at rx.internal.operators.OperatorMap$1.onNext(OperatorMap.java:54)
at retrofit.RxSupport$2.run(RxSupport.java:56)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:856)
请任何帮助。