The annotation to a model property lets you pass the serialized and deserialized * Data class to handle the response from the server. The return type for this function is Call with its type CryptocurrenciesLatest.ĭata classes are POJOs (Plain Old Java Objects) that represent the responses of the API calls we’re going to make. baseUrl + v1/cryptocurrency/listings/latest + query Annotation is used to define query parameter for request. The string value tells retrofit that the path of this request is The annotation tells retrofit that this request is a get type request. Here we define the API on the client side. It’s an interface that declares our requests and their types. Versions is just a separate gradle file for convenience: def versions = Įxt.versions = versions Next set up the Retrofit interface Implementation "2:converter-gson:$versions.retrofit"Īs you noticed from my comment, the OkHttp dependency is already shipped with the Retrofit 2 dependency. JSON converter Gson for JSON to Java object mapping Implementation "2:retrofit:$versions.retrofit" Let’s add these lines to your gradle file: // 3rd party We are going to use latest Google’s JSON converter Gson version. Since we will get responses in JSON format, we need to include the converter manually in the dependencies too. Also you should know that Retrofit doesn’t ship with an integrated JSON converter. Then you should add library dependenciesįind the latest Retrofit version. Give this permission by adding these lines to your Manifest file: We are going to execute HTTP requests on a server accessible via the Internet. Don’t worry right now about code quality or any programming principles or optimizations - we’ll just write some code to make Retrofit 2 work in our project and discuss what it does.įollow these steps to set up Retrofit 2 on My Crypto Coins app project: First, give INTERNET permission for the app We will apply a similar approach with Retrofit 2 for you to understand it better more quickly. When learning something new, I like to try it out in practice as soon as I can. Quick setup for Retrofit 2 just to try it first We will get back a JSON data result and then convert it using the Gson library. We will use a special OkHttp interceptor class for CoinMarketCap API authentication when making a call to the server. Our first goal is to get all the cryptocurrencies list using Retrofit from the Internet. Now that you know that everything is closely related, we are going to use all these 3 libraries at once. Retrofit is strongly coupled with OkHttp and makes intensive use of it. In contrast, Retrofit is a high-level REST abstraction build on top of OkHttp. OkHttp is a pure HTTP/SPDY client responsible for any low-level network operations, caching, requests and responses manipulation. Also if you need, you can add custom converters to Retrofit to process XML or other protocols.įor making HTTP requests Retrofit uses the OkHttp library. Typically to serialize and deserialize objects to and from JSON you use an open-source Java library - Gson. In Retrofit you configure which converter is used for the data serialization. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. This library, in my opinion, is the most important one to learn, as it will do the main job. Retrofit is a REST Client for Java and Android.
0 Comments
Leave a Reply. |