How to Use Fast Android Networking Library in Android with Example?
Last Updated :
18 Feb, 2022
In Android, we know multiple networking libraries like Retrofit, Volley. We use these libraries specifically for making networking requests like performing actions on API. But Besides that, there is another library called Fast Networking Library which has a few advantages over other libraries. In this tutorial, we will be specifically focusing on learning about this library. We will be doing the following.
- Getting started with Fast Networking Library
- Making a simple GET request
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add dependencies
Copy the following dependency and paste it into your app-level build.gradle file.
implementation 'com.amitshekhar.android:android-networking:1.0.2
Step 3: Add Internet permission in the manifest file
Make sure to add the following line of code in the Android Manifest File. which will give you access to using the internet, otherwise the app will crash.
<uses-permission android:name="android.permission.INTERNET" />
Step 4: Adding TextView in activity_main.xml
For testing the working of our Fast Networking Library code we will need to add a text and change its text in order to make sure if we got a response successful or we got an error. So the code for activity_main.xml file is as below
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Testing"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 5: Initializing the Android Networking Class
First, we have to initialize the Android Networking Class before we can use it. It can be done by adding a single line in MainActivity as follows
AndroidNetworking.initialize(getApplicationContext());
Step 6: Making a get request
We will try to fetch data from a REST API. The link for the API we will be using is given below
https://meme-api.herokuapp.com/gimme
Add the following code in MainActivity which will actually make a GET request on the API
AndroidNetworking.get("https://meme-api.herokuapp.com/gimme")
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
textView.setText("Response Successful");
}
@Override
public void onError(ANError anError) {
textView.setText("Response Failure");
}
});
Step 7: Working with the MainActivity.java
After performing all the steps above our code in MainActivity will look like the code below. You can also directly copy the below code and paste it into your MainActivity file.
Java
package com.example.gfgfastnetworkinglib;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Defining TextView which we made in XML
TextView textView = findViewById(R.id.textView);
// Initializing Android Networking
AndroidNetworking.initialize(getApplicationContext());
// Actually making the GET request
AndroidNetworking.get("https://meme-api.herokuapp.com/gimme")
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
// Text will show success if Response is success
textView.setText("Response Successful");
}
@Override
public void onError(ANError anError) {
// Text will show failure if Response is failure
textView.setText("Response Failure");
}
});
}
}
Explanation of the above code:
We have to pass the API link inside the get() method of AndroidNetworking as you can see in the code above. As we proceed further we have two overridden methods onResponse and onError. Anyone one of both methods will get according to what response we get from the API.
If the response is successful the onResponse method will get called and hence the text will get set to Response Successful, If we get failure Response from the API then the onError method will get called hence the code inside it will get executed and text will set to Response Unsuccessful. We may get a failure response because of some problem with the internet or some security issues with the API.
Output:
As we can clearly see the output shows Response Successful which means our GET request using Fast Networking is Successful. If Some problem would have occurred then in output we would see Response Failure.
Similar Reads
How to Implement Paging Library in Android with Example?
As the number of users of a particular mobile application grows, so does the amount of data associated with that application. For example, as the number of Instagram app users increased, so did the number of daily feeds on the app. In general, if we want to display data from a remote server in our A
11 min read
Fresco Image Loading Library in Android with Example
Fresco is one of the famous image loading libraries from URLs in Android. It is a powerful library for displaying and managing images from URLs. This library can load images from Users' devices, servers, and other local sources. The most important feature of this library is to show a placeholder ima
3 min read
How to Use FFmpeg in Android with Example?
FFmpeg, short for Fast-forward MPEG, is a free and open-source multimedia framework, which is able to decode, encode, transcode, mux, demux, stream, filter and play fairly all kinds of multimedia files that have been created to date. It also supports some of the eldest formats. FFmpeg compiles and r
15+ min read
Deep Linking in Android with Example
Deep Linking is one of the most important features that is used by various apps to gather data inside their apps in the form of a URL link. So it becomes helpful for the users from other apps to easily share the data with different apps. In this article, we will take a look at the implementation of
7 min read
How to Use PRDownloader Library in Android App?
PRDownloader library is a file downloader library for android. It comes with pause and resumes support while downloading a file. This library is capable of downloading large files from the internet and can download any type of file like image, video, pdf, apk and etc. It provides many features that
11 min read
How to Fix "android.os.Network On Main Thread Exception error" in Android Studio?
The main reason for Network On Main Thread Exception Error is because the newer versions of android are very strict against the UI thread abuse. Whenever we open an app, a new âmainâ thread is created which helps applications interact with the running components of an appâs UI and is responsible for
3 min read
How to use Firebase UI Authentication Library in Android?
Firebase UI is a library provided by Firebase for Android apps which makes or so many tasks easy while integrating Firebase in Android. This library provides so many extra features that we can integrate into our Android very easily. In this article, we will take a look at using this library for addi
10 min read
Line Graph View in Android with Example
If you are looking for a view to represent some statistical data or looking for a UI for displaying a graph in your app then in this article we will take a look on creating a line graph view in our Android App using the GraphView library. What we are going to build in this article? We will be buildi
3 min read
Intent Service in Android with Example
An IntentService is a subclass of Service in Android that is used to handle asynchronous requests (expressed as "Intents") on demand. It runs in the background and stops itself once it has processed all the intents that were sent to it. An IntentService in Java and Kotlin: Kotlin class MyIntentServi
5 min read
Implicit and Explicit Intents in Android with Examples
Pre-requisites: Android App Development Fundamentals for Beginners Guide to Install and Set up Android Studio Android | Starting with the first app/android project Android | Running your first Android app This article aims to tell about the Implicit and Explicit intents and how to use them in an and
6 min read