当我传递带有Retrofit2
的对象时,我收到一个空响应。我收到带有URL的请求,但看到的内容为null。我不知道为什么...
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)
更新
我的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
答案 0 :(得分:0)
我建议您使用此在线服务生成 POJO 。 有关信息,您的对象名称getter和setter应该与 JSON 中各个元素的名称相同。我假设您正在使用 Gson ,因为您正在使用GsonConverterFactory来解析响应。由于JSON响应不完整,但是我在这里发布了它的样子。您遇到的情况不一样,缺少一些元素。
样本响应:
{
"location": {
"name": "Glasgow",
"region": "Glasgow City",
"country": "United Kingdom",
"lat": 55.86,
"lon": -4.25
}
}
样本POJO应为以上响应应为:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Location {
@SerializedName("name")
@Expose
private String name;
@SerializedName("region")
@Expose
private String region;
@SerializedName("country")
@Expose
private String country;
@SerializedName("lat")
@Expose
private double lat;
@SerializedName("lon")
@Expose
private double lon;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLon() {
return lon;
}
public void setLon(double lon) {
this.lon = lon;
}
}
-
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("location")
@Expose
private Location location;
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
}
-
这是一本很好的书。希望对您有帮助。
http://www.jsonschema2pojo.org/
https://github.com/codepath/android_guides/wiki/Consuming-APIs-with-Retrofit
答案 1 :(得分:0)
我已经使用pojo进行了转换。但是,当我导入它时,我在gson类和更多类中得到了一个错误,因此我不得不对其进行一些修改。这就是结果。名称都一样:
enter code here
public class WeatherInfoResponse {
public Location LocationObjectWeather;
public Current CurrentObjectWeather;
public Location getLocation() {
return LocationObjectWeather;
}
public Current getCurrent() {
return CurrentObjectWeather;
}
public void setLocation(Location LocationObjectWeather) {
this.LocationObjectWeather = LocationObjectWeather;
}
public void setCurrent(Current CurrentObjectWeather) {
this.CurrentObjectWeather = CurrentObjectWeather;
}
}
enter code here
package com.example.weatherapi;
public class Location {
private String name;
private String region;
private String country;
private float lat;
private float lon;
private String tz_id;
private float localtime_epoch;
private String localtime;
public String getName() {
return name;
}
public String getRegion() {
return region;
}
public String getCountry() {
return country;
}
public float getLat() {
return lat;
}
public float getLon() {
return lon;
}
public String getTz_id() {
return tz_id;
}
public float getLocaltime_epoch() {
return localtime_epoch;
}
public String getLocaltime() {
return localtime;
}
// Setter Methods
public void setName(String name) {
this.name = name;
}
public void setRegion(String region) {
this.region = region;
}
public void setCountry(String country) {
this.country = country;
}
public void setLat(float lat) {
this.lat = lat;
}
public void setLon(float lon) {
this.lon = lon;
}
public void setTz_id(String tz_id) {
this.tz_id = tz_id;
}
public void setLocaltime_epoch(float localtime_epoch) {
this.localtime_epoch = localtime_epoch;
}
public void setLocaltime(String localtime) {
this.localtime = localtime;
}
}
`package com.example.weatherapi;
public class Current {
private float last_updated_epoch;
private String last_updated;
private float temp_c;
private float temp_f;
private float is_day;
Condition ConditionObject;
private float wind_mph;
private float wind_kph;
private float wind_degree;
private String wind_dir;
private float pressure_mb;
private float pressure_in;
private float precip_mm;
private float precip_in;
private float humidity;
private float cloud;
private float feelslike_c;
private float feelslike_f;
private float vis_km;
private float vis_miles;
private float uv;
private float gust_mph;
private float gust_kph;
public float getLast_updated_epoch() {
return last_updated_epoch;
}
public String getLast_updated() {
return last_updated;
}
public float getTemp_c() {
return temp_c;
}
public float getTemp_f() {
return temp_f;
}
public float getIs_day() {
return is_day;
}
public Condition getCondition() {
return ConditionObject;
}
public float getWind_mph() {
return wind_mph;
}
public float getWind_kph() {
return wind_kph;
}
public float getWind_degree() {
return wind_degree;
}
public String getWind_dir() {
return wind_dir;
}
public float getPressure_mb() {
return pressure_mb;
}
public float getPressure_in() {
return pressure_in;
}
public float getPrecip_mm() {
return precip_mm;
}
public float getPrecip_in() {
return precip_in;
}
public float getHumidity() {
return humidity;
}
public float getCloud() {
return cloud;
}
public float getFeelslike_c() {
return feelslike_c;
}
public float getFeelslike_f() {
return feelslike_f;
}
public float getVis_km() {
return vis_km;
}
public float getVis_miles() {
return vis_miles;
}
public float getUv() {
return uv;
}
public float getGust_mph() {
return gust_mph;
}
public float getGust_kph() {
return gust_kph;
}
// Setter Methods
public void setLast_updated_epoch(float last_updated_epoch) {
this.last_updated_epoch = last_updated_epoch;
}
public void setLast_updated(String last_updated) {
this.last_updated = last_updated;
}
public void setTemp_c(float temp_c) {
this.temp_c = temp_c;
}
public void setTemp_f(float temp_f) {
this.temp_f = temp_f;
}
public void setIs_day(float is_day) {
this.is_day = is_day;
}
public void setCondition(Condition conditionObject) {
this.ConditionObject = conditionObject;
}
public void setWind_mph(float wind_mph) {
this.wind_mph = wind_mph;
}
public void setWind_kph(float wind_kph) {
this.wind_kph = wind_kph;
}
public void setWind_degree(float wind_degree) {
this.wind_degree = wind_degree;
}
public void setWind_dir(String wind_dir) {
this.wind_dir = wind_dir;
}
public void setPressure_mb(float pressure_mb) {
this.pressure_mb = pressure_mb;
}
public void setPressure_in(float pressure_in) {
this.pressure_in = pressure_in;
}
public void setPrecip_mm(float precip_mm) {
this.precip_mm = precip_mm;
}
public void setPrecip_in(float precip_in) {
this.precip_in = precip_in;
}
public void setHumidity(float humidity) {
this.humidity = humidity;
}
public void setCloud(float cloud) {
this.cloud = cloud;
}
public void setFeelslike_c(float feelslike_c) {
this.feelslike_c = feelslike_c;
}
public void setFeelslike_f(float feelslike_f) {
this.feelslike_f = feelslike_f;
}
public void setVis_km(float vis_km) {
this.vis_km = vis_km;
}
public void setVis_miles(float vis_miles) {
this.vis_miles = vis_miles;
}
public void setUv(float uv) {
this.uv = uv;
}
public void setGust_mph(float gust_mph) {
this.gust_mph = gust_mph;
}
public void setGust_kph(float gust_kph) {
this.gust_kph = gust_kph;
}
}