尝试检索 JSON 数组时,Retrofit2 GET 请求在列表中返回一些空条目
Retrofit2 GET request is returning some null Entries in List when attempting to retrieve a JSON Array
JSON数据:
[
{
"ID": 47,
"lat": 20.2,
"lon": 18.04308,
"alt": "100",
"location": "Location 001",
"meinOrt": "Ort1",
"meineBeschreibung": "wald",
"satNr": "0"
},
{
"ID": 47,
"lat": 50,
"lon": 28.04308,
"alt": "134.5",
"location": "Location 002",
"meinOrt": "Ort2",
"meineBeschreibung": "wald2",
"satNr": "0"
},
{
"ID": 47,
"lat": 49.07458166666667,
"lon": 16.04308,
"alt": "134.5",
"location": "Location 003",
"meinOrt": "Ort3",
"meineBeschreibung": "wald",
"satNr": "0"
},
{
"ID": 47,
"lat": 46.07458166666667,
"lon": 33.04308,
"alt": "134",
"location": "Location 004",
"meinOrt": "Ort4",
"meineBeschreibung": "wald",
"satNr": "0"
},
{
"ID": 47,
"lat": 45,
"lon": 24.2,
"alt": "134.5",
"location": "Location 005",
"meinOrt": "Ort5",
"meineBeschreibung": "wald",
"satNr": "0"
}
]
Api接口:
@GET("get_entries.php")
Call<List<Entries>> getEntries();
Entries.java
class Entries {
private int id;
private String latitude;
private String longitude;
private String location;
private String altitude;
private String meinOrt;
private String meineBeschreibung;
private String myDatetime;
public Entries(int id, String latitude, String longitude, String location, String altitude, String meinOrt, String meineBeschreibung, String myDatetime) {
this.id = id;
this.latitude = latitude;
this.longitude = longitude;
this.location = location;
this.altitude = altitude;
this.meinOrt = meinOrt;
this.meineBeschreibung = meineBeschreibung;
this.myDatetime = myDatetime;
}
public int getID() {
Log.d("DEBUG", "in getID = " + latitude );
return id;
}
public String getlatitude() {
return latitude;
}
public String getlongitude() {
return longitude;
}
public String getlocation() {
return location;
}
public String getaltitude() {
return altitude;
}
public String getmeinOrt() {
return meinOrt;
}
public String getmeineBeschreibung() {
return meineBeschreibung;
}
public String getmyDatetime() {
return myDatetime;
}
}
Orte.java(是否拨打电话):
private void getEntries() {
textViewResult = findViewById(R.id.text_view_result);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myBaseUrlexample.de/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Log.d("DEBUG", "--> API CREATE = " + jsonPlaceHolderApi );
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries();
Log.d("DEBUG", "--> create call = " + call );
call.enqueue(new Callback<List<Entries>>() {
@Override
public void onResponse(Call<List<Entries>> call, Response<List<Entries>> response) {
Log.d("DEBUG", "--> on response = " + call );
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
List<Entries> alleEintraege = response.body();
Log.d("DEBUG", "--> API CREATE = " + alleEintraege );
for (Entries einzeleintrag : alleEintraege) {
String content = "";
content += "ID: " + einzeleintrag.getID() + "\n";
content += "Lat: " + einzeleintrag.getlatitude() + "\n";
content += "Lon: " + einzeleintrag.getlongitude() + "\n";
content += "Ort: " + einzeleintrag.getmeinOrt() + "\n";
//content += "Entfernung: " + distance + "\n";
content += "Location: " + einzeleintrag.getlocation() + "\n";
content += "Höhe: " + einzeleintrag.getmyDatetime() + "\n";
content += "Beschreibung: " + einzeleintrag.getmeineBeschreibung() + "\n\n";
textViewResult.append(content);
}
}
@Override
public void onFailure(Call<List<Entries>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
Logcat:
11-06 08:11:18.702 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> API CREATE = retrofit2.Retrofit@25f1b684
11-06 08:11:18.713 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> create call = retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@208d07a1
11-06 08:11:18.873 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> on response = retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@208d07a1
11-06 08:11:18.873 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> API CREATE = [com.example.mikkigpsv4.Entries@1cf2c6e4, com.example.mikkigpsv4.Entries@450574d, com.example.mikkigpsv4.Entries@235cd202, com.example.mikkigpsv4.Entries@60e3713, com.example.mikkigpsv4.Entries@14876d50]
我的手机结果 Phone:
ID: 0 Lat: null Lon: null Ort: Ort1 Location: Location001 Höhe: null
Beschreibung: wald
ID: 0 Lat: null Lon: null Ort: Ort2 Location: Location002 Höhe: null
Beschreibung: wald2
ID: 0 Lat: null Lon: null Ort: Ort3 Location: Location003 Höhe: null
Beschreibung: wald
ID: 0 Lat: null Lon: null Ort: Ort4 Location: Location004 Höhe: null
Beschreibung: wald
ID: 0 Lat: null Lon: null Ort: Ort5 Location: Location005 Höhe: null
Beschreibung: waldschreibung: wald
因此,一些条目得到了很好的响应,而一些条目只是被取消了,任何帮助正确获取这些条目的帮助将不胜感激,谢谢。
在您的模型中使用 Gson @SerializedName("")
将键与 json
匹配
@SerializedName("ID")
private int id;
@SerializedName("lat")
private String latitude;
@SerializedName("lon")
private String longitude;
@SerializedName("location")
private String location;
@SerializedName("alt")
private String altitude;
@SerializedName("meinOrt")
private String meinOrt;
@SerializedName("meineBeschreibung")
private String meineBeschreibung;
@SerializedName("satNr")
private String myDatetime;
使用 jsonschema2pojo 生成您的模型,它应该是:
应该是"ID"不是"id"
public class Example {
@SerializedName("ID")
@Expose
private Integer iD;
@SerializedName("lat")
@Expose
private Integer lat;
@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("alt")
@Expose
private String alt;
@SerializedName("location")
@Expose
private String location;
@SerializedName("meinOrt")
@Expose
private String meinOrt;
@SerializedName("meineBeschreibung")
@Expose
private String meineBeschreibung;
@SerializedName("satNr")
@Expose
private String satNr;
public Integer getID() {
return iD;
}
public void setID(Integer iD) {
this.iD = iD;
}
public Integer getLat() {
return lat;
}
public void setLat(Integer lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getMeinOrt() {
return meinOrt;
}
public void setMeinOrt(String meinOrt) {
this.meinOrt = meinOrt;
}
public String getMeineBeschreibung() {
return meineBeschreibung;
}
public void setMeineBeschreibung(String meineBeschreibung) {
this.meineBeschreibung = meineBeschreibung;
}
public String getSatNr() {
return satNr;
}
public void setSatNr(String satNr) {
this.satNr = satNr;
}
}
问题是 Retrofit 使用 GsonConvertor 将 (JSON 对象转换为 POJO) 序列化。为此,您应该在 JSON 对象和您创建的 POJO 对象中使用相同的名称。
作为@Md.Asaduzzaman 的回答,您可以使用@SerializedName("---")
或者您可以重命名您的POJO class 成员以匹配JSON 响应。
i.e latitude to lat
JSON数据:
[
{
"ID": 47,
"lat": 20.2,
"lon": 18.04308,
"alt": "100",
"location": "Location 001",
"meinOrt": "Ort1",
"meineBeschreibung": "wald",
"satNr": "0"
},
{
"ID": 47,
"lat": 50,
"lon": 28.04308,
"alt": "134.5",
"location": "Location 002",
"meinOrt": "Ort2",
"meineBeschreibung": "wald2",
"satNr": "0"
},
{
"ID": 47,
"lat": 49.07458166666667,
"lon": 16.04308,
"alt": "134.5",
"location": "Location 003",
"meinOrt": "Ort3",
"meineBeschreibung": "wald",
"satNr": "0"
},
{
"ID": 47,
"lat": 46.07458166666667,
"lon": 33.04308,
"alt": "134",
"location": "Location 004",
"meinOrt": "Ort4",
"meineBeschreibung": "wald",
"satNr": "0"
},
{
"ID": 47,
"lat": 45,
"lon": 24.2,
"alt": "134.5",
"location": "Location 005",
"meinOrt": "Ort5",
"meineBeschreibung": "wald",
"satNr": "0"
}
]
Api接口:
@GET("get_entries.php")
Call<List<Entries>> getEntries();
Entries.java
class Entries {
private int id;
private String latitude;
private String longitude;
private String location;
private String altitude;
private String meinOrt;
private String meineBeschreibung;
private String myDatetime;
public Entries(int id, String latitude, String longitude, String location, String altitude, String meinOrt, String meineBeschreibung, String myDatetime) {
this.id = id;
this.latitude = latitude;
this.longitude = longitude;
this.location = location;
this.altitude = altitude;
this.meinOrt = meinOrt;
this.meineBeschreibung = meineBeschreibung;
this.myDatetime = myDatetime;
}
public int getID() {
Log.d("DEBUG", "in getID = " + latitude );
return id;
}
public String getlatitude() {
return latitude;
}
public String getlongitude() {
return longitude;
}
public String getlocation() {
return location;
}
public String getaltitude() {
return altitude;
}
public String getmeinOrt() {
return meinOrt;
}
public String getmeineBeschreibung() {
return meineBeschreibung;
}
public String getmyDatetime() {
return myDatetime;
}
}
Orte.java(是否拨打电话):
private void getEntries() {
textViewResult = findViewById(R.id.text_view_result);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://myBaseUrlexample.de/api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Log.d("DEBUG", "--> API CREATE = " + jsonPlaceHolderApi );
Call<List<Entries>> call = jsonPlaceHolderApi.getEntries();
Log.d("DEBUG", "--> create call = " + call );
call.enqueue(new Callback<List<Entries>>() {
@Override
public void onResponse(Call<List<Entries>> call, Response<List<Entries>> response) {
Log.d("DEBUG", "--> on response = " + call );
if (!response.isSuccessful()) {
textViewResult.setText("Code: " + response.code());
return;
}
List<Entries> alleEintraege = response.body();
Log.d("DEBUG", "--> API CREATE = " + alleEintraege );
for (Entries einzeleintrag : alleEintraege) {
String content = "";
content += "ID: " + einzeleintrag.getID() + "\n";
content += "Lat: " + einzeleintrag.getlatitude() + "\n";
content += "Lon: " + einzeleintrag.getlongitude() + "\n";
content += "Ort: " + einzeleintrag.getmeinOrt() + "\n";
//content += "Entfernung: " + distance + "\n";
content += "Location: " + einzeleintrag.getlocation() + "\n";
content += "Höhe: " + einzeleintrag.getmyDatetime() + "\n";
content += "Beschreibung: " + einzeleintrag.getmeineBeschreibung() + "\n\n";
textViewResult.append(content);
}
}
@Override
public void onFailure(Call<List<Entries>> call, Throwable t) {
textViewResult.setText(t.getMessage());
}
});
}
Logcat:
11-06 08:11:18.702 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> API CREATE = retrofit2.Retrofit@25f1b684
11-06 08:11:18.713 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> create call = retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@208d07a1
11-06 08:11:18.873 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> on response = retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@208d07a1
11-06 08:11:18.873 5803-5803/com.example.mikkigpsv4 D/DEBUG: --> API CREATE = [com.example.mikkigpsv4.Entries@1cf2c6e4, com.example.mikkigpsv4.Entries@450574d, com.example.mikkigpsv4.Entries@235cd202, com.example.mikkigpsv4.Entries@60e3713, com.example.mikkigpsv4.Entries@14876d50]
我的手机结果 Phone:
ID: 0 Lat: null Lon: null Ort: Ort1 Location: Location001 Höhe: null Beschreibung: wald
ID: 0 Lat: null Lon: null Ort: Ort2 Location: Location002 Höhe: null Beschreibung: wald2
ID: 0 Lat: null Lon: null Ort: Ort3 Location: Location003 Höhe: null Beschreibung: wald
ID: 0 Lat: null Lon: null Ort: Ort4 Location: Location004 Höhe: null Beschreibung: wald
ID: 0 Lat: null Lon: null Ort: Ort5 Location: Location005 Höhe: null Beschreibung: waldschreibung: wald
因此,一些条目得到了很好的响应,而一些条目只是被取消了,任何帮助正确获取这些条目的帮助将不胜感激,谢谢。
在您的模型中使用 Gson @SerializedName("")
将键与 json
@SerializedName("ID")
private int id;
@SerializedName("lat")
private String latitude;
@SerializedName("lon")
private String longitude;
@SerializedName("location")
private String location;
@SerializedName("alt")
private String altitude;
@SerializedName("meinOrt")
private String meinOrt;
@SerializedName("meineBeschreibung")
private String meineBeschreibung;
@SerializedName("satNr")
private String myDatetime;
使用 jsonschema2pojo 生成您的模型,它应该是:
应该是"ID"不是"id"
public class Example {
@SerializedName("ID")
@Expose
private Integer iD;
@SerializedName("lat")
@Expose
private Integer lat;
@SerializedName("lon")
@Expose
private Double lon;
@SerializedName("alt")
@Expose
private String alt;
@SerializedName("location")
@Expose
private String location;
@SerializedName("meinOrt")
@Expose
private String meinOrt;
@SerializedName("meineBeschreibung")
@Expose
private String meineBeschreibung;
@SerializedName("satNr")
@Expose
private String satNr;
public Integer getID() {
return iD;
}
public void setID(Integer iD) {
this.iD = iD;
}
public Integer getLat() {
return lat;
}
public void setLat(Integer lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
public String getAlt() {
return alt;
}
public void setAlt(String alt) {
this.alt = alt;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getMeinOrt() {
return meinOrt;
}
public void setMeinOrt(String meinOrt) {
this.meinOrt = meinOrt;
}
public String getMeineBeschreibung() {
return meineBeschreibung;
}
public void setMeineBeschreibung(String meineBeschreibung) {
this.meineBeschreibung = meineBeschreibung;
}
public String getSatNr() {
return satNr;
}
public void setSatNr(String satNr) {
this.satNr = satNr;
}
}
问题是 Retrofit 使用 GsonConvertor 将 (JSON 对象转换为 POJO) 序列化。为此,您应该在 JSON 对象和您创建的 POJO 对象中使用相同的名称。
作为@Md.Asaduzzaman 的回答,您可以使用@SerializedName("---")
或者您可以重命名您的POJO class 成员以匹配JSON 响应。
i.e latitude to lat