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","lat": 49.07458166666667,"lon": 16.04308,"location": "Location 003","meinOrt": "Ort3","lat": 46.07458166666667,"lon": 33.04308,"alt": "134","location": "Location 004","meinOrt": "Ort4","lat": 45,"lon": 24.2,"location": "Location 005","meinOrt": "Ort5","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:瓦尔德
因此,某些条目的响应很好,而某些条目只是为空,非常感谢您能正确地获得所有帮助,谢谢。