<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Retrofit - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Retrofit - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 26 Jun 2026 17:32:54 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/retrofit/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ RxAndroid and Retrofit 2.0 ]]>
                </title>
                <description>
                    <![CDATA[ By Ahmed Rizwan Ok, so this isn’t new or anything, but I thought let’s just make a simple tutorial with the new Retrofit 2.0. This should give us a starting point. This isn’t a tutorial for RxAndroid. If you don’t know much about RxAndroid, you shoul... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/rxandroid-and-retrofit-2-0-66dc52725fff/</link>
                <guid isPermaLink="false">66c35e34e9895571912a0d06</guid>
                
                    <category>
                        <![CDATA[ android app development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ mobile app development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Retrofit ]]>
                    </category>
                
                    <category>
                        <![CDATA[ rxjava ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 17 Sep 2015 01:20:38 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*OlU3vLjcEhu-oKeGeAdqyA.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ahmed Rizwan</p>
<p>Ok, so this isn’t new or anything, but I thought let’s just make a simple tutorial with the new Retrofit 2.0. This should give us a starting point.</p>
<p>This isn’t a tutorial for RxAndroid. If you don’t know much about RxAndroid, you should first check <a target="_blank" href="https://medium.com/@ahmedrizwan/rxandroid-and-kotlin-part-1-f0382dc26ed8">this</a> out.</p>
<p>Let’s get into it then. Here are the things you’ll need before we start:</p>
<ol>
<li><a target="_blank" href="https://github.com/ReactiveX/RxAndroid">RxAndroid</a> and <a target="_blank" href="https://github.com/square/retrofit">Retrofit</a></li>
<li><a target="_blank" href="https://github.com/google/gson">Gson</a> (I’ll be using Gson, you can use other parsers as well)</li>
<li>An Internet connection!</li>
</ol>
<p>So after you’ve added the dependencies, your gradle files should look something like this (ignore the <a target="_blank" href="https://github.com/evant/gradle-retrolambda">Retrolambda</a> plugin, I just added it for code conciseness because… Lambdas! <em>_</em>):</p>
<p>Now you might be wondering, what is Retrofit exactly? Well, Retrofit is an HTTP Client, but it’s type-safe. That means you can transform an HTTP API into a Java Interface. This makes it ridiculously convenient to interact with the API.</p>
<h3 id="heading-the-starting-example">The Starting Example</h3>
<p>In the example, I’m going to use the <a target="_blank" href="http://openweathermap.org">OpenWeather API</a> and I’ll keep it as simple as possible. I’ll just get the <a target="_blank" href="http://openweathermap.org/current">weather forecast for today</a> in three steps.</p>
<h4 id="heading-step-1-generate-java-model-pojo-classes-from-json">Step 1: Generate Java Model (Pojo) classes from JSON</h4>
<p>Here’s the API URL:</p>
<pre><code>http:<span class="hljs-comment">//api.openweathermap.org/data/2.5/weather?q=London</span>
</code></pre><p>And the response returned when you call it is:</p>
<pre><code>{ “coord”: { “lon”: <span class="hljs-number">-0.13</span>, “lat”: <span class="hljs-number">51.51</span> }, “weather”: [ { “id”: <span class="hljs-number">521</span>, “main”: “Rain”, “description”: “shower rain”, “icon”: “<span class="hljs-number">09</span>d” } ], “base”: “cmc stations”, “main”: { “temp”: <span class="hljs-number">289.49</span>, “pressure”: <span class="hljs-number">993</span>, “humidity”: <span class="hljs-number">67</span>, “temp_min”: <span class="hljs-number">285.93</span>, “temp_max”: <span class="hljs-number">291.15</span> }, “wind”: { “speed”: <span class="hljs-number">8.7</span>, “deg”: <span class="hljs-number">210</span>, “gust”: <span class="hljs-number">14.4</span> }, “rain”: { “<span class="hljs-number">1</span>h”: <span class="hljs-number">1.02</span> }, “clouds”: { “all”: <span class="hljs-number">40</span> }, “dt”: <span class="hljs-number">1442242382</span>, “sys”: { “type”: <span class="hljs-number">1</span>, “id”: <span class="hljs-number">5091</span>, “message”: <span class="hljs-number">0.0052</span>, “country”: “GB”, “sunrise”: <span class="hljs-number">1442208848</span>, “sunset”: <span class="hljs-number">1442254609</span> }, “id”: <span class="hljs-number">2643743</span>, “name”: “London”, “cod”: <span class="hljs-number">200</span>}
</code></pre><p>Looks pretty messy, right? Well, don’t worry, just go to this awesome <a target="_blank" href="http://www.jsonschema2pojo.org">website</a> and paste in the JSON. It’ll come out looking like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*pD1QD3UiI0A4M9yidSIaVQ.png" alt="Image" width="800" height="710" loading="lazy"></p>
<p>If the JSON has to many model classes to be generated, the easier thing to do is generate a <strong>Jar</strong> and download it, extract it, and then add the files. Otherwise just click on Preview, and copy-paste the classes you need. Do remove the <strong>@Generated(“org.jsonschema2pojo”)</strong> from each model class, as this annotation isn’t recognized by Android by default.</p>
<p>I downloaded the Jar because there are a lot of classes. And also because I’m lazy. :)</p>
<p>After extracting and adding the classes, now the project tree looks something like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*HCtSwhgbB3ERajLMixmuyQ.png" alt="Image" width="800" height="540" loading="lazy"></p>
<p>So far, so good!</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*8Aok7yzCTbxShebikqfTsw.png" alt="Image" width="800" height="525" loading="lazy">
<em>Just an evil raccoon being evil.</em></p>
<h4 id="heading-step-2-create-retrofit-interface-for-your-api-calls">Step 2: Create Retrofit Interface for your API calls</h4>
<p>For Retrofit, you have to create an interface for the endpoints of your API.</p>
<p>When creating the interface, you should ask yourself: what exactly is the meaning of life? And secondly: what information do I need from the API?</p>
<p>For me, the answer to both questions is WeatherData (the top-most object). Now let’s examine the URL:</p>
<pre><code>http:<span class="hljs-comment">//api.openweathermap.org/data/2.5/weather?q=London</span>
</code></pre><p>There’s a <strong>query</strong> at the very end, so I’ll do this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*MDro75UsBFVQHUVFVgAtEg.png" alt="Image" width="800" height="145" loading="lazy"></p>
<pre><code>Note: <span class="hljs-keyword">in</span> Retrofit <span class="hljs-number">2.0</span>, the endpoint path string should NOT start <span class="hljs-keyword">with</span> “/”
</code></pre><pre><code>@GET(<span class="hljs-string">"/weather?"</span>) --&gt; incorrect@GET(<span class="hljs-string">"weather?"</span>)  --&gt; correct
</code></pre><p>Now as parameter, I’ll send the query value using the @Query annotation.</p>
<p>As you’ll notice, I’m returning the <strong>Observable</strong> of WeatherData. That’s Rx right there!</p>
<h4 id="heading-step-3-create-the-retrofit-adapter-and-weatherservice-instance">Step 3: Create the Retrofit Adapter and WeatherService instance</h4>
<p>Now in our Activity, we have to create a Retrofit adapter using the Base URL, along with some other info. Once built, we can initiate an object of <strong>WeatherService</strong> interface, then call the method.</p>
<p>Take a look at this delicious code:</p>
<p>So the question here is what the hell are those <em>addCallAdapter</em> and <em>addConverterFactory</em> methods doing there?</p>
<p>Well in order for our calls to return type <strong>Observable</strong>, we have to set the call adapter to <strong>RxJavaCallAdapter</strong>.</p>
<p>And addConverFactory is there to tell Retrofit which sort of converter I want it to use for serializing the JSON. I prefer the GSON converter. There are other converters available too.</p>
<p>So, for these two you need to add their dependencies to your gradle:</p>
<pre><code>compile <span class="hljs-string">'com.squareup.retrofit2:adapter-rxjava:2.0.2'</span>compile <span class="hljs-string">'com.squareup.retrofit2:converter-gson:2.0.0'</span>
</code></pre><p>Now run the code, and voila! It’ll log the weather description.</p>
<h3 id="heading-a-different-example">A Different Example</h3>
<p>So yeah… Another example! Because why not? This time, I’ll try out the GitHub API. Again just 3 steps.</p>
<p>First, the API call URL:</p>
<pre><code>https:<span class="hljs-comment">//api.github.com/users/ahmedrizwan</span>
</code></pre><p>and the response:</p>
<pre><code>{    <span class="hljs-string">"login"</span>: <span class="hljs-string">"ahmedrizwan"</span>,    <span class="hljs-string">"id"</span>: <span class="hljs-number">4357275</span>,    <span class="hljs-string">"avatar_url"</span>: <span class="hljs-string">"https://avatars.githubusercontent.com/u/4357275?v=3"</span>,    <span class="hljs-string">"gravatar_id"</span>: <span class="hljs-string">""</span>,    <span class="hljs-string">"url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan"</span>,    <span class="hljs-string">"html_url"</span>: <span class="hljs-string">"https://github.com/ahmedrizwan"</span>,    <span class="hljs-string">"followers_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/followers"</span>,    <span class="hljs-string">"following_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/following{/other_user}"</span>,    <span class="hljs-string">"gists_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/gists{/gist_id}"</span>,    <span class="hljs-string">"starred_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/starred{/owner}{/repo}"</span>,    <span class="hljs-string">"subscriptions_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/subscriptions"</span>,    <span class="hljs-string">"organizations_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/orgs"</span>,    <span class="hljs-string">"repos_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/repos"</span>,    <span class="hljs-string">"events_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/events{/privacy}"</span>,    <span class="hljs-string">"received_events_url"</span>: <span class="hljs-string">"https://api.github.com/users/ahmedrizwan/received_events"</span>,    <span class="hljs-string">"type"</span>: <span class="hljs-string">"User"</span>,    <span class="hljs-string">"site_admin"</span>: <span class="hljs-literal">false</span>,    <span class="hljs-string">"name"</span>: <span class="hljs-string">"ahmed"</span>,    <span class="hljs-string">"company"</span>: <span class="hljs-literal">null</span>,    <span class="hljs-string">"blog"</span>: <span class="hljs-string">"https://medium.com/@ahmedrizwan"</span>,    <span class="hljs-string">"location"</span>: <span class="hljs-literal">null</span>,    <span class="hljs-string">"email"</span>: <span class="hljs-string">"ahmedrizwan@outlook.com"</span>,    <span class="hljs-string">"hireable"</span>: <span class="hljs-literal">true</span>,    <span class="hljs-string">"bio"</span>: <span class="hljs-literal">null</span>,    <span class="hljs-string">"public_repos"</span>: <span class="hljs-number">19</span>,    <span class="hljs-string">"public_gists"</span>: <span class="hljs-number">0</span>,    <span class="hljs-string">"followers"</span>: <span class="hljs-number">25</span>,    <span class="hljs-string">"following"</span>: <span class="hljs-number">16</span>,    <span class="hljs-string">"created_at"</span>: <span class="hljs-string">"2013-05-06T18:32:59Z"</span>,    <span class="hljs-string">"updated_at"</span>: <span class="hljs-string">"2016-07-08T11:29:26Z"</span>}
</code></pre><p>Again, the JSON looks pretty messy.</p>
<h4 id="heading-step-1-generate-the-pojos-plain-old-java-objects"><strong>Step 1: Generate the Pojos (plain old Java objects)</strong></h4>
<p>I copied the response and pasted it <a target="_blank" href="http://www.jsonschema2pojo.org">here</a> (yet again):</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*gHJzvU0FcKSNWe6yTjSvLg.png" alt="Image" width="800" height="633" loading="lazy"></p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*67owad0Zun8o84QOrAWaUA.png" alt="Image" width="800" height="597" loading="lazy"></p>
<p>And clicked on Preview.</p>
<p>Then I copied the Github class into my Project. <em>music intensifies</em></p>
<h4 id="heading-step-2-create-interface-for-endpoints"><strong>Step 2: Create Interface for Endpoints</strong></h4>
<p>Now examine the API URL. And I mean, really examine it. You’ll see the endpoint starts from users and ends with the username.</p>
<pre><code>https:<span class="hljs-comment">//api.github.com/users/ahmedrizwan</span>
</code></pre><p>So the interface I created looks like this:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*jabUA4iGndv51G0d7Pi_9A.png" alt="Image" width="800" height="144" loading="lazy"></p>
<p>I created a call method, and used <strong>@Path</strong> annotation to replace the value of <em>{username}</em> in the EndPoint string dynamically.</p>
<h4 id="heading-step-3-create-adapter-and-instance-of-githubservice"><strong>Step 3: Create adapter and instance of GithubService</strong></h4>
<p>Here’s the beautiful code for doing just that:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*gEbFR7b6wmJWNJg1sSe9sA.png" alt="Image" width="800" height="285" loading="lazy"></p>
<p>You’ll notice I’m mapping the user object to String (you can do that with Rx. So Its output becomes:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*hoXm3j3xqKF9HbEBWK73kQ.png" alt="Image" width="800" height="63" loading="lazy"></p>
<p>So this is it. Although the article doesn’t cover everything Retrofit and RxAndroid can do (of course), I hope it will get you off to a good start.</p>
<p>Happy coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
