
Requirements
It supports Android 2.3 (Gingerbread) and later.
Using this Library in your application
compile 'com.amitshekhar.android:android-networking:1.0.0'
<uses-permission android:name="android.permission.INTERNET" />
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 the 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 the 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
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 }
})
Canceling a request.
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