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
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);
TextView textView = findViewById(R.id.textView);
AndroidNetworking.initialize(getApplicationContext());
.build()
.getAsJSONObject( new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
textView.setText( "Response Successful" );
}
@Override
public void onError(ANError anError) {
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 GET Data From API using Retrofit Library in Android?
A GET request is one of the HTTP request methods that can be used to retrieve data from a server. In an API context, a GET request is typically used to retrieve a specific resource or a collection of resources from a server. When a client makes a GET request to a server, it sends a request message t
7 min read
How to Add External Library in Android Studio?
Android Studio is the official IDE (Integrated Development Environment) for Android app development and it is based on JetBrainsâ IntelliJ IDEA software. Android Studio provides many excellent features that enhance productivity when building Android apps. In this article, we will learn how to add ex
2 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 AwesomeBar Library in Android App?
AwesomeBar is a library that animates and makes it really easy to integrate various features and functionalities in the app bar such as a navigation drawer, action button, and overflow menu. In this article, we will be implementing this library in an Android App using Java language. A sample GIF is
3 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
Android - Extract Data From JSON Array using Retrofit Library with Kotlin
In the android application, the response from the server is in the form of JSON. We can get to see either a JSON Array or a JSON Object in the response. JSON Array is the list of data which is having similar JSON objects. In the previous article, we have taken a look at How to parse data from JSON O
7 min read
Android - JSON Parsing Using Retrofit Library with Jetpack Compose
JSON is a format with the help of which we can exchange the data from the server within our application or a website. For accessing this data from the server within android applications. There are several libraries that are available such as Volley and Retrofit. In this article, we will take a look
9 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 build
3 min read