Volley & Retrofit Kotlin 2019–20 | BulletTut

To-the-point comparison with very basic implementation to get started

Karan Jatav
Nov 5 · 6 min read

Tutorial Content

Volley Intro

Retrofit Intro

Volley and Retrofit difference

Project Setup

Volley Implementation(Basic)

Retrofit Implementation(Basic)

Volley Intro

  • Developed by Google and introduced during Google I/O 2013.
  • Part of the Android Open Source Project(AOSP).
  • Manages the processing and caching of network requests.

Retrofit Intro

  • Developed by Square.
  • A type-safe HTTP client for Android and Java.
  • Uses OkHttp as the systems administration layer and is based over it.

Volley and Retrofit differences

1. In-Built Request Types

Volley:

  • StringRequest– Returned data is parsed and converted into a String.
  • JsonObjectRequest– Converts the response into a JSONObject.
  • JsonArrayRequest– Response is automatically converted into a JSONArray.
  • ImageRequest– Request converts the response into a decoded bitmap automatically.

Retrofit:

  • Boolean– Web API response needs to be a String boolean.
  • Integer– Web API response needs to be an integer.
  • Date– Web API response should be Long format date.
  • String– Web API response needs to be in String format.
  • Object– Web API response needs to be in a JSON object.
  • Collections– Web API response needs to be in a String Format.

2. Retry Mechanism

Volley:

  • Supports retries on request timeout.
  • Can set a retry policy by using the setRetryPolicy() method.
  • By default, a volley request timeout time is set to 5 seconds and can be customized.

Retrofit:

  • Does not come with retry support by default.
  • Have to check for failure, clone and enqueue the call manually for each network call.

3. Caching

Volley:

  • Has a very elaborate caching mechanism.
  • Customizable caching functionality to support your requirements.

Retrofit:

  • Caching not supported by default.
  • Can implement RFC 2616 caching which is the spec for HTTP caching, through the OkHttpClient.

4. Loading Images

Volley:

  • Has a special type of request to get images from a network called ImageRequest.
  • Supports the resizing of the returned image in the worker thread itself.
  • Also has a NetworkImageView class which can be used with ImageLoader class, to automatically load images, whenever the NetworkImageViewappears.

Retrofit:

  • Does not support the loading of images by default.
  • Can be combined with OkHttpClient to support the loading of images.

5. Data Parsing

Volley:

  • The entire network call + JSON/XML parsing is completely handled with help from Gson for JSON parsing.
  • Support for arbitrary formats with pluggable serialization/deserialization.

Retrofit:

  • Need to include this GsonRequest<T> class, which represents a request whose response is converted to type T by Gson.

Project Setup

  • Create a new project in Android Studio.
  • Select “Empty Activity” press next.
  • Name the project “RestApisLibs”.
  • Select Language as Kotlin.
  • Leave rest as default and press next.
  • Declare all the TextViews.
  • Connect TextViews from the XML file in onCreate function.
  • Create a function called setTextViews to set up the TextViews as data comes in.

Volley Implementation(Basic)

  • In MainActivity.kt create a function getVolleyResponse() for API calling through Volley:
  1. Create a request queue.
  2. Create a stringRequest adding method, URL, Response.Listener, Response.ErrorListener.
  3. Add the request to the queue.
  • Handle response inside Response listeners.

4. Parse the response inside Response.Listener.

5. Set text of tvResponseFrom in Response.ErrorListener in case of faliure.

  • Add the function in onCreate method at the bottom.
  • Run the app.

Retrofit Implementation(Basic)

  • Create a file Post.kt and add the following code:
  • In MainActivity.kt create a function getRetrofitResponse() for API calling through Retrofit:
  1. Create a Retrofit.Builder instance and implement methods like baseUrl(), addConverterFactory(), build().
  2. Create JsonPlaceHolderApi instance.
  3. Call a method getPosts() through jsonPlaceHolderApi and put the result in postApi value.
  4. Call enqueue method on postApi and with a Callback<Post> object as a parameter.
  • Handle listeners in Callback<Post> object.

5. Check if the response gives a proper HTTP code and extract the data from it.

6. Set tvResponseFrom text in case of a failure.

  • Add the function in onCreate method at the bottom.
  • Run the app.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade