I used the code below. I checked the code doesn’t add JSONObject without url_s attribute. When I checked the url API in browser, it doesn’t url_s attribute in any of those. I am not sure if the issue is already raised before but I can’t find it. Please suggest solution for the same.
public class FllickrFetchr {
public static final String TAG=“FlickrFetchr”;
public static final String API_KEY=“20876718ca497c63915ce5ad9519456a”;
public List<GalleryItem> fetchItems(){
List<GalleryItem> items=new ArrayList<>();
try{
String url= Uri.parse("https://api.flickr.com/services/rest/").buildUpon().appendQueryParameter("method","flickr.photos.getRecent").appendQueryParameter("api_key",API_KEY).appendQueryParameter("format","json").appendQueryParameter("nojsoncallback","1").appendQueryParameter("extras","url_S").build().toString();
String jsonString=getUrlString(url);
Log.i(TAG,"Received Json:"+jsonString);
JSONObject jsonBody=new JSONObject(jsonString);
parseItems(items,jsonBody);
}catch (IOException ioe){
Log.e(TAG,"Failed to open",ioe);
}catch (JSONException e){
Log.e(TAG,"Failed to read JSON"+e.getMessage());
}
return items;
}
private void parseItems(List<GalleryItem> items, JSONObject jsonBody)throws IOException,JSONException{
JSONObject photosJsonObject=jsonBody.getJSONObject("photos");
JSONArray photoJsonArray=photosJsonObject.getJSONArray("photo");
for(int i=0;i<photoJsonArray.length();i++){
JSONObject photoInsideObject=photoJsonArray.getJSONObject(i);
GalleryItem item=new GalleryItem();
item.setmCaption(photoInsideObject.getString("title"));
item.setmId(photoInsideObject.getString("id"));
if(!photoInsideObject.has("url_s")){continue;}
item.setmUrl(photoInsideObject.getString("url_S"));
items.add(item);
}
}