{"id":31326,"date":"2016-01-26T18:22:18","date_gmt":"2016-01-26T12:52:18","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=31326"},"modified":"2016-08-26T17:42:36","modified_gmt":"2016-08-26T12:12:36","slug":"retrofit-vs-volley","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/retrofit-vs-volley\/","title":{"rendered":"Retrofit vs Volley"},"content":{"rendered":"<p>Now a days , Almost every <a title=\"mobile app development service\" href=\"http:\/\/www.tothenew.com\/mobile-application-development-services\">mobile app<\/a> includes some sort of network hits to perform its functionality and there are many alternatives available to perform network operations.Networking libraries are one of the best option for this.Now the question arises which library we should use.The confusion is mainly with two popular and most used libraries i.e. Retrofit and Volley.<\/p>\n<p>So, Here is a comparison of both the libraries with different parameters.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Uses \/Code complexity:<\/strong><\/span><br \/>\nBoth Retrofit and Volley are easy to use and provides the feature to make network calls both synchronously and asynchronously.If we talk about the request and response handling than Retrofit treats the Api calls as simple java method calls and it also handle the Json\/Xml parsing itself, while with Volley we have to do some extra code to achieve the same for parsing(need to create a custom request).<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Inbuilt Types :<\/strong><\/span><br \/>\nThe data returned by an Api plays vital role in implementation of network call and its handling.It is one of the important parameter to decide which option we should use.<br \/>\nVolley can capture four types of responses automatically through these requests:<\/p>\n<p>StringRequest \u2013 This type of request converts the response into a String.<br \/>\nJsonObjectRequest \u2013 This type of request and response is automatically converted into a JSONObject.<br \/>\nJsonArrayRequest \u2013 This type of request and response is automatically converted into a JSONArray.<br \/>\nImageRequest \u2013 This type of request converts the response into a decoded bitmap.<\/p>\n<p>Retrofit can parse many other types of responses automatically like:<br \/>\nBoolean \u2013 Web API response needs to be a boolean.<br \/>\nInteger \u2013 Web API response needs to be an integer.<br \/>\nDate\u2013 Web API response should be Long format date.<br \/>\nString \u2013 Web API response needs to be in String format.<br \/>\nObject \u2013 Web API response needs to be in Json object.<br \/>\nCollections \u2013 Web API response needs to be in a String Format.<br \/>\nImage Loading:<\/p>\n<p>Volley has an inbuilt support for image loading.It is packaged with a loader a custom view called NetworkImageView which is specially designed to download and show images.<\/p>\n<p>On the other hand Retrofit does not provide any such feature, Other libraries such as picasso or glide is recommended to perform image loading.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Caching:<\/strong><\/span><br \/>\nAndroid volley has a very elaborate and flexible caching mechanism.When a request is made through volley first the cache is checked for appropriate response if it is found there than it is returned and parsed else network hit is made.<\/p>\n<p>Retrofit does not support caching.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Retrying Mechanism:<\/strong><\/span><br \/>\nWith volley we can set a retry policy using setRetryPolicy method. It support the customized request timeout, number of retries and back off multiplier.<\/p>\n<p>Retrofit does not support any retrying mechanism.But it can be achieved manually by doing some extra code.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Request Cancellation:<\/strong><\/span><br \/>\nBoth Volley and Retrofit supports request cancellation.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Post Requests and Multipart uploads:<\/strong><\/span><br \/>\nVolley supports both post requests and multipart uploads but for post requests we have to convert our java objects to JSONObject.Also for multipart uploads we have to do some extra code and use some additional classes.<\/p>\n<p>On the other hand Retrofit has full support for Post Requests and Multipart uploads.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now a days , Almost every mobile app includes some sort of network hits to perform its functionality and there are many alternatives available to perform network operations.Networking libraries are one of the best option for this.Now the question arises which library we should use.The confusion is mainly with two popular and most used libraries [&hellip;]<\/p>\n","protected":false},"author":255,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":25},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31326"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/255"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=31326"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/31326\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=31326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=31326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=31326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}