From the course: Android Development: Retrofit with Java

Manipulate the URL path - Android Tutorial

From the course: Android Development: Retrofit with Java

Start my 1-month free trial

Manipulate the URL path

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

Contents