Skip to content
Related Articles

Related Articles

How to Use Fast Android Networking Library in Android with Example?

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • 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: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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!