改造1.9 - 响应为空

时间:2017-02-03 18:32:26

标签: android retrofit

JSON架构:

https://gyazo.com/654b65bc293518d8202573ddccfe1b61

我想访问first object中的results array,但当我尝试通过retrofit检索它时,它表示results数组为空。那是为什么?

接口

public interface MapInterface {
@GET("/json")
public void getResults(@Query("location") String location, @Query("radius") double radius,
                            @Query("type") String type, @Query("key") String key, Callback<Result> response);

 }

MainActivity

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint("https://maps.googleapis.com/maps/api/place/nearbysearch/json?").build();
    MapInterface map = restAdapter.create(MapInterface.class);

        map.getResults("65.9667,-18.5333", PROXIMITY_RADIUS, "restaurant", placesKey , new Callback<Result>() {
        @Override
        public void success(Result result, Response response) {
            Log.d("Matt", result.getResults().toString()); 
            //this logs  `D/Matt: []`
        }

        @Override
        public void failure(RetrofitError error) {
            Log.d("Matt", error.getMessage());
        }
    });

POJO(从jsonschema2pojo生成)

public class Result {
    @SerializedName("results")
    @Expose
    private List<Result> results = null;
    @SerializedName("geometry")
    @Expose
    private Geometry geometry;
    @SerializedName("icon")
    @Expose
    private String icon;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("opening_hours")
    @Expose
    private OpeningHours openingHours;
    @SerializedName("photos")
    @Expose
    private List<Photo> photos = null;
    @SerializedName("place_id")
    @Expose
    private String placeId;
    @SerializedName("reference")
    @Expose
    private String reference;
    @SerializedName("scope")
    @Expose
    private String scope;
    @SerializedName("types")
    @Expose
    private List<String> types = null;
    @SerializedName("vicinity")
    @Expose
    private String vicinity;
    @SerializedName("rating")
    @Expose
    private Double rating;

    public List<Result> getResults() {
        return results;
    }

    public void setResults(List<Result> results) {
        this.results = results;
    }

    public Geometry getGeometry() {
        return geometry;
    }

    public void setGeometry(Geometry geometry) {
        this.geometry = geometry;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public OpeningHours getOpeningHours() {
        return openingHours;
    }

    public void setOpeningHours(OpeningHours openingHours) {
        this.openingHours = openingHours;
    }

    public List<Photo> getPhotos() {
        return photos;
    }

    public void setPhotos(List<Photo> photos) {
        this.photos = photos;
    }

    public String getPlaceId() {
        return placeId;
    }

    public void setPlaceId(String placeId) {
        this.placeId = placeId;
    }

    public String getReference() {
        return reference;
    }

    public void setReference(String reference) {
        this.reference = reference;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public List<String> getTypes() {
        return types;
    }

    public void setTypes(List<String> types) {
        this.types = types;
    }

    public String getVicinity() {
        return vicinity;
    }

    public void setVicinity(String vicinity) {
        this.vicinity = vicinity;
    }

    public Double getRating() {
        return rating;
    }

    public void setRating(Double rating) {
        this.rating = rating;
    }

}

1 个答案:

答案 0 :(得分:0)

您需要添加将从GET方法返回的返回Java对象

试试这个

public interface MapInterface {
@GET("/json")
Call<Result> getResults(@Query("location") String location, @Query("radius") double radius,
                        @Query("type") String type, @Query("key") String key, Callback<Result> response);

}