Skip to main content

Retrofit Tutorial & Volley & AsyncTask Tutorial With Example In Android Studio

Retrofit Tutorial With Example In Android Studio [Step by Step]

In Android, Retrofit is a REST Client for Java and Android by Square inc under Apache 2.0 license. Its a simple network library that used for network transactions. By using this library we can seamlessly capture JSON response from web service/web API. It’s easy and fast library to retrieve and upload the data(JSON or any other structured data) via a REST based web service.
Retrofit Post Example In Android Studio

Need of Retrofit In Android:

We have a lot of network libraries that used to fetch and send the data from/to server. In our previous article we use Volley library for network transactions but Retrofit is an ultimate replacement of Volleyand all other libraries. Retrofit is better alternative of other libraries in terms of performance, ease of use, extensibility and others.

Difference Between Retrofit and other libraries:

In Android, retrofit library is different from other network libraries because it gives us an easy to use platform through which we don’t need to parse JSON responses as they are done by library itself. It used GSON library in the background to parser the response data. What we need to do is define a POJO (Plain Old Java Object) to parse the response.

Performance benchmarks for Android AsyncTask, Volley and Retrofit (in milliseconds, lower value is better):

Here is the main difference between our three mainly used techniques for implementing API’s is our android app. You can see the difference in performance that for one discussion means for one network request and response they will take how much time.
1. AsyncTask: 
one(1) discussion: 941 ms
Seven(7) discussions: 4539 ms
Twenty Five(25) discussions: 13957 ms
2. Volley: 
one(1) discussion: 560 ms
Seven(7) discussions: 2202 ms
Twenty Five(25) discussions: 4275 ms
3. Retrofit: 
one(1) discussion: 312 ms
Seven(7) discussions: 889 ms
Twenty Five(25) discussions: 1059 ms

Steps to Integrate Retrofit 1.9 in our Android Studio Project.

Retrofit-In-Android
Here are the main steps to integrate Retrofit in our project.
1.Add dependency file in gradle: 
Firsly add this library in your build.gradle file in app module.
compile ‘com.squareup.retrofit:retrofit:1.9.0’


Volley Tutorial With Example In Android Studio

Volley is a HTTP library developed by Google and was first introduced during Google I/O 2013. This library is used to transmit data over the network. It actually makes networking faster and easier for Apps. It is available through AOSP(Android Open Source Project) repository.
Volley Example Android
The volley library has the features like automatic scheduling of network request, multiple concurrent connections, request prioritization, cancel/block a request, easier management of UI with data fetched asynchronously from the network and also offers easier customization.
Important Note: Volley uses cache to improve the App performance by saving memory and bandwidth of remote server.

Volley Uses Caches To Improve Performance In Android:

Volley uses caches concept to improve the performance of App. For example, lets say you are using Asynctask to fetch image and description from a JSON array created in server API. The content is fetched in the portrait mode and now user rotate screen to change it to landscape mode. The activity is destroyed and so the content will be fetched again. Since the user is calling the same resource again to server so it is a waste of resource and ultimately a poor user experience.
Volley provide solution to this problem as it caches the data. When user request the same data, instead of calling from server Volley directly shows it from cache saving resource and thus improving user experience.
Below is the basic diagram of Volley:
Volley-In-Android

Understanding RequestQueue & Working With Volley In Android:

Volley is a networking library managed by the RequestQueue and mainly used for smaller Networking purposes in Android. To use it, first you need to instantiate the RequestQueue and later on you can start or stop request, add or cancel request and access the response cache(s).

RequestQueue queue = Volley.newRequestQueue(this);



AsyncTask Tutorial With Example Android Studio [Step By Step]

In Android, AsyncTask (Asynchronous Task) allows us to run the instruction in the background and then synchronize again with our main thread. This class will override at least one method i.e doInBackground(Params) and most often will override second method onPostExecute(Result).
AsyncTask class is used to do background operations that will update the UI(user interface). Mainly we used it for short operations that will not effect on our main thread.
AsyncTask class is firstly executed using execute() method. In the first step AsyncTask is called onPreExecute() then onPreExecute() calls doInBackground() for background processes and then doInBackground() calls onPostExecute() method to update the UI.
AsyncTask Example Android Flow

Need of AsyncTask In Android:

By default, our application code runs in our main thread and every statement is therefore execute in a sequence. If we need to perform long tasks/operations then our main thread is blocked until the corresponding operation has finished. For providing a good user experience in our application we need to use AsyncTasks class that runs in a separate thread. This class will executes everything in doInBackground() method inside of other thread which doesn’t have access to the GUI where all the views are present. The onPostExecute() method of this class synchronizes itself again with the main UI thread and allows it to make some updating. This method is called automatically after the doInBackground method finished its work.

Syntax of AsyncTask In Android:

To use AsyncTask you must subclass it. The parameters are the following AsyncTask <TypeOfVarArgParams, ProgressValue, ResultValue>. Here is the

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap then to bytes. Also

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App §   2.4.7  Set the color §   2.4.8  Adding Animations o    2.5  To plot grouped bar chart §   2.5.1  Creating Dataset o    2.6  Get the best of Android plus exclusive deals and freebies in your inbox!