我得到了這樣一個非常奇怪的問題。我已經創建了使用此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補充說。檢查代碼,請參閱 –