{"id":4873,"date":"2018-07-19T18:01:31","date_gmt":"2018-07-19T12:31:31","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4873"},"modified":"2018-07-19T18:05:03","modified_gmt":"2018-07-19T12:35:03","slug":"retrofit-library-in-android","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/","title":{"rendered":"Retrofit Library in Android"},"content":{"rendered":"<p>Retrofit is REST API Client for Java.<br \/>\nIt is developed by Square Inc. It uses OkHttp library for HTTP Request. It is a simple library that is used for network transaction.<br \/>\nIt is a very easy and fast library to retrieve and upload the data via Rest based web service.<\/p>\n<h1>Adding Dependency in Build.gradle-<\/h1>\n<pre class=\"\">dependencies {\r\n    implementation 'com.squareup.retrofit2:retrofit:2.1.0'\r\n    implementation 'com.google.code.gson:gson:2.6.2'\r\n    implementation 'com.squareup.retrofit2:converter-gson:2.1.0'\r\n}<\/pre>\n<h1>Adding Internet Permission in Manifest-<\/h1>\n<pre class=\"lang:xhtml decode:true\">&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    package=\"com.example.kripashankar.countryappusingretrofit\"&gt;\r\n    &lt;uses-permission android:name=\"android.permission.INTERNET\" \/&gt;<\/pre>\n<p>Retrofit mainly need three things which are following-<\/p>\n<h3>1. Retrofit Instance-<\/h3>\n<p>You can create Retrofit instance by Retrofit.Builder().<br \/>\nYou have to specify base url and converter factory at the time of Retrofit instance creation as in the below example.<\/p>\n<h3>2. Interface &#8211;<\/h3>\n<pre class=\"lang:xhtml decode:true\">public interface ApiCallInterface\r\n{\r\n   @POST(\"api_name\") \/\/ use @POST if api is post.\r\n   Call&lt;CountryResponse&gt; getResponseData();\r\n}<\/pre>\n<h3>3. Model Class-<\/h3>\n<p>Retrofit need a model class (POJO) for sending and receiving request.<br \/>\nRetrofit use the model class for parsing the server response by using convertors like Gson, Jackson.<\/p>\n<h3>Example &#8211;<\/h3>\n<p>In below example, we are going to display country name and country code in RecyclerView.<br \/>\nFor which as in createRetroFitBuilder() method of example first we set header using OkHttpClient class because API is authenticate api (set header only if api is authenticate). Then we create Retrofit instance and set base url and converter factory and call interface method which interact with API on background thread and success\u00a0 of API can be get in onResponse() method of enqueue call back.Failure of API can be get in onFailure() method of enqueue call back.After that pass the list of country at adapter as in method\u00a0setUpAdapterView() of example.<\/p>\n<pre class=\"lang:xhtml decode:true\">public class MainActivity extends AppCompatActivity {\r\n\r\n    private RecyclerView recyclerViewCountry;\r\n    private CountryListAdapter countryListAdapter;\r\n    String BASE_URL = \"http:\/\/example\/retrofit\/api\/\";\r\n    CountryResponse countryResponse;\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        initializeView();\r\n        createRetroFitBuilder();\r\n    }\r\n\r\n    private void initializeView()\r\n    {\r\n        recyclerViewCountry = (RecyclerView) findViewById(R.id.recyclerViewCountry);\r\n    }\r\n\r\n    private void createRetroFitBuilder() {\r\n        \/\/ set header through OkHttpClient if API is authenticate API.\r\n        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();\r\n\r\n        httpClient.addInterceptor(new Interceptor() {\r\n            @Override\r\n            public okhttp3.Response intercept(Chain chain) throws IOException {\r\n                Request request = chain.request().newBuilder()\r\n                        .addHeader(\"timestamp\", \"153138130\")\r\n                        .addHeader(\"authentication_key\", \"QJTpP\/7rai7D7KF2RcNK=\")\r\n                        .build();\r\n                return chain.proceed(request);\r\n            }\r\n        });\r\n\r\n        \/\/ creating retrofit object and set base url , Converter factory\r\n        Retrofit retrofit = new Retrofit.Builder()\r\n                .baseUrl(BASE_URL)\r\n                .addConverterFactory(GsonConverterFactory.create())\r\n                .client(httpClient.build())\r\n                .build();\r\n       \r\n        \/\/ calling of interface method which interact with API and give response in onResponse().\r\n        ApiCallInterface apiCallInterface = retrofit.create(ApiCallInterface.class);\r\n        Call&lt;CountryResponse&gt; call = apiCallInterface.getResponseData();\r\n\r\n        call.enqueue(new Callback&lt;CountryResponse&gt;() {\r\n            @Override\r\n            public void onResponse(Call&lt;CountryResponse&gt; call, Response&lt;CountryResponse&gt; response)\r\n            {\r\n                countryResponse = response.body();\r\n                setUpAdapterView();\r\n            }\r\n\r\n            @Override\r\n            public void onFailure(Call&lt;CountryResponse&gt; call, Throwable t) {\r\n                Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();\r\n            }\r\n        });\r\n    }\r\n\r\n    private void setUpAdapterView() {\r\n        if(countryResponse != null)\r\n        {\r\n            countryListAdapter = new CountryListAdapter(countryResponse,this);\r\n            LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);\r\n            mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);\r\n            recyclerViewCountry.setLayoutManager(mLayoutManager);\r\n            recyclerViewCountry.setAdapter(countryListAdapter);\r\n        }\r\n\r\n    }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Retrofit is REST API Client for Java. It is developed by Square Inc. It uses OkHttp library for HTTP Request. It is a simple library that is used for network transaction. It is a very easy and fast library to retrieve and upload the data via Rest based web service. Adding Dependency in Build.gradle- dependencies [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4901,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,71],"tags":[290,289,288],"class_list":["post-4873","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-mobile","tag-handling-of-apis-request-and-response","tag-network-communication","tag-retrofit-library"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Retrofit Library in Android - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Retrofit Library in Android - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"Retrofit is REST API Client for Java. It is developed by Square Inc. It uses OkHttp library for HTTP Request. It is a simple library that is used for network transaction. It is a very easy and fast library to retrieve and upload the data via Rest based web service. Adding Dependency in Build.gradle- dependencies [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-19T12:31:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-07-19T12:35:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Retrofit_Library.png\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"510\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Retrofit Library in Android\",\"datePublished\":\"2018-07-19T12:31:31+00:00\",\"dateModified\":\"2018-07-19T12:35:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/\"},\"wordCount\":244,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Retrofit_Library.png\",\"keywords\":[\"Handling of API's Request and Response\",\"Network Communication\",\"Retrofit Library\"],\"articleSection\":[\"Android\",\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/\",\"name\":\"Retrofit Library in Android - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Retrofit_Library.png\",\"datePublished\":\"2018-07-19T12:31:31+00:00\",\"dateModified\":\"2018-07-19T12:35:03+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Retrofit_Library.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Retrofit_Library.png\",\"width\":900,\"height\":510},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/retrofit-library-in-android\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Retrofit Library in Android\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Retrofit Library in Android - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/","og_locale":"en_US","og_type":"article","og_title":"Retrofit Library in Android - InnovationM - Blog","og_description":"Retrofit is REST API Client for Java. It is developed by Square Inc. It uses OkHttp library for HTTP Request. It is a simple library that is used for network transaction. It is a very easy and fast library to retrieve and upload the data via Rest based web service. Adding Dependency in Build.gradle- dependencies [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-07-19T12:31:31+00:00","article_modified_time":"2018-07-19T12:35:03+00:00","og_image":[{"width":900,"height":510,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Retrofit_Library.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Retrofit Library in Android","datePublished":"2018-07-19T12:31:31+00:00","dateModified":"2018-07-19T12:35:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/"},"wordCount":244,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Retrofit_Library.png","keywords":["Handling of API's Request and Response","Network Communication","Retrofit Library"],"articleSection":["Android","Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/","url":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/","name":"Retrofit Library in Android - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Retrofit_Library.png","datePublished":"2018-07-19T12:31:31+00:00","dateModified":"2018-07-19T12:35:03+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Retrofit_Library.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Retrofit_Library.png","width":900,"height":510},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/retrofit-library-in-android\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Retrofit Library in Android"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4873"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4873\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4901"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}