Fast Networking Library - Top JSON Examples.


Android Fast Networking Library JSON Examples.

What is Fast Networking Library?

Fast Android Networking Library is a Complete Android Networking Library that also supports HTTP/2 created by Amit Shekhar, a popular open source contributor.

It provides us with powerful networking capabilities yet in an elegant manner. It's a mature library and is currently one of the most popular networking library.

Fast Android Networking Library allows us perform any type of networking in Android applications.

It's a library built on top of OkHttp Networking Layer.

Fast Android Networking Library does alot of work for you in the background, such that you only make your request and listen for the response.

Top Reasons to start using Fast Android Networking Library

Here are some of the reasons why you need to consider using this library for networking needs in android.

  1. Android Framework Engineers in the recent years got rid of HttpClient, that is in Android Marshmallow(Android M). Because of this alot of networking libraries like Volley became obsolete.
  2. Fast Android Networking does allows you do these things easier than any other library:

    1. Making HTTP requests.
    2. Downloading any type of file.
    3. Uploading files.
    4. Loading image from network into ImageView, etc.
  3. Fast Networking Library provides a simple interface for doing all types of things in networking like setting priority, cancelling, etc.
  4. Fast Networking Library uses Okio hence no more GC overhead in android applications. Okio is made to handle GC overhead while allocating memory. Okio does some clever things to save CPU and memory.
  5. Fast Networking Library is built on top of the proven OkHttp.
  6. Fast Networking Libary supports HTTP/2.
Requirements

Fast Android Networking Library is generous with it's requirements.

To use it you need:

  1. Android 2.3 (Gingerbread) and later.
Fast Networking Libary Installation

To insall Fast Networking Library, head over to your app level and add it as a dependency:

implementation 'com.amitshekhar.android:android-networking:1.0.2'

Then in your android manifest don't forget to add permission:

<uses-permission android:name="android.permission.INTERNET" />

Using it in Java

Well once you've installed it you can head over to your java code and initialize it first:

AndroidNetworking.initialize(getApplicationContext());
1. How to make a HTTP GET Request in Fast Networking Library

You invoke the AndroidNetworking.get() method:

AndroidNetworking.get("https://fierce-cove-29863.herokuapp.com/getAllUsers/{pageNumber}")
                 .addPathParameter("pageNumber", "0")
                 .addQueryParameter("limit", "3")
                 .addHeaders("token", "1234")
                 .setTag("test")
                 .setPriority(Priority.LOW)
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                      // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error
                    }
                });  
2. How to make a HTTP POST Request in Fast Networking Library

You invoke the AndroidNetworking.post() method:

AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createAnUser")
                 .addBodyParameter("firstname", "Amit")
                 .addBodyParameter("lastname", "Shekhar")
                 .setTag("test")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                      // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error
                    }
                });
3. How to Download a File in Fast Networking Library

You invoke the AndroidNetworking.download() method:

AndroidNetworking.download(url,dirPath,fileName)
                 .setTag("downloadTest")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .setDownloadProgressListener(new DownloadProgressListener() {
                    @Override
                    public void onProgress(long bytesDownloaded, long totalBytes) {
                      // do anything with progress  
                    }
                 })
                 .startDownload(new DownloadListener() {
                    @Override
                    public void onDownloadComplete() {
                      // do anything after completion
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error    
                    }
                });  
4. How to Upload a File to Server in Fast Networking Library

You invoke the AndroidNetworking.upload() method:

AndroidNetworking.upload(url)
                 .addMultipartFile("image",file)    
                 .addMultipartParameter("key","value")
                 .setTag("uploadTest")
                 .setPriority(Priority.HIGH)
                 .build()
                 .setUploadProgressListener(new UploadProgressListener() {
                    @Override
                    public void onProgress(long bytesUploaded, long totalBytes) {
                      // do anything with progress 
                    }
                 })
                 .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                      // do anything with response                
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error 
                    }
                 }); 

Top Android Fast Networking Library JSON Examples

Let's look at some examples.

How to populate custom gridview with json data containing images and text.

In this tutorial we want to se how to download some JSON data from online asynchronously, parse that data and bind to a custom gridview with cardviews.

We are fetching images and text in our gridview. So our custom gridview will have cardviews that render for us images and text in a nice way.

We will be using the Fast Networking library which is built on top OkHttp. This will provide us with a fast and easy to work networking capability.

We are making a HTTP GET request. As our data is downloaded, we will show an indeterminate progressbar will be hidden automatically when the data download is finish.

This allows us to show user the download progress. In case of any error, we will show a Toast message containing details about the error so that you can view it at runtime, which is very important for newbies.

Let's start.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post