Site icon C1CTech

Android Retrofit Example

<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Retrofit<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>Retrofit is a<strong><span style&equals;"color&colon; &num;008000&semi;"> REST Client<&sol;span><&sol;strong> for Android and Java developed by <strong><span style&equals;"color&colon; &num;008000&semi;">Square<&sol;span><&sol;strong>&period; It makes it relatively easy to retrieve and upload JSON &lpar;or other structured data&rpar; via a <strong><span style&equals;"color&colon; &num;008000&semi;">REST based webservice<&sol;span><&sol;strong>&period; In Retrofit you configure which converter is used for the data serialization&period; Typically for JSON you use GSon&comma; but you can add custom converters to process XML or other protocols&period; Retrofit uses the <strong><span style&equals;"color&colon; &num;008000&semi;">OkHttp library for HTTP requests<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>Get <span style&equals;"color&colon; &num;0000ff&semi;"><strong>GITHUB<&sol;strong><&sol;span> code from <span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;RetrofitExample"><strong>here<&sol;strong><&sol;a>&period;<&sol;span><&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"XuueDLBJYqk" title&equals;"Android Retrofit Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;XuueDLBJYqk"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;XuueDLBJYqk&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Retrofit Example"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Using Retrofit<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>To work with <strong><span style&equals;"color&colon; &num;008000&semi;">Retrofit<&sol;span><&sol;strong> you need basically three classes&period;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>Model class<&sol;strong><&sol;span> which is used to map the JSON data&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;008000&semi;">Builder class &&num;8211&semi; Instance<&sol;span><&sol;strong> which uses the interface and the Builder API which allows defining the URL end point for the HTTP operation&period;<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;008000&semi;"><strong>Interfaces<&sol;strong><&sol;span> which defines the possible HTTP operations&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><&sol;h3>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Getting Cric API Key &sol; Sample JSON<&sol;strong><&sol;span><&sol;h3>&NewLine;<p><strong> <&sol;strong>In this tutorial we will use The <span style&equals;"color&colon; &num;008000&semi;"><strong>Cric API<&sol;strong><&sol;span>&period; In order to use this API it is necessary to obtain the <span style&equals;"color&colon; &num;008000&semi;"><strong>API key<&sol;strong><&sol;span>&period; <span style&equals;"color&colon; &num;000080&semi;"><strong><a style&equals;"color&colon; &num;000080&semi;" href&equals;"http&colon;&sol;&sol;www&period;cricapi&period;com&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer">Here<&sol;a><&sol;strong><&sol;span> you need to register and login in order to obtain the key&period;<&sol;p>&NewLine;<p>This is the <strong><span style&equals;"color&colon; &num;008000&semi;">Json<&sol;span><&sol;strong> format provided by the <strong><span style&equals;"color&colon; &num;008000&semi;">Cric API<&sol;span><&sol;strong> under  <span style&equals;"color&colon; &num;008000&semi;"><strong>MatchCalender<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-270 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;Capture-2&period;jpg" alt&equals;"" width&equals;"355" height&equals;"205" &sol;><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Extended form of above Json&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-271 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;json3&period;jpg" alt&equals;"" width&equals;"489" height&equals;"466" &sol;><&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;"> Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;<ol>&NewLine;<li>Create a new project in Android Studio from <span style&equals;"color&colon; &num;008000&semi;"><strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong>&period;<&sol;span> When it prompts you to select the default activity&comma; select<span style&equals;"color&colon; &num;008000&semi;"> <&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">Empty Activity<&sol;span> <&sol;strong>and proceed&period;<&sol;li>&NewLine;<li>Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;<&sol;strong><strong>gradle <&sol;strong><&sol;span>and add <span style&equals;"color&colon; &num;008000&semi;"><strong>Retrofit<&sol;strong>&comma; <strong>Gson <&sol;strong><&sol;span>dependencies&period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>build&period;<&sol;strong><&sol;span><strong><span style&equals;"color&colon; &num;0000ff&semi;">gradle<&sol;span> <&sol;strong><&sol;span><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; implementation 'com&period;android&period;support&colon;appcompat-v7&colon;26&period;1&period;0'&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;retrofit&comma;json<&sol;span><&sol;strong>&NewLine; compile 'com&period;google&period;code&period;gson&colon;gson&colon;2&period;6&period;2'&NewLine; compile 'com&period;squareup&period;retrofit2&colon;retrofit&colon;2&period;0&period;2'&NewLine; compile 'com&period;squareup&period;retrofit2&colon;converter-gson&colon;2&period;0&period;2'&NewLine; &NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>3&period;Since we are working with network operations we need to add<span style&equals;"color&colon; &num;008000&semi;"> <strong>INTERNET <&sol;strong><&sol;span>permissions in <span style&equals;"color&colon; &num;008000&semi;"><strong>AndroidManifest&period;<&sol;strong><strong>xml <&sol;strong><&sol;span>file&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;<&sol;strong><strong>xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;manifest xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; package&equals;"com&period;example&period;lenovo&period;cricketmatchapp"&gt&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&lt&semi;uses-permission android&colon;name&equals;"android&period;permission&period;INTERNET"&sol;&gt&semi;<&sol;span><&sol;strong>&NewLine; &NewLine; &lt&semi;application&NewLine; android&colon;allowBackup&equals;"true"&NewLine; android&colon;icon&equals;"&commat;drawable&sol;ic&lowbar;ball"&NewLine; android&colon;label&equals;"&commat;string&sol;app&lowbar;name"&NewLine; android&colon;supportsRtl&equals;"true"&NewLine; android&colon;theme&equals;"&commat;style&sol;AppTheme"&gt&semi;&NewLine;&NewLine; &lt&semi;activity android&colon;name&equals;"&period;activity&period;MainActivity"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;MAIN" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;category android&colon;name&equals;"android&period;intent&period;category&period;LAUNCHER" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine; &lt&semi;&sol;activity&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;application&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;manifest&gt&semi;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>4&period;Create four sub-packages named <span style&equals;"color&colon; &num;008000&semi;"><strong>activity<&sol;strong>&comma; <strong>adapter<&sol;strong>&comma; <strong>rest <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">and<&sol;span> <strong>model <&sol;strong><&sol;span>in your main package&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating Model Class<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>Using this <span style&equals;"color&colon; &num;008000&semi;">route<&sol;span> <strong>&lpar;<span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"color&colon; &num;008000&semi;" href&equals;"http&colon;&sol;&sol;www&period;cricapi&period;com&sol;api&sol;matchCalendar&sol;&quest;apikey&equals;INSERT&lowbar;YOUR&lowbar;API&lowbar;KEY">http&colon;&sol;&sol;www&period;cricapi&period;com&sol;api&sol;matchCalendar&sol;&quest;apikey&equals;INSERT&lowbar;YOUR&lowbar;API&lowbar;KEY<&sol;a><&sol;span>&rpar;<&sol;strong> we  get the total matches of this year&period;<&sol;p>&NewLine;<p>In this <strong><span style&equals;"color&colon; &num;008000&semi;"> Json<&sol;span><&sol;strong> format <strong><span style&equals;"color&colon; &num;008000&semi;"> data&lbrack; &rsqb;<&sol;span><&sol;strong> is the array of object consists of <strong><span style&equals;"color&colon; &num;008000&semi;">unique&lowbar;id<&sol;span><&sol;strong>&comma;<strong><span style&equals;"color&colon; &num;008000&semi;">name<&sol;span><&sol;strong>&comma;<span style&equals;"color&colon; &num;008000&semi;"><strong>date<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-267 size-full" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;03&sol;json2&period;jpg" alt&equals;"" width&equals;"414" height&equals;"374" &sol;><&sol;p>&NewLine;<p>5&period;Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>CalenderResponse&period;java <&sol;strong><&sol;span>and <span style&equals;"color&colon; &num;008000&semi;"><strong>MatchCalender&period;java<&sol;strong><&sol;span> under <span style&equals;"color&colon; &num;008000&semi;"><strong>model <&sol;strong><&sol;span>package&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>CalenderResponse&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;cricketmatchapp&period;model&semi;&NewLine;&NewLine;import com&period;google&period;gson&period;annotations&period;SerializedName&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;public class CalenderResponse &lbrace;&NewLine; &commat;SerializedName&lpar;"data"&rpar;&NewLine; private List&lt&semi;MatchCalender&gt&semi; data&semi;&NewLine;&NewLine; public List&lt&semi;MatchCalender&gt&semi; getData&lpar;&rpar; &lbrace;&NewLine; return data&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setData&lpar;List&lt&semi;MatchCalender&gt&semi; data&rpar; &lbrace;&NewLine; this&period;data &equals; data&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MatchCalender&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;cricketmatchapp&period;model&semi;&NewLine;&NewLine;import com&period;google&period;gson&period;annotations&period;SerializedName&semi;&NewLine;&NewLine;public class MatchCalender &lbrace;&NewLine;&NewLine; &commat;SerializedName&lpar;"unique&lowbar;id"&rpar;&NewLine; private String uniqueId&semi;&NewLine;&NewLine; &commat;SerializedName&lpar;"name"&rpar;&NewLine; private String name&semi;&NewLine;&NewLine;&NewLine; &commat;SerializedName&lpar;"date"&rpar;&NewLine; private String date&semi;&NewLine;&NewLine; public MatchCalender&lpar;String uniqueId&comma; String name&comma; String date&rpar; &lbrace;&NewLine; this&period;uniqueId &equals; uniqueId&semi;&NewLine; this&period;name &equals; name&semi;&NewLine; this&period;date &equals; date&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public String getUniqueId&lpar;&rpar; &lbrace;&NewLine; return uniqueId&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setUniqueId&lpar;String uniqueId&rpar; &lbrace;&NewLine; this&period;uniqueId &equals; uniqueId&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getName&lpar;&rpar; &lbrace;&NewLine; return name&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setName&lpar;String name&rpar; &lbrace;&NewLine; this&period;name &equals; name&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getDate&lpar;&rpar; &lbrace;&NewLine; return date&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setDate&lpar;String date&rpar; &lbrace;&NewLine; this&period;date &equals; date&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Creating the Retrofit instance<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>6&period; To send network requests to an API&comma; we need to use the <strong><span style&equals;"color&colon; &num;008000&semi;">Retrofit Builder class<&sol;span><&sol;strong> and specify the base URL for the service&period; So&comma; create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>ApiClient&period;<&sol;strong> <&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">java<&sol;span> <&sol;strong>under <span style&equals;"color&colon; &num;008000&semi;"><strong>rest<&sol;strong> <&sol;span>package&period;<&sol;p>&NewLine;<p>Here<span style&equals;"color&colon; &num;008000&semi;"> <strong>BASE&lowbar;URL<&sol;strong><&sol;span> – it is basic URL of our API&period; We will use this URL for all requests later&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>ApiClient&period;<&sol;strong> <strong>java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;cricketmatchapp&period;rest&semi;&NewLine;&NewLine;import retrofit2&period;Retrofit&semi;&NewLine;import retrofit2&period;converter&period;gson&period;GsonConverterFactory&semi;&NewLine;&NewLine;public class ApiClient &lbrace;&NewLine; public static final String BASE&lowbar;URL &equals; "http&colon;&sol;&sol;cricapi&period;com&sol;"&semi;&NewLine; public static Retrofit retrofit &equals; null&semi;&NewLine;&NewLine; public static Retrofit getClient&lpar;&rpar; &lbrace;&NewLine; if &lpar;retrofit &equals;&equals; null&rpar; &lbrace;&NewLine; retrofit &equals; new Retrofit&period;Builder&lpar;&rpar;&NewLine; &period;baseUrl&lpar;BASE&lowbar;URL&rpar;&NewLine; &period;addConverterFactory&lpar;GsonConverterFactory&period;create&lpar;&rpar;&rpar;&NewLine; &period;build&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; return retrofit&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Defining endpoints inside of an interface<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>The endpoints are defined inside of an interface using special retrofit annotations to encode details about the parameters and request method&period; Interfaces defines the possible HTTP operations<&sol;p>&NewLine;<p>7&period;Create <span style&equals;"color&colon; &num;008000&semi;"><strong>ApiInterface&period;<&sol;strong><&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;">java<&sol;span> <&sol;strong>under <span style&equals;"color&colon; &num;008000&semi;"><strong>rest <&sol;strong><&sol;span>package&period;<&sol;p>&NewLine;<p>So&comma; using this route the retrofit will generate the following URL&colon;<br &sol;>&NewLine;<span style&equals;"color&colon; &num;008000&semi;"><strong>http&colon;&sol;&sol;www&period;cricapi&period;com&sol;api&sol;matchCalendar&sol;&quest;apikey&equals;INSERT&lowbar;YOUR&lowbar;API&lowbar;KEY<&sol;strong><&sol;span><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>ApiInterface&period;<&sol;strong><strong>java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;cricketmatchapp&period;rest&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;model&period;CalenderResponse&semi;&NewLine;&NewLine;import retrofit2&period;Call&semi;&NewLine;import retrofit2&period;http&period;GET&semi;&NewLine;import retrofit2&period;http&period;Query&semi;&NewLine;&NewLine;public interface ApiInterface &lbrace;&NewLine;&NewLine; &commat;GET&lpar;"api&sol;matchCalendar"&rpar;&NewLine; Call&lt&semi;CalenderResponse&gt&semi; getMatchCalender&lpar;&commat;Query&lpar;"apikey"&rpar; String apiKey&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>Take a look to other annotations&colon;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><em><strong>&commat;Path<&sol;strong><&sol;em><&sol;span> – variable substitution for the API endpoint&period; For example movie id will be swapped for&lbrace;id&rcub; in the URL endpoint&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><em><strong>&commat;Query<&sol;strong><&sol;em><&sol;span> – specifies the query key name with the value of the annotated parameter&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><em><strong>&commat;Body<&sol;strong><&sol;em><&sol;span> – payload for the POST call<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;008000&semi;"><em><strong>&commat;Header<&sol;strong><&sol;em> <&sol;span>– specifies the header with the value of the annotated parameter<&sol;p>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;"><strong>Making the Request<&sol;strong><&sol;span><&sol;h3>&NewLine;<p>8&period;Let’s make the request from our <strong><span style&equals;"color&colon; &num;008000&semi;">MainActivity<&sol;span><&sol;strong>&period; Open the <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;java <&sol;strong><&sol;span>and do the below changes&period; Make sure that your replaced <span style&equals;"color&colon; &num;008000&semi;"><strong>API&lowbar;KEY<&sol;strong><&sol;span> should be correct&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;cricketmatchapp&period;activity&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;R&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;adapter&period;MatchCalenderAdapter&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;model&period;CalenderResponse&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;model&period;MatchCalender&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;rest&period;ApiClient&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;rest&period;ApiInterface&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;import retrofit2&period;Call&semi;&NewLine;import retrofit2&period;Callback&semi;&NewLine;import retrofit2&period;Response&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private final static String API&lowbar;KEY &equals; "kAv1Vdcj8teYg5QnVmAACIofzdg2"&semi;&NewLine; private static final String TAG &equals; MainActivity&period;class&period;getSimpleName&lpar;&rpar;&semi;&NewLine; &NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; ApiInterface apiService &equals; ApiClient&period;getClient&lpar;&rpar;&period;create&lpar;ApiInterface&period;class&rpar;&semi;&NewLine;&NewLine; Call&lt&semi;CalenderResponse&gt&semi; call &equals; apiService&period;getMatchCalender&lpar;API&lowbar;KEY&rpar;&semi;&NewLine; call&period;enqueue&lpar;new Callback&lt&semi;CalenderResponse&gt&semi;&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onResponse&lpar;Call&lt&semi;CalenderResponse&gt&semi; call&comma; Response&lt&semi;CalenderResponse&gt&semi; response&rpar; &lbrace;&NewLine; int statuscode &equals; response&period;code&lpar;&rpar;&semi;&NewLine; List&lt&semi;MatchCalender&gt&semi; matchlist &equals; response&period;body&lpar;&rpar;&period;getData&lpar;&rpar;&semi;&NewLine; &NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onFailure&lpar;Call&lt&semi;CalenderResponse&gt&semi; call&comma; Throwable t&rpar; &lbrace;&NewLine; Log&period;e&lpar;TAG&comma; t&period;toString&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Retrofit<&sol;span><&sol;strong> will download and parse the API data on a background thread&comma; and then return the results back to the UI thread via the<strong><span style&equals;"color&colon; &num;008000&semi;"> onResponse<&sol;span><&sol;strong> or <span style&equals;"color&colon; &num;008000&semi;"><strong>onFailure <&sol;strong><&sol;span>method&period;<&sol;p>&NewLine;<h3><strong><span style&equals;"color&colon; &num;000080&semi;">Displaying the Results in RecyclerView<&sol;span><&sol;strong><&sol;h3>&NewLine;<p>9&period;Let’s use <strong><span style&equals;"color&colon; &num;008000&semi;">RecyclerView<&sol;span><&sol;strong> for fetched results&period; First of all&comma; add it to the <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>build&period;gradle<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; implementation fileTree&lpar;dir&colon; 'libs'&comma; include&colon; &lbrack;'&ast;&period;jar'&rsqb;&rpar;&NewLine; implementation 'com&period;android&period;support&colon;appcompat-v7&colon;26&period;1&period;0'&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;recyclerView<&sol;span><&sol;strong> &NewLine; compile 'com&period;android&period;support&colon;recyclerview-v7&colon;26&period;1&period;0'&NewLine; &NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>10&period;Create a layout named <span style&equals;"color&colon; &num;008000&semi;"><strong>match&lowbar;calender&lowbar;item&period;xml <&sol;strong><&sol;span>under res layout&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>match&lowbar;calender&lowbar;item&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;orientation&equals;"vertical"&NewLine; android&colon;background&equals;"&commat;drawable&sol;bg&lowbar;color"&NewLine; &gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;date"&NewLine; android&colon;padding&equals;"5dp"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"10dp"&NewLine; android&colon;layout&lowbar;marginRight&equals;"20dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"8dp"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"20sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;name"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;padding&equals;"5dp"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"8dp"&NewLine; android&colon;textAllCaps&equals;"true"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textSize&equals;"20sp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine;<&sol;pre>&NewLine;<p>11&period;Adapter is a common pattern which helps to bind view and data&comma; so let’s implement adapter for this&period;<&sol;p>&NewLine;<p>Create a class named <span style&equals;"color&colon; &num;008000&semi;"><strong>MatchCalenderAdapter&period;java<&sol;strong><&sol;span> under <span style&equals;"color&colon; &num;008000&semi;"><strong>adapter<&sol;strong><&sol;span> package&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MatchCalenderAdapter&period;java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;cricketmatchapp&period;adapter&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;R&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;model&period;MatchCalender&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;&NewLine;public class MatchCalenderAdapter extends RecyclerView&period;Adapter&lt&semi;MatchCalenderAdapter&period;MatchCalenderViewHolder&gt&semi; &lbrace;&NewLine;&NewLine; List&lt&semi;MatchCalender&gt&semi; matchlist&semi;&NewLine; Context context&semi;&NewLine; &NewLine; public static class MatchCalenderViewHolder extends RecyclerView&period;ViewHolder &lbrace;&NewLine;&NewLine; TextView name&semi;&NewLine; TextView date&semi;&NewLine;&NewLine; public MatchCalenderViewHolder&lpar;View v&rpar; &lbrace;&NewLine; super&lpar;v&rpar;&semi;&NewLine; name &equals; &lpar;TextView&rpar; v&period;findViewById&lpar;R&period;id&period;name&rpar;&semi;&NewLine; date &equals; &lpar;TextView&rpar; v&period;findViewById&lpar;R&period;id&period;date&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public MatchCalenderAdapter&lpar;List&lt&semi;MatchCalender&gt&semi; matchlist&comma; Context context&rpar; &lbrace;&NewLine; this&period;matchlist &equals; matchlist&semi;&NewLine; this&period;context &equals; context&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onBindViewHolder&lpar;MatchCalenderViewHolder holder&comma; int position&rpar; &lbrace;&NewLine; holder&period;date&period;setText&lpar;matchlist&period;get&lpar;position&rpar;&period;getDate&lpar;&rpar;&rpar;&semi;&NewLine; holder&period;name&period;setText&lpar;matchlist&period;get&lpar;position&rpar;&period;getName&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public MatchCalenderAdapter&period;MatchCalenderViewHolder onCreateViewHolder&lpar;ViewGroup parent&comma; int viewType&rpar; &lbrace;&NewLine; View view &equals; LayoutInflater&period;from&lpar;parent&period;getContext&lpar;&rpar;&rpar;&period;inflate&lpar;R&period;layout&period;match&lowbar;calender&lowbar;item&comma; parent&comma; false&rpar;&semi;&NewLine; return new MatchCalenderViewHolder&lpar;view&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public int getItemCount&lpar;&rpar; &lbrace;&NewLine; return matchlist&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<p>12&period;Open <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period; java <&sol;strong><&sol;span>and modify the code as below&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period; java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;cricketmatchapp&period;activity&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;util&period;Log&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;R&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;adapter&period;MatchCalenderAdapter&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;model&period;CalenderResponse&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;model&period;MatchCalender&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;rest&period;ApiClient&semi;&NewLine;import com&period;example&period;lenovo&period;cricketmatchapp&period;rest&period;ApiInterface&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;import retrofit2&period;Call&semi;&NewLine;import retrofit2&period;Callback&semi;&NewLine;import retrofit2&period;Response&semi;&NewLine;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; private final static String API&lowbar;KEY &equals; "kAv1Vdcj8teYg5QnVmAACIofzdg2"&semi;&NewLine; private static final String TAG &equals; MainActivity&period;class&period;getSimpleName&lpar;&rpar;&semi;&NewLine; RecyclerView recyclerView&semi;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine;&NewLine; recyclerView &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;match&lowbar;calender&lowbar;recycler&lowbar;view&rpar;&semi;&NewLine;&NewLine; recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;this&rpar;&rpar;&semi;&NewLine;&NewLine; ApiInterface apiService &equals; ApiClient&period;getClient&lpar;&rpar;&period;create&lpar;ApiInterface&period;class&rpar;&semi;&NewLine;&NewLine; Call&lt&semi;CalenderResponse&gt&semi; call &equals; apiService&period;getMatchCalender&lpar;API&lowbar;KEY&rpar;&semi;&NewLine; call&period;enqueue&lpar;new Callback&lt&semi;CalenderResponse&gt&semi;&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onResponse&lpar;Call&lt&semi;CalenderResponse&gt&semi; call&comma; Response&lt&semi;CalenderResponse&gt&semi; response&rpar; &lbrace;&NewLine; int statuscode &equals; response&period;code&lpar;&rpar;&semi;&NewLine; List&lt&semi;MatchCalender&gt&semi; matchlist &equals; response&period;body&lpar;&rpar;&period;getData&lpar;&rpar;&semi;&NewLine; recyclerView&period;setAdapter&lpar;new MatchCalenderAdapter&lpar;matchlist&comma; getApplicationContext&lpar;&rpar;&rpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onFailure&lpar;Call&lt&semi;CalenderResponse&gt&semi; call&comma; Throwable t&rpar; &lbrace;&NewLine; Log&period;e&lpar;TAG&comma; t&period;toString&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p><strong><span style&equals;"color&colon; &num;008000&semi;">Now when you run the app it will look like this&colon; <&sol;span>     <&sol;strong><&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-1530" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2020&sol;02&sol;Screenshot&lowbar;1580907706&period;png" alt&equals;"Screenshot&lowbar;1580907706" width&equals;"438" height&equals;"778" &sol;>&NewLine;

Exit mobile version