Я получаю нулевой ответ, когда передал объект с Retrofit2
. Я получаю запрос с URL-адресом, но, видя содержимое, это пустое значение. Я не знаю почему ...
public interface WeatherAPI {
@GET("current.json")
@Headers("Accept:application/json")
Call<WeatherInfoResponse> getWeather(@Query("key") String key,@Query("q") String location);
}
retrofit = new Retrofit.Builder()
.baseUrl("https://api.apixu.com/v1/")
.client(client)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
weatherAPI =retrofit.create(WeatherAPI.class);
weatherGET(KEY, locationCurrent);
}
void weatherGET(String key, final String location){
Call<WeatherInfoResponse> call = weatherAPI.getWeather(key,location);
call.enqueue(new Callback<WeatherInfoResponse>() {
@Override
public void onResponse(Call <WeatherInfoResponse> call, Response<WeatherInfoResponse> response) {
weatherInfoResponse = response.body();
Log.d("Temp",String.valueOf(weatherInfoResponse));
Log.d("%%%%%","Entrada");
float temp = weatherInfoResponse.getCurrent().getTemp_c();
}
@Override
public void onFailure(Call<WeatherInfoResponse> call, Throwable t) {
Log.d("%%%%%","Salida");
}
});
}
Это LogCat моей ошибки
ava.lang.NullPointerException: Attempt to invoke virtual method 'float com.example.weatherapi.Current.getTemp_c()' on a null object reference
at com.example.weatherapi.MainActivity$1.onResponse(MainActivity.java:65)
UPDATE
Мой класс POJO, надеюсь, он поможет
public class WeatherInfoResponse {
public Location LocationObjectWeather;
public Current CurrentObjectWeather; // Getter Methods public
Location getLocation() { return LocationObjectWeather; } public
Current getCurrent() { return CurrentObjectWeather; } // Setter
// Methods
public void setLocation(Location LocationObjectWeather) {
this.LocationObjectWeather = LocationObjectWeather;
}
public void setCurrent(Current CurrentObjectWeather) {
this.CurrentObjectWeather = CurrentObjectWeather;
}
}
а вот пример ответа
{
"location": {
"name": "Glasgow",
"region": "Glasgow City",
"country": "United Kingdom",
"lat": 55.86,
"lon": -4.25
}
}
Any help would be appreciated, Thank you in advance