2017-06-26 37 views
0

我想搜索附近的場地並在Google地圖上的每個附近場地上添加標記。 爲此,我使用Foursquare API和Retrofit向服務器發出請求,但問題是我收到空身體作爲響應。android - 來自Foursquare的JSON請求之後的空身體

這些是我需要傳遞的參數: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主體獲取參數的類:

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 

如果我評估expres錫永response.body()我得到:

errorDetail":"ll must be of the form XX.XX,YY.YY (received lat\/lng: (46.7700381,23.551585)) 

但我不明白,因爲我已經給了它一個經緯度類型,爲什麼會沒有那麼好,爲什麼我得到一個空的身體從響應服務器?

回答

0

服務預計格式XX.XX,YY.YYll參數。當您使用LatLng時,服務使用LatLng.toString()將參數格式設置爲lat/lng: (46.7700381,23.55158),這是服務不可接受的。

你可以改變你的服務接受Stringll參數:

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