A replacement block can be used to directly manipulate the resulting URL for a call. Just like a Query parameter, this allows you to use the value passed into the method to be used in place of the replacement block.
- [Instructor] Now that loading the lists of gists is all buttoned up, what I really want to be able to do is dig into a specific gist when I click on it. Open the GitHub service interface again. A restful API will typically expose an address we can use to obtain information about specific records using their unique ID. Using the GitHub API, we can get a specific gist by going to api.github.com/gists/ the gist ID.
And just like query parameters, we can set this gist ID using a method parameter. Let's define a new method that returns a call, wrapping a gist, because we only want the one gist with the ID we provide. Call it getGistById. The parameters we want to be placed into the URL need to be annotated with the @Path annotation.
This annotation takes one parameter, which is the string that should be substituted in the path, with the value that is passed into this method. These are called replacement blocks. Let's use gistId, with a capital I. The method parameter is a string we will also call gistID. This method definition needs to be annotated with an @GET tag like the others, but the URL should be /gists/ open curly brace, gistId, with a capital I, close curly brace.
Retrofit will try to match the strings surrounded with curly braces in the URL to any path annotation in the method definition, and will throw an error if it cannot match them exactly, so make sure they match. Let's connect this to the UI now. In the project outline on the left, the view model package, open the gist view model class. This class handles all the logic for the gist activity.
But before we do anything, let's quickly copy a few things from the main view model class so that we can actually use the GitHub service. In the main view model class, scroll up to initAnonService. Copy it all. Go back to the gist view model class, below the mGist comment previous page property, define a new private IGitHub service called mGitHubService.
Scroll down a little, and replace the initAnonService method declaration here with the one we copied from the main view model class, making sure to import all of the relevant classes. Now we can work with the GitHub service interface to load the gist. Scroll down some more to the load gist method. Just like all of the other requests we have made to load gists, below the mProgressBarVisbility line, call mGitHubService.getGistById, passing it the mGistId, which is a property that is set to the ID of the gist the user clicked.
Then .enqueue to add it to the call stack, passing in a new callback, enter to auto fill. Getting it out of the way in the on failure method, always make sure that you call showError with t.getLocalizedMessage to display a message for the user if something went wrong. Now in the on response method, hide the progress bar, with mProgressBarVisibility.postValue, view.gone, and then if not response.isSuccessful, show an error from the value returned from our networkUtil.onResponseError for this response.
And then return. Otherwise, when the response was successful, we want to show the loaded gist with mGist.postvalue, passing it the response.body, which will tell the UI to show the details of the gist. And then we need to call two more methods that we will cover later on. Load comment page count, and get gist star.
- REST API basics
- Methods for reading and writing data
- Defining data models with Retrofit
- Implementing an API interface
- Getting data with a Retrofit call
- Authorizing requests
- Putting, deleting, and posting data
- Customizing the data converter
- Working with the request and response headers