【发布时间】:2017-11-29 05:58:49
【问题描述】:
我想搜索附近的场地并在谷歌地图上的每个附近场地上添加一个标记。 为此,我使用 Foursquare API 和 Retrofit 向服务器发出请求,但问题是我收到一个空正文作为响应。
这些是我需要传入的参数:https://developer.foursquare.com/start/search
https://api.foursquare.com/v2/venues/search
?client_id=CLIENT_ID
&client_secret=CLIENT_SECRET
&v=20130815
&ll=40.7,-74
&query=sushi
基本网址:https://developer.foursquare.com/docs/venues/search
.baseUrl("https://api.foursquare.com/v2/venues/search/")
我用来请求 JSON 的接口:
public interface FoursquareService {
@GET("venues/search")
Call<List<FoursquarePlaces>> getAllVenues(@Query("client_id") String clientId,
@Query("client_secret") String clientSecret,
@Query("v") String date,
@Query("ll") LatLng longitudeLatitude);
}
我用来从 JSON 主体获取参数的类:
public class FoursquarePlaces {
private String name;
private String city;
private String category;
private String longitude;
private String latitude;
private String address;
public FoursquarePlaces() {
this.name = "";
this.city = "";
this.setCategory("");
}
public void setCity(String city){
if (city != null){
this.city = city.replaceAll("\\(", "").replaceAll("\\)", "");
}
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
改造客户:
retrofit = new Retrofit.Builder()
.baseUrl("https://api.foursquare.com/v2/venues/search/")
.addConverterFactory(GsonConverterFactory.create())
.build();
我在从 JSON 正文获得的位置上添加一个标记:
foursquareService = retrofit.create(FoursquareService.class);
venuesLatLng = new LatLng(venuesLatitude, venuesLongitude);
foursquareService.getAllVenues(Constants.FOURSQUARE_CLIENT_KEY,
Constants.FOURSQUARE_CLIENT_SECRET, formatedDate, latLng).enqueue(new Callback<List<FoursquarePlaces>>() {
@Override
public void onResponse(Call<List<FoursquarePlaces>> call, Response<List<FoursquarePlaces>> response) {
for (FoursquarePlaces place : response.body()) {
// I get the error here on response.body()
venuesLatitude = Integer.parseInt(place.getLatitude());
venuesLongitude = Integer.parseInt(place.getLongitude());
venuesMarker.position(venuesLatLng);
mMap.addMarker(venuesMarker);
}
}
@Override
public void onFailure(Call<List<FoursquarePlaces>> call, Throwable t) {
}
});
我得到的错误:
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
如果我评估表达式 response.body() 我得到:
errorDetail":"ll must be of the form XX.XX,YY.YY (received lat\/lng: (46.7700381,23.551585))
但我不明白,因为我给了它一个 LatLng 类型,为什么这样不好,为什么我得到一个空的正文作为服务器的响应?
【问题讨论】:
标签: android json google-maps retrofit foursquare