Fast Android Networking (FAN) Library and How to Use

Fast Android Networking – Have you ever heard of FAN or Android Fast Networking ? Yes, Android Fast Networking is one of the libraries that can be used to exit requests to the network/interne

In addition to Fast Android Networking, there are many similar libraries that are often used, including Retrofit , Volley , Loopj , and OkHttp.

Everything is the same, it can be used to connect to the network / internet as well as to request API rest, here I will explain in detail about this library

What is FAN (Fast Android Networking) ?

Fast Android Networking is a powerful library for making network requests in Android applications built on top of the OkHttp Networking Layer.

Features owned by FAN (Android Fast Networking)

Besides being powerful and easy to use, the basic difference between this FAN and other libraries is in terms of features, along with the features offered by FAN

  1. Unlike other libraries, FAN supports all types of HTTP/HTTPS requests such as GET, POST, DELETE, HEAD, PUT, PATCH.
  2. Faster connection because it uses OkHttp and Okio
  3. Support for download, upload, and multipart
  4. Support Parsing JSON to Java Object and support Jackson Parser
  5. Caching management that can reduce bandwidth usage
  6. Simple code and easy to use
  7. Support RxJava

Also Read: Android Shared Preferences

How to use Fast Android Networking

Included in the features of this FAN is a simple and easy-to-use code, before using this library, you must first add this library to your project, add this line of code in the build.gradle file

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

Then add permissions for access to the Internet in AndroidManifest.xml

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

Then initialize it in Activity by adding the following line of code

AndroidNetworking. initialize ( getApplicationContext ()) ;

If all of the above requirements have been met, here are some examples of requests that can be used with Fast Android Networking:

Making a GET Request

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
                    }
                });  

Making POST Requests

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
                    }
                });

You can also post java objects, json, files, etc in a POST request like this.

User user = new User();
user.firstname = "Amit";
user.lastname = "Shekhar";

AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createUser")
                 .addBodyParameter(user) // posting java object
                 .setTag("test")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                      // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error
                    }
                });


JSONObject jsonObject = new JSONObject();
try {
    jsonObject.put("firstname", "Amit");
    jsonObject.put("lastname", "Shekhar");
} catch (JSONException e) {
  e.printStackTrace();
}
       
AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createUser")
                 .addJSONObjectBody(jsonObject) // posting json
                 .setTag("test")
                 .setPriority(Priority.MEDIUM)
                 .build()
                 .getAsJSONArray(new JSONArrayRequestListener() {
                    @Override
                    public void onResponse(JSONArray response) {
                      // do anything with response
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error
                    }
                });
                
AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/postFile")
                 .addFileBody(file) // posting any type of file
                 .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
                    }
                });               

Download files from Server

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    
                    }
                });  

Upload files to Server

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 
                    }
                 }); 

Cancel Request

AndroidNetworking.cancel("tag"); // All requests with the given tag will be cancelled. 
AndroidNetworking.forceCancel("tag"); // All requests with given tag will be cancelled, even if any percent threshold is set, it will be force cancelled. AndroidNetworking.cancelAll(); // All requests will be cancelled. 
AndroidNetworking.forceCancelAll(); // All requests will be cancelled, even if there is a percent threshold, it will be force cancelled. 

Loading images from the Internet into ImageView

<com .androidnetworking.widget.ANImageView
           android : id = " @ + id / imageView "
           android : layout_width = " 100dp "
           android : layout_height = " 100dp "
           android : layout_gravity = " center " />

In the java file

imageView.setDefaultImageResId (R.drawable. default); 
      imageView.setErrorImageResId (R.drawable.error); 
      imageView.setImageUrl (imageUrl);   

Getting Bitmap from url with some specified parameters

AndroidNetworking.get(imageUrl)
                 .setTag("imageRequestTag")
                 .setPriority(Priority.MEDIUM)
                 .setBitmapMaxHeight(100)
                 .setBitmapMaxWidth(100)
                 .setBitmapConfig(Bitmap.Config.ARGB_8888)
                 .build()
                 .getAsBitmap(new BitmapRequestListener() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                    // do anything with bitmap
                    }
                    @Override
                    public void onError(ANError error) {
                      // handle error
                    }
                });

Error Code Handling

public void onError ( ANError error ) {   
   if ( error. getErrorCode () != 0 ) {  
       // received error from server 
        // error.getErrorCode() - error code from server 
        // error.getErrorBody() - error body from server 
        // error.getErrorDetail() - error details only 
        Logs. d ( TAG, "onError errorCode : " + error. getErrorCode ()) ;
        Logs. d ( TAG, "onError errorBody : " + error. getErrorBody ()) ;
        Logs. d ( TAG, "onError errorDetail : " + error. getErrorDetail ()) ;
        // get parsed error object (If ApiError is your class) 
        ApiError apiError = error. getErrorAsObject ( ApiError. class ) ;
   } else {  
        // error.getErrorDetail() : connectionError, parseError, requestCancelledError
        Logs. d ( TAG, "onError errorDetail : " + error. getErrorDetail ()) ;
   }
}

Clear Bitmap from cache or clear cache

AndroidNetworking. evictBitmap ( key ) ; // delete bitmap with key from LruCache
AndroidNetworking. evictAllBitmap () ; // clear LruCache 

reference

Conclusion

This Fast Android Networking (FAN) library has many advantages compared to other networking libraries, but also does not rule out the possibility of having weaknesses compared to other libraries.

The use of networking libraries should be adapted to existing needs. Try to try other libraries like Retrofit , Volley , Loopj , and OkHttp to feel the difference

Because this library works on top of OkHttp it makes it a fast, powerful, and also concise networking library in terms of lines of code

With the various features it has, I think this library is a networking library that is worthy of you to try

If you want to see my projects that use this library, you can see the previous article on RecycleView With Rest API (Java) or RecycleView With Rest API (Kotlin)

I’ve had enough of this discussion about the Fast Android Networking (FAN) Library , hopefully it can be a reference and useful for all of you

Related Articles

Leave a Reply

Back to top button