API 数据未转换为 java 对象
API data not getting converted to java object
我正在使用一个 openweathermap API,这是一个电话 API,来获取当前和未来 7 天的天气详细信息。我能够检索所有非嵌套的静态数据,但对于嵌套的数据(当前、每日等),它表示空指针异常。我已经为嵌套数据定义了所有 classes,它仍然没有将嵌套数据存储在 pojo class
中
这就是数据在 API 响应中的样子
{
"lat": 55.25,
"lon": 23.99,
"timezone": "Europe/Vilnius",
"timezone_offset": 10800,
"current": {
"dt": 1652008177,
"sunrise": 1651976922,
"sunset": 1652033543,
"temp": 11.67,
"feels_like": 10.62,
"pressure": 1023,
"humidity": 66,
"dew_point": 5.54,
"uvi": 1.26,
"clouds": 100,
"visibility": 10000,
"wind_speed": 3.06,
"wind_deg": 34,
"wind_gust": 4.8,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"rain": {
"1h": 0.12
}
},
"daily": [
{
"dt": 1652004000,
"sunrise": 1651976922,
"sunset": 1652033543,
"moonrise": 1651996080,
"moonset": 1651970160,
"moon_phase": 0.23,
"temp": {
"day": 11.56,
"min": 4.9,
"max": 12.65,
"night": 4.9,
"eve": 11.07,
"morn": 8.06
},
"feels_like": {
"day": 10.49,
"night": 1.15,
"eve": 9.75,
"morn": 6.91
},
"pressure": 1023,
"humidity": 66,
"dew_point": 5.43,
"wind_speed": 6.67,
"wind_deg": 17,
"wind_gust": 12.61,
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
],
"clouds": 100,
"pop": 0.2,
"uvi": 2.88
},
......
{
"dt": 1652608800,
"sunrise": 1652580930,
"sunset": 1652639127,
"moonrise": 1652636520,
"moonset": 1652580000,
"moon_phase": 0.47,
"temp": {
"day": 13.65,
"min": 6.22,
"max": 13.65,
"night": 10.34,
"eve": 13.3,
"morn": 6.24
},
"feels_like": {
"day": 12.69,
"night": 9.44,
"eve": 12.38,
"morn": 4.12
},
"pressure": 1007,
"humidity": 62,
"dew_point": 6.58,
"wind_speed": 4.15,
"wind_deg": 280,
"wind_gust": 5.23,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": 100,
"pop": 0.2,
"rain": 0.12,
"uvi": 5
}
]
}
这是我主 activity(FetchOutfitInputDetails.java) 中的代码,我从那里调用 API
WeatherAPIInterface weatherAPIInterface= RetrofitInstance.getRetrofit().create(WeatherAPIInterface.class);
weatherAPIInterface.getWeather(lat,lon,"minutely,hourly","metric","en",apiKey).enqueue(new Callback<WeatherPojo>() {
@Override
public void onResponse(Call<WeatherPojo> call, Response<WeatherPojo> response) {
if(response.code()!=404){
Toast.makeText(getContext(),"list present",Toast.LENGTH_SHORT).show();
WeatherPojo pojo=response.body();
Log.v("Aaaaaa",pojo.getCurrent().toString());
}else{
Toast.makeText(getContext(),"LIst Empty",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<WeatherPojo> call, Throwable t) {
}
}
这是我的改造实例class
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitInstance {
private static Retrofit retrofit;
private static final String BASEURL ="https://api.openweathermap.org/data/2.5/";
public static Retrofit getRetrofit() {
if(retrofit==null ){
retrofit=new Retrofit.Builder().
baseUrl(BASEURL).
addConverterFactory(GsonConverterFactory.create()).
build();
}return retrofit;
}
}
天气预报class
import java.util.ArrayList;
public class WeatherPojo {
private float lat;
private float lon;
private String timezone;
private float timezone_offset;
CurrentWeatherPojo CurrentObject;
ArrayList<DailyPojo> daily = new ArrayList<DailyPojo>();
// Getter Methods
public float getLat() {
return lat;
}
public float getLon() {
return lon;
}
public String getTimezone() {
return timezone;
}
public float getTimezone_offset() {
return timezone_offset;
}
public CurrentWeatherPojo getCurrent() {
return CurrentObject;
}
// Setter Methods
public void setLat(float lat) {
this.lat = lat;
}
public void setLon(float lon) {
this.lon = lon;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
public void setTimezone_offset(float timezone_offset) {
this.timezone_offset = timezone_offset;
}
public ArrayList<DailyPojo> getDaily() {
return daily;
}
public void setDaily(ArrayList<DailyPojo> daily) {
this.daily = daily;
}
public void setCurrent(CurrentWeatherPojo currentObject) {
this.CurrentObject = currentObject;
}
}
当前天气 pojo class,用于 api 数据中嵌套的当前天气
package com.example.clothme.WeatherAPI;
import java.util.ArrayList;
public class CurrentWeatherPojo {
private float dt;
private float sunrise;
private float sunset;
private float temp;
private float feels_like;
private float pressure;
private float humidity;
private float dew_point;
private float uvi;
private float clouds;
private float visibility;
private float wind_speed;
private float wind_deg;
private float wind_gust;
ArrayList< DailyPojo.weath > weather = new ArrayList < DailyPojo.weath>();
// Getter Methods
public CurrentWeatherPojo(float dt, float sunrise, float sunset, float temp, float feels_like, float pressure, float humidity, float dew_point, float uvi, float clouds, float visibility, float wind_speed, float wind_deg, float wind_gust, ArrayList<DailyPojo.weath> weather) {
this.dt = dt;
this.sunrise = sunrise;
this.sunset = sunset;
this.temp = temp;
this.feels_like = feels_like;
this.pressure = pressure;
this.humidity = humidity;
this.dew_point = dew_point;
this.uvi = uvi;
this.clouds = clouds;
this.visibility = visibility;
this.wind_speed = wind_speed;
this.wind_deg = wind_deg;
this.wind_gust = wind_gust;
this.weather = weather;
}
public float getDt() {
return dt;
}
public float getSunrise() {
return sunrise;
}
public float getSunset() {
return sunset;
}
public float getTemp() {
return temp;
}
public float getFeels_like() {
return feels_like;
}
public float getPressure() {
return pressure;
}
public float getHumidity() {
return humidity;
}
public float getDew_point() {
return dew_point;
}
public float getUvi() {
return uvi;
}
public float getClouds() {
return clouds;
}
public float getVisibility() {
return visibility;
}
public float getWind_speed() {
return wind_speed;
}
public float getWind_deg() {
return wind_deg;
}
public float getWind_gust() {
return wind_gust;
}
// Setter Methods
public void setDt(float dt) {
this.dt = dt;
}
public void setSunrise(float sunrise) {
this.sunrise = sunrise;
}
public void setSunset(float sunset) {
this.sunset = sunset;
}
public void setTemp(float temp) {
this.temp = temp;
}
public void setFeels_like(float feels_like) {
this.feels_like = feels_like;
}
public void setPressure(float pressure) {
this.pressure = pressure;
}
public void setHumidity(float humidity) {
this.humidity = humidity;
}
public void setDew_point(float dew_point) {
this.dew_point = dew_point;
}
public void setUvi(float uvi) {
this.uvi = uvi;
}
public void setClouds(float clouds) {
this.clouds = clouds;
}
public void setVisibility(float visibility) {
this.visibility = visibility;
}
public void setWind_speed(float wind_speed) {
this.wind_speed = wind_speed;
}
public void setWind_deg(float wind_deg) {
this.wind_deg = wind_deg;
}
public void setWind_gust(float wind_gust) {
this.wind_gust = wind_gust;
}
}
另一个 class 用于更多嵌套数据
package com.example.clothme.WeatherAPI;
import java.util.ArrayList;
public class DailyPojo {
private float dt;
private float sunrise;
private float sunset;
private float moonrise;
private float moonset;
private float moon_phase;
Temp TempObject;
Feels_like Feels_likeObject;
private float pressure;
private float humidity;
private float dew_point;
private float wind_speed;
private float wind_deg;
private float wind_gust;
ArrayList< weath > weather = new ArrayList < weath > ();
private float clouds;
private float pop;
private float uvi;
public DailyPojo(float dt, float sunrise, float sunset, float moonrise, float moonset, float moon_phase, Temp tempObject, Feels_like feels_likeObject, float pressure, float humidity, float dew_point, float wind_speed, float wind_deg, float wind_gust, ArrayList<weath> weather, float clouds, float pop, float uvi) {
this.dt = dt;
this.sunrise = sunrise;
this.sunset = sunset;
this.moonrise = moonrise;
this.moonset = moonset;
this.moon_phase = moon_phase;
TempObject = tempObject;
Feels_likeObject = feels_likeObject;
this.pressure = pressure;
this.humidity = humidity;
this.dew_point = dew_point;
this.wind_speed = wind_speed;
this.wind_deg = wind_deg;
this.wind_gust = wind_gust;
this.weather = weather;
this.clouds = clouds;
this.pop = pop;
this.uvi = uvi;
}
// Getter Methods
public float getDt() {
return dt;
}
public float getSunrise() {
return sunrise;
}
public float getSunset() {
return sunset;
}
public float getMoonrise() {
return moonrise;
}
public float getMoonset() {
return moonset;
}
public float getMoon_phase() {
return moon_phase;
}
public Temp getTemp() {
return TempObject;
}
public Feels_like getFeels_like() {
return Feels_likeObject;
}
public float getPressure() {
return pressure;
}
public float getHumidity() {
return humidity;
}
public float getDew_point() {
return dew_point;
}
public float getWind_speed() {
return wind_speed;
}
public float getWind_deg() {
return wind_deg;
}
public float getWind_gust() {
return wind_gust;
}
public float getClouds() {
return clouds;
}
public float getPop() {
return pop;
}
public float getUvi() {
return uvi;
}
// Setter Methods
public void setDt(float dt) {
this.dt = dt;
}
public void setSunrise(float sunrise) {
this.sunrise = sunrise;
}
public void setSunset(float sunset) {
this.sunset = sunset;
}
public void setMoonrise(float moonrise) {
this.moonrise = moonrise;
}
public void setMoonset(float moonset) {
this.moonset = moonset;
}
public void setMoon_phase(float moon_phase) {
this.moon_phase = moon_phase;
}
public void setTemp(Temp tempObject) {
this.TempObject = tempObject;
}
public void setFeels_like(Feels_like feels_likeObject) {
this.Feels_likeObject = feels_likeObject;
}
public void setPressure(float pressure) {
this.pressure = pressure;
}
public void setHumidity(float humidity) {
this.humidity = humidity;
}
public void setDew_point(float dew_point) {
this.dew_point = dew_point;
}
public void setWind_speed(float wind_speed) {
this.wind_speed = wind_speed;
}
public void setWind_deg(float wind_deg) {
this.wind_deg = wind_deg;
}
public void setWind_gust(float wind_gust) {
this.wind_gust = wind_gust;
}
public void setClouds(float clouds) {
this.clouds = clouds;
}
public void setPop(float pop) {
this.pop = pop;
}
public void setUvi(float uvi) {
this.uvi = uvi;
}
public static class Feels_like {
private float day;
private float night;
private float eve;
private float morn;
public Feels_like(float day, float night, float eve, float morn) {
this.day = day;
this.night = night;
this.eve = eve;
this.morn = morn;
}
// Getter Methods
public float getDay() {
return day;
}
public float getNight() {
return night;
}
public float getEve() {
return eve;
}
public float getMorn() {
return morn;
}
// Setter Methods
public void setDay(float day) {
this.day = day;
}
public void setNight(float night) {
this.night = night;
}
public void setEve(float eve) {
this.eve = eve;
}
public void setMorn(float morn) {
this.morn = morn;
}
}
public static class Temp {
private float day;
private float min;
private float max;
private float night;
private float eve;
private float morn;
public Temp(float day, float min, float max, float night, float eve, float morn) {
this.day = day;
this.min = min;
this.max = max;
this.night = night;
this.eve = eve;
this.morn = morn;
}
// Getter Methods
public float getDay() {
return day;
}
public float getMin() {
return min;
}
public float getMax() {
return max;
}
public float getNight() {
return night;
}
public float getEve() {
return eve;
}
public float getMorn() {
return morn;
}
// Setter Methods
public void setDay(float day) {
this.day = day;
}
public void setMin(float min) {
this.min = min;
}
public void setMax(float max) {
this.max = max;
}
public void setNight(float night) {
this.night = night;
}
public void setEve(float eve) {
this.eve = eve;
}
public void setMorn(float morn) {
this.morn = morn;
}
}
public static class weath{
private float id;
private String main;
private String description;
private String icon;
public weath(float id, String main, String description, String icon) {
this.id = id;
this.main = main;
this.description = description;
this.icon = icon;
}
// Getter Methods
public float getId() {
return id;
}
public String getMain() {
return main;
}
public String getDescription() {
return description;
}
public String getIcon() {
return icon;
}
// Setter Methods
public void setId(float id) {
this.id = id;
}
public void setMain(String main) {
this.main = main;
}
public void setDescription(String description) {
this.description = description;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
}
显示的错误消息
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clothme, PID: 25632
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.clothme.Fragments.FetchOutfitInputDetails.onResponse(FetchOutfitInputDetails.java:193)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall.lambda$onResponse[=18=]$retrofit2-DefaultCallAdapterFactory$ExecutorCallbackCall(DefaultCallAdapterFactory.java:89)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$$ExternalSyntheticLambda1.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8030)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
这是main中的错误行activity(FetchOutfitInputDetails.java)
Log.v("Aaaaaa",pojo.getCurrent().toString());
尝试将您的字段名称 CurrentObject
更改为 current
,使其与 json 字段的名称匹配
public class WeatherPojo {
// CurrentWeatherPojo CurrentObject;
CurrentWeatherPojo current;
}
我正在使用一个 openweathermap API,这是一个电话 API,来获取当前和未来 7 天的天气详细信息。我能够检索所有非嵌套的静态数据,但对于嵌套的数据(当前、每日等),它表示空指针异常。我已经为嵌套数据定义了所有 classes,它仍然没有将嵌套数据存储在 pojo class
中这就是数据在 API 响应中的样子
{
"lat": 55.25,
"lon": 23.99,
"timezone": "Europe/Vilnius",
"timezone_offset": 10800,
"current": {
"dt": 1652008177,
"sunrise": 1651976922,
"sunset": 1652033543,
"temp": 11.67,
"feels_like": 10.62,
"pressure": 1023,
"humidity": 66,
"dew_point": 5.54,
"uvi": 1.26,
"clouds": 100,
"visibility": 10000,
"wind_speed": 3.06,
"wind_deg": 34,
"wind_gust": 4.8,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"rain": {
"1h": 0.12
}
},
"daily": [
{
"dt": 1652004000,
"sunrise": 1651976922,
"sunset": 1652033543,
"moonrise": 1651996080,
"moonset": 1651970160,
"moon_phase": 0.23,
"temp": {
"day": 11.56,
"min": 4.9,
"max": 12.65,
"night": 4.9,
"eve": 11.07,
"morn": 8.06
},
"feels_like": {
"day": 10.49,
"night": 1.15,
"eve": 9.75,
"morn": 6.91
},
"pressure": 1023,
"humidity": 66,
"dew_point": 5.43,
"wind_speed": 6.67,
"wind_deg": 17,
"wind_gust": 12.61,
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04d"
}
],
"clouds": 100,
"pop": 0.2,
"uvi": 2.88
},
......
{
"dt": 1652608800,
"sunrise": 1652580930,
"sunset": 1652639127,
"moonrise": 1652636520,
"moonset": 1652580000,
"moon_phase": 0.47,
"temp": {
"day": 13.65,
"min": 6.22,
"max": 13.65,
"night": 10.34,
"eve": 13.3,
"morn": 6.24
},
"feels_like": {
"day": 12.69,
"night": 9.44,
"eve": 12.38,
"morn": 4.12
},
"pressure": 1007,
"humidity": 62,
"dew_point": 6.58,
"wind_speed": 4.15,
"wind_deg": 280,
"wind_gust": 5.23,
"weather": [
{
"id": 500,
"main": "Rain",
"description": "light rain",
"icon": "10d"
}
],
"clouds": 100,
"pop": 0.2,
"rain": 0.12,
"uvi": 5
}
]
}
这是我主 activity(FetchOutfitInputDetails.java) 中的代码,我从那里调用 API
WeatherAPIInterface weatherAPIInterface= RetrofitInstance.getRetrofit().create(WeatherAPIInterface.class);
weatherAPIInterface.getWeather(lat,lon,"minutely,hourly","metric","en",apiKey).enqueue(new Callback<WeatherPojo>() {
@Override
public void onResponse(Call<WeatherPojo> call, Response<WeatherPojo> response) {
if(response.code()!=404){
Toast.makeText(getContext(),"list present",Toast.LENGTH_SHORT).show();
WeatherPojo pojo=response.body();
Log.v("Aaaaaa",pojo.getCurrent().toString());
}else{
Toast.makeText(getContext(),"LIst Empty",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<WeatherPojo> call, Throwable t) {
}
}
这是我的改造实例class
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitInstance {
private static Retrofit retrofit;
private static final String BASEURL ="https://api.openweathermap.org/data/2.5/";
public static Retrofit getRetrofit() {
if(retrofit==null ){
retrofit=new Retrofit.Builder().
baseUrl(BASEURL).
addConverterFactory(GsonConverterFactory.create()).
build();
}return retrofit;
}
}
天气预报class
import java.util.ArrayList;
public class WeatherPojo {
private float lat;
private float lon;
private String timezone;
private float timezone_offset;
CurrentWeatherPojo CurrentObject;
ArrayList<DailyPojo> daily = new ArrayList<DailyPojo>();
// Getter Methods
public float getLat() {
return lat;
}
public float getLon() {
return lon;
}
public String getTimezone() {
return timezone;
}
public float getTimezone_offset() {
return timezone_offset;
}
public CurrentWeatherPojo getCurrent() {
return CurrentObject;
}
// Setter Methods
public void setLat(float lat) {
this.lat = lat;
}
public void setLon(float lon) {
this.lon = lon;
}
public void setTimezone(String timezone) {
this.timezone = timezone;
}
public void setTimezone_offset(float timezone_offset) {
this.timezone_offset = timezone_offset;
}
public ArrayList<DailyPojo> getDaily() {
return daily;
}
public void setDaily(ArrayList<DailyPojo> daily) {
this.daily = daily;
}
public void setCurrent(CurrentWeatherPojo currentObject) {
this.CurrentObject = currentObject;
}
}
当前天气 pojo class,用于 api 数据中嵌套的当前天气
package com.example.clothme.WeatherAPI;
import java.util.ArrayList;
public class CurrentWeatherPojo {
private float dt;
private float sunrise;
private float sunset;
private float temp;
private float feels_like;
private float pressure;
private float humidity;
private float dew_point;
private float uvi;
private float clouds;
private float visibility;
private float wind_speed;
private float wind_deg;
private float wind_gust;
ArrayList< DailyPojo.weath > weather = new ArrayList < DailyPojo.weath>();
// Getter Methods
public CurrentWeatherPojo(float dt, float sunrise, float sunset, float temp, float feels_like, float pressure, float humidity, float dew_point, float uvi, float clouds, float visibility, float wind_speed, float wind_deg, float wind_gust, ArrayList<DailyPojo.weath> weather) {
this.dt = dt;
this.sunrise = sunrise;
this.sunset = sunset;
this.temp = temp;
this.feels_like = feels_like;
this.pressure = pressure;
this.humidity = humidity;
this.dew_point = dew_point;
this.uvi = uvi;
this.clouds = clouds;
this.visibility = visibility;
this.wind_speed = wind_speed;
this.wind_deg = wind_deg;
this.wind_gust = wind_gust;
this.weather = weather;
}
public float getDt() {
return dt;
}
public float getSunrise() {
return sunrise;
}
public float getSunset() {
return sunset;
}
public float getTemp() {
return temp;
}
public float getFeels_like() {
return feels_like;
}
public float getPressure() {
return pressure;
}
public float getHumidity() {
return humidity;
}
public float getDew_point() {
return dew_point;
}
public float getUvi() {
return uvi;
}
public float getClouds() {
return clouds;
}
public float getVisibility() {
return visibility;
}
public float getWind_speed() {
return wind_speed;
}
public float getWind_deg() {
return wind_deg;
}
public float getWind_gust() {
return wind_gust;
}
// Setter Methods
public void setDt(float dt) {
this.dt = dt;
}
public void setSunrise(float sunrise) {
this.sunrise = sunrise;
}
public void setSunset(float sunset) {
this.sunset = sunset;
}
public void setTemp(float temp) {
this.temp = temp;
}
public void setFeels_like(float feels_like) {
this.feels_like = feels_like;
}
public void setPressure(float pressure) {
this.pressure = pressure;
}
public void setHumidity(float humidity) {
this.humidity = humidity;
}
public void setDew_point(float dew_point) {
this.dew_point = dew_point;
}
public void setUvi(float uvi) {
this.uvi = uvi;
}
public void setClouds(float clouds) {
this.clouds = clouds;
}
public void setVisibility(float visibility) {
this.visibility = visibility;
}
public void setWind_speed(float wind_speed) {
this.wind_speed = wind_speed;
}
public void setWind_deg(float wind_deg) {
this.wind_deg = wind_deg;
}
public void setWind_gust(float wind_gust) {
this.wind_gust = wind_gust;
}
}
另一个 class 用于更多嵌套数据
package com.example.clothme.WeatherAPI;
import java.util.ArrayList;
public class DailyPojo {
private float dt;
private float sunrise;
private float sunset;
private float moonrise;
private float moonset;
private float moon_phase;
Temp TempObject;
Feels_like Feels_likeObject;
private float pressure;
private float humidity;
private float dew_point;
private float wind_speed;
private float wind_deg;
private float wind_gust;
ArrayList< weath > weather = new ArrayList < weath > ();
private float clouds;
private float pop;
private float uvi;
public DailyPojo(float dt, float sunrise, float sunset, float moonrise, float moonset, float moon_phase, Temp tempObject, Feels_like feels_likeObject, float pressure, float humidity, float dew_point, float wind_speed, float wind_deg, float wind_gust, ArrayList<weath> weather, float clouds, float pop, float uvi) {
this.dt = dt;
this.sunrise = sunrise;
this.sunset = sunset;
this.moonrise = moonrise;
this.moonset = moonset;
this.moon_phase = moon_phase;
TempObject = tempObject;
Feels_likeObject = feels_likeObject;
this.pressure = pressure;
this.humidity = humidity;
this.dew_point = dew_point;
this.wind_speed = wind_speed;
this.wind_deg = wind_deg;
this.wind_gust = wind_gust;
this.weather = weather;
this.clouds = clouds;
this.pop = pop;
this.uvi = uvi;
}
// Getter Methods
public float getDt() {
return dt;
}
public float getSunrise() {
return sunrise;
}
public float getSunset() {
return sunset;
}
public float getMoonrise() {
return moonrise;
}
public float getMoonset() {
return moonset;
}
public float getMoon_phase() {
return moon_phase;
}
public Temp getTemp() {
return TempObject;
}
public Feels_like getFeels_like() {
return Feels_likeObject;
}
public float getPressure() {
return pressure;
}
public float getHumidity() {
return humidity;
}
public float getDew_point() {
return dew_point;
}
public float getWind_speed() {
return wind_speed;
}
public float getWind_deg() {
return wind_deg;
}
public float getWind_gust() {
return wind_gust;
}
public float getClouds() {
return clouds;
}
public float getPop() {
return pop;
}
public float getUvi() {
return uvi;
}
// Setter Methods
public void setDt(float dt) {
this.dt = dt;
}
public void setSunrise(float sunrise) {
this.sunrise = sunrise;
}
public void setSunset(float sunset) {
this.sunset = sunset;
}
public void setMoonrise(float moonrise) {
this.moonrise = moonrise;
}
public void setMoonset(float moonset) {
this.moonset = moonset;
}
public void setMoon_phase(float moon_phase) {
this.moon_phase = moon_phase;
}
public void setTemp(Temp tempObject) {
this.TempObject = tempObject;
}
public void setFeels_like(Feels_like feels_likeObject) {
this.Feels_likeObject = feels_likeObject;
}
public void setPressure(float pressure) {
this.pressure = pressure;
}
public void setHumidity(float humidity) {
this.humidity = humidity;
}
public void setDew_point(float dew_point) {
this.dew_point = dew_point;
}
public void setWind_speed(float wind_speed) {
this.wind_speed = wind_speed;
}
public void setWind_deg(float wind_deg) {
this.wind_deg = wind_deg;
}
public void setWind_gust(float wind_gust) {
this.wind_gust = wind_gust;
}
public void setClouds(float clouds) {
this.clouds = clouds;
}
public void setPop(float pop) {
this.pop = pop;
}
public void setUvi(float uvi) {
this.uvi = uvi;
}
public static class Feels_like {
private float day;
private float night;
private float eve;
private float morn;
public Feels_like(float day, float night, float eve, float morn) {
this.day = day;
this.night = night;
this.eve = eve;
this.morn = morn;
}
// Getter Methods
public float getDay() {
return day;
}
public float getNight() {
return night;
}
public float getEve() {
return eve;
}
public float getMorn() {
return morn;
}
// Setter Methods
public void setDay(float day) {
this.day = day;
}
public void setNight(float night) {
this.night = night;
}
public void setEve(float eve) {
this.eve = eve;
}
public void setMorn(float morn) {
this.morn = morn;
}
}
public static class Temp {
private float day;
private float min;
private float max;
private float night;
private float eve;
private float morn;
public Temp(float day, float min, float max, float night, float eve, float morn) {
this.day = day;
this.min = min;
this.max = max;
this.night = night;
this.eve = eve;
this.morn = morn;
}
// Getter Methods
public float getDay() {
return day;
}
public float getMin() {
return min;
}
public float getMax() {
return max;
}
public float getNight() {
return night;
}
public float getEve() {
return eve;
}
public float getMorn() {
return morn;
}
// Setter Methods
public void setDay(float day) {
this.day = day;
}
public void setMin(float min) {
this.min = min;
}
public void setMax(float max) {
this.max = max;
}
public void setNight(float night) {
this.night = night;
}
public void setEve(float eve) {
this.eve = eve;
}
public void setMorn(float morn) {
this.morn = morn;
}
}
public static class weath{
private float id;
private String main;
private String description;
private String icon;
public weath(float id, String main, String description, String icon) {
this.id = id;
this.main = main;
this.description = description;
this.icon = icon;
}
// Getter Methods
public float getId() {
return id;
}
public String getMain() {
return main;
}
public String getDescription() {
return description;
}
public String getIcon() {
return icon;
}
// Setter Methods
public void setId(float id) {
this.id = id;
}
public void setMain(String main) {
this.main = main;
}
public void setDescription(String description) {
this.description = description;
}
public void setIcon(String icon) {
this.icon = icon;
}
}
}
显示的错误消息
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clothme, PID: 25632
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.clothme.Fragments.FetchOutfitInputDetails.onResponse(FetchOutfitInputDetails.java:193)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall.lambda$onResponse[=18=]$retrofit2-DefaultCallAdapterFactory$ExecutorCallbackCall(DefaultCallAdapterFactory.java:89)
at retrofit2.DefaultCallAdapterFactory$ExecutorCallbackCall$$ExternalSyntheticLambda1.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:233)
at android.app.ActivityThread.main(ActivityThread.java:8030)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
这是main中的错误行activity(FetchOutfitInputDetails.java)
Log.v("Aaaaaa",pojo.getCurrent().toString());
尝试将您的字段名称 CurrentObject
更改为 current
,使其与 json 字段的名称匹配
public class WeatherPojo {
// CurrentWeatherPojo CurrentObject;
CurrentWeatherPojo current;
}