我得到了这样一个非常奇怪的问题。我已经创建了使用此API的天气应用程序。问题是,当我打电话给“CONDITION”对象时,比我得到一个空指针异常。但是当我调用另一个对象时,比一切都好!有点奇怪。在2周内这个API工作正常,但从昨天开始,不。任何人都可以解释我,为什么它是这样的,我必须做的。因为应用程序已经完成了,而且我正在创建另一个应用程序,并且我不想仅仅因为这个问题而重写这个应用程序。JSON Null Poiner异常
我把我的回应放在另一个应用程序中,检查可能是问题出在我的应用程序中,而不是在API中。这里是代码..
API CLASS:
public interface WeatherAPI {
@GET("/v1/current.json?key=fe1c9cd3fd2d4b189c010010172505")
Call<Weather> getForecastWeather(
@Query("q") String cityName
// @Query("days")Integer days
);
}
控制器类:
public class Controller {
static final String BASE_WEATHER_URL = "https://api.apixu.com/";
public static WeatherAPI getApi(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_WEATHER_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
WeatherAPI weatherAPI = retrofit.create(WeatherAPI.class);
return weatherAPI;
}
}
主要活动类:
mWeatherAPI = Controller.getApi();
mWeatherAPI.getForecastWeather("Paris").enqueue(new Callback<Weather>() {
@Override
public void onResponse(Call<Weather> call, Response<Weather> response) {
String description = response.body().getCurrent().getCondition().getIcon();
Log.e("TAG", "description: " + description);
}
@Override
public void onFailure(Call<Weather> call, Throwable t) {
Log.e("TAG", "onFailure: " + t.getMessage());
}
});
您需要在解析json响应的地方添加代码。 – Sac
没有堆栈跟踪,没有代码=>没有答案(因为这是不可能的,除非涉及一些水晶球) – 2017-05-25 09:02:59
@Sac补充说。检查代码,请参阅 –