JSON数据:
2647ms
360ms
10440ms
Api界面:
[
{
"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"
}
]
Entries.java
@GET("get_entries.php")
Call<List<Entries>> getEntries();
Orte.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;
}
}
Logcat:
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());
}
});
}
手机结果:
ID:0纬度:空Lon:空Ort:Ort1位置:Location001Höhe:空 Beschreibung:瓦尔德
ID:0纬度:空Lon:空Ort:Ort2位置:Location002Höhe:空 Beschreibung:wald2
ID:0纬度:空Lon:空Ort:Ort3位置:Location003Höhe:空 Beschreibung:瓦尔德
ID:0纬度:空Lon:空Ort:Ort4位置:Location004Höhe:空 Beschreibung:瓦尔德
ID:0纬度:空Lon:空Ort:Ort5位置:Location005Höhe:空 Beschreibung:waldschreibung:瓦尔德
因此,某些条目的响应很好,而某些条目只是为空,非常感谢您能正确地获得所有帮助,谢谢。
答案 0 :(得分:3)
在模型中使用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;
答案 1 :(得分:0)
使用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;
}
}
答案 2 :(得分:0)
问题是Retrofit使用GsonConvertor将(JSON对象转换为POJO)序列化。为此,您应该在创建的JSON对象和POJO对象中使用相同的名称。
作为@ Md.Asaduzzaman答案,您可以使用@SerializedName("---")
,也可以重命名POJO类成员以与JSON响应匹配。
i.e latitude to lat