One Last Bit of Flickr Data

No matter how you choose to open Flickr’s photo page, you need to get its URL first. If you look at the JSON data you are currently receiving for each photo, you can see that the photo page is not part of those results.

    {
      "photos": {
        ...,
        "photo": [
          {
            "id": "9452133594",
            "owner": "44494372@N05",
            "secret": "d6d20af93e",
            "server": "7365",
            "farm": 8,
            "title": "Low and Wisoff at Work",
            "ispublic": 1,
            "isfriend": 0,
            "isfamily": 0,
            "url_s":"https://farm8.staticflickr.com/7365/9452133594_d6d20af93e_m.jpg"
          }, ...
        ]
      },
      "stat": "ok"
    }

(Recall that url_s is the URL for the small version of the photo, not the full-size photo.)

You might think that you are in for some more JSON request writing. Fortunately, that is not the case. If you look at the Web Page URLs section of Flickr’s documentation at flickr.com/​services/​api/​misc.urls.html, you will see that you can create the URL for an individual photo’s page like so:

    https://www.flickr.com/photos/user-id/photo-id

The photo-id in the URL is the same as the value of the id attribute from your JSON data. You are already stashing that in id in GalleryItem. What about user-id? If you poke around the documentation, you will find that the owner attribute in your JSON data is a user ID. So if you pull out the owner attribute, you should be able to build the URL from your photo JSON data:

    https://www.flickr.com/photos/owner/id

Update GalleryItem to put this plan into action.

Listing 29.1  Adding code for the photo page (GalleryItem.kt)

data class GalleryItem(
    var title: String = "",
    var id: String = "",
    @SerializedName("url_s") var url: String = "",
    @SerializedName("owner") var owner: String = ""
) {
    val photoPageUri: Uri
        get() {
            return Uri.parse("https://www.flickr.com/photos/")
                .buildUpon()
                .appendPath(owner)
                .appendPath(id)
                .build()
        }
}

To determine the photo URL, you create a new owner property and add a computed property called photoPageUri to generate photo page URLs as discussed above. Because Gson is translating your JSON responses into GalleryItems on your behalf, you can immediately start using the photoPageUri property without any other code changes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.183.137