Reputation: 740
I have been having a hard time fetching photo reference
value for use of google places
in my android app.
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
}
],
Model Class
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;
}
}
}
Adapter Class
@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
}
So how do i properly structure my model class to fetch data photo reference from the api. I have successfully fetched other values e.g icon and name but i have no idea how to go about photo reference. Looking at the api, you can see that it is embedded inside a 'Photos' array.
Upvotes: 1
Views: 131
Reputation: 15423
As all your Result
doesn't have photos
. So you have to handle this. Try like below:
List<NearbyPlaces.Result.Photos> photos = placeModels.get(i).getPhotos();
if(photos != null && !photos.isEmpty()) {
String photoReference = photos.get(0).getPhotoReference();
// Use photoReference here
}
Upvotes: 1