android - 来自 Foursquare JSON 请求后的空正文
android - Null body after JSON request from Foursquare
我想搜索附近的地点并在 google 地图上的每个附近地点添加标记。
为此,我使用 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
基地URL: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的正文中获取参数的class:
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 类型,为什么这样不好,为什么我从服务器得到一个空体作为响应?
服务需要 ll
参数,格式为 XX.XX,YY.YY
。当您使用 LatLng
时,服务使用 LatLng.toString()
将参数格式设置为 lat/lng: (46.7700381,23.55158)
,这是服务无法接受的。
您可以更改您的服务以接受 String
作为 ll
参数:
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") String longitudeLatitude); // <- here data type is changed
}
并且,在调用时只需将参数传递为:latLng.latitude + "," + latLng.longitude
我想搜索附近的地点并在 google 地图上的每个附近地点添加标记。 为此,我使用 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
基地URL: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的正文中获取参数的class:
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 类型,为什么这样不好,为什么我从服务器得到一个空体作为响应?
服务需要 ll
参数,格式为 XX.XX,YY.YY
。当您使用 LatLng
时,服务使用 LatLng.toString()
将参数格式设置为 lat/lng: (46.7700381,23.55158)
,这是服务无法接受的。
您可以更改您的服务以接受 String
作为 ll
参数:
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") String longitudeLatitude); // <- here data type is changed
}
并且,在调用时只需将参数传递为:latLng.latitude + "," + latLng.longitude