[英]How do i fetch photo reference from api with retrofit
我一直很難在我的 android 應用程序中獲取photo reference
值以使用google places
。
API數據
"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 獲取數據照片引用。 我已成功獲取其他值,例如圖標和名稱,但我不知道如何進行照片參考。 查看 api,您可以看到它嵌入在“照片”數組中。
由於您的所有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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.