Как получить ссылку на фотографию из API с модернизацией - PullRequest
1 голос
/ 20 января 2020

Мне было трудно получить значение photo reference для использования google places в моем приложении android.

API DATA

"photos" : [
            {
               "height" : 332,
               "html_attributions" : [
                  "\u003ca href=\"https://maps.google.com/maps/contrib/110539175553196663923\"\u003ePrecious Imianmian\u003c/a\u003e"
               ],
               "photo_reference" : "CmRaAAAApEbJYuLAwp8WO9BpnKrpdbqyuOPjHOdu3eKVqjUIg0Kg6LNgRswF_tWhLOipbkzP6Nlf1_1P-EUxwP2jQhIm90N0jEShobu5leyfaAqDIiEn_e5fJLpEZLSXLasvWo2HEhArF2rvEhbkfldVDWcOtGJ7GhQtrp8IJ5OSBZnynf90N5-84Mal7Q",
               "width" : 500
            }
         ],

Класс модели

public class NearbyPlaces {
    @SerializedName("results")
    private ArrayList<Result> list;

    public ArrayList<Result> getList() {
        return list;
    }
 public class Result {
        @Expose
        @SerializedName("photos")
        private List<Photos> photos;
        @SerializedName("geometry")
        private Geometry getGeometry;

        @SerializedName("icon")
        private String icon;

        @SerializedName("id")
        private String id;

        @SerializedName("name")
        private String name;

        @SerializedName("vicinity")
        public List<Photos> getPhotos() {
            return photos;
        }

        public void setPhotos(List<Photos> photos) {
            this.photos = photos;
        }
 public static class Photos {
        @Expose
        @SerializedName("width")
        private int width;
        @Expose
        @SerializedName("photo_reference")
        private String photoReference;
        @Expose
        @SerializedName("html_attributions")
        private List<String> htmlAttributions;
        @Expose
        @SerializedName("height")
        private int height;

        public int getWidth() {
            return width;
        }

        public void setWidth(int width) {
            this.width = width;
        }

        public String getPhotoReference() {
            return photoReference;
        }

        public void setPhotoReference(String photoReference) {
            this.photoReference = photoReference;
        }

        public List<String> getHtmlAttributions() {
            return htmlAttributions;
        }

        public void setHtmlAttributions(List<String> htmlAttributions) {
            this.htmlAttributions = htmlAttributions;
        }

        public int getHeight() {
            return height;
        }

        public void setHeight(int height) {
            this.height = height;
        }
    }
}

Класс адаптера

 @Override
    public void onBindViewHolder(@NonNull PlaceResultAdapter.ViewHolder viewHolder, int i) {
        viewHolder.place_name.setText(placeModels.get(i).getName());
        String image_url = placeModels.get(i).getIcon();
      //  Picasso.get().load(placeModels.get(i).getPhotos().get(0).getPhotoReference().into(viewHolder.place_image));//returns null
       // Toast.makeText(context, String.valueOf(placeModels.get(i).getPhotos().size()), Toast.LENGTH_SHORT).show(); this returns zero size
    }

Итак, как мне правильно структурировать мой класс модели для получения справочной информации о фотографии из API. Я успешно выбрал другие значения, например, значок и имя, но не знаю, как go узнать о фотографии. Глядя на API, вы можете видеть, что он встроен в массив 'Photos'.

1 Ответ

1 голос
/ 20 января 2020

Поскольку все ваши Result не имеют photos. Таким образом, вы должны справиться с этим. Попробуйте как ниже:

List<NearbyPlaces.Result.Photos> photos = placeModels.get(i).getPhotos();
if(photos != null && !photos.isEmpty()) {
    String photoReference = photos.get(0).getPhotoReference();

    // Use photoReference here
}
...