Fast Android Networking Library Kotlin



Requirements

Fast Android Networking Library can be included in any Android application.
Fast Android Networking Library supports Android 2.3 (Gingerbread) and later.

Using Fast Android Networking Library in your application

Add this in your build.gradle

compile 'com.amitshekhar.android:android-networking:1.0.0'
 
Do not forget to add internet permission in manifest if already not present

<uses-permission android:name="android.permission.INTERNET" />
 
Then initialize it in onCreate() Method of application class :

AndroidNetworking.initialize(applicationContext)
 

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(object : JSONArrayRequestListener {
            override fun onResponse(response: JSONArray) {
                // do anything with response            }

            override fun onError(error: ANError) {
                // handle error            }
        })
 

Making a POST Request

 AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createAnUser")
        .addBodyParameter("firstname", "Amit")
        .addBodyParameter("lastname", "Shekhar")
        .setTag("test")
        .setPriority(Priority.MEDIUM)
        .build()
        .getAsJSONObject(object : JSONObjectRequestListener {
            override fun onResponse(response: JSONObject) {
                // do anything with response            }

            override fun onError(error: ANError) {
                // handle error            }
        })

Downloading a file from server

AndroidNetworking.download(url, dirPath, fileName)
        .setTag("downloadTest")
        .setPriority(Priority.MEDIUM)
        .build()
        .setDownloadProgressListener { bytesDownloaded, totalBytes ->            // do anything with progress        }        .startDownload(object : DownloadListener {
            override fun onDownloadComplete() {
                // do anything after completion            }

            override fun onError(error: ANError) {
                // handle error            }
        })

Uploading a file to server

 

AndroidNetworking.upload(url)
        .addMultipartFile("image", file)
        .addMultipartParameter("key", "value")
        .setTag("uploadTest")
        .setPriority(Priority.HIGH)
        .build()
        .setUploadProgressListener { bytesUploaded, totalBytes ->            // do anything with progress         }        .getAsJSONObject(object : JSONObjectRequestListener {
            override fun onResponse(response: JSONObject) {
                // do anything with response                            }

            override fun onError(error: ANError) {
                // handle error             }
        })

Getting Response and completion in an another thread executor

(Note : Error and Progress will always be returned in main thread of application)

AndroidNetworking.upload(url)
        .addMultipartFile("image", file)
        .addMultipartParameter("key", "value")
        .setTag("uploadTest")
        .setPriority(Priority.HIGH)
        .build()
        .setExecutor(Executors.newSingleThreadExecutor()) // setting an executor to get response or completion on that executor thread        .setUploadProgressListener(UploadProgressListener { bytesUploaded, totalBytes ->            // do anything with progress        })
        .getAsJSONObject(object : JSONObjectRequestListener {
            override fun onResponse(response: JSONObject) {
                // below code will be executed in the executor provided                // do anything with response            }

            override fun onError(error: ANError) {
                // handle error            }
        })

Setting a Percentage Threshold For Not Cancelling the request if it has completed the given threshold

 

AndroidNetworking.download(url, dirPath, fileName)
        .setTag("downloadTest")
        .setPriority(Priority.MEDIUM)
        .setPercentageThresholdForCancelling(50) // even if at the time of cancelling it will not cancel if 50%         .build()                                 // downloading is done.But can be cancalled with forceCancel.        .setDownloadProgressListener { bytesDownloaded, totalBytes ->            // do anything with progress          }        .startDownload(object : DownloadListener {
            override fun onDownloadComplete() {
                // do anything after completion            }

            override fun onError(error: ANError) {
                // handle error                }
        })
 

Cancelling a request.

Any request with a given tag can be cancelled. Just do like this.

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

 Source: Fast-Android-Networking

 

 


 

Comments

Popular posts from this blog

Kotlin Android Extensions

Android Easy Runtime Permissions with Dexter