【发布时间】:2012-04-01 20:26:07
【问题描述】:
我想创建一个 android 应用程序,在用户当前位置附近的地图上绘制设施。到目前为止,除了 lat 和 lng 值之外,我能够执行请求并打印响应(例如 id、名称、附近区域等)。
我确信我得到了所需的响应,正如您从这条 logcat 消息中看到的那样:
以下是我如何访问/打印检索到的值:
-- 如注释行所述,访问“for”中的“name”的第一行代码有效,而第二行则无效。
@Override
protected void onPostExecute(PlacesList result) {
// TODO Auto-generated method stub
String text = "Result \n";
if (result!=null){
for(Place place: result.results) {
// This works
text = text + place.name +"\n";
// This doesn't
text = text + place.geometry.location.lat +"\n";
}
txt1.setText(text);
}
setProgressBarIndeterminateVisibility(false);
}
我猜这个问题可能是由模型(来源:link)的处理方式引起的:
-- 需要注意的是,声明(并且不加注释)public Geometry geometry;完全阻止我检索任何响应值(id、名称等)。
public class PlacesList {
@Key
public String status;
@Key
public List<Place> results;
}
public class Place {
@Key
public String id;
@Key
public String name;
@Key
public String reference;
@Key
public String vicinity;
@Key
public String icon;
// Leaving this line prevents me from retrieving any of the above values
@Key
public Geometry geometry;
// On the other hand, declaring non-existing elements (elements that are not
// included on the Place Search response) does not break the program
// at all (i.e. values from valid elements such as id, name, etc. can be retrieved)
@Key
public String testFakeElement;
@Override
public String toString() {
return name + " - " + id + " - " + reference;
}
}
public class Geometry {
@Key
public Location location;
@Override
public String toString() {
return location.toString();
}
public class Location {
@Key
public double lat;
@Key
public double lng;
@Override
public String toString() {
return Double.toString(lat) + ", " + Double.toString(lng);
}
}
}
【问题讨论】:
-
@DilSe 现在可以使用了。在下面检查我的答案。
标签: android httpresponse google-api-java-client google-places-api