Fast Android Networking Library is a powerful library for doing any type of networking in Android applications which is made on top of OkHttp Networking Layer.
Fast Android Networking Library takes care of each and everything. So you don’t have to do anything, just make request and listen for the response.
Why use Fast Android Networking ?
Recent removal of HttpClient in Android Marshmallow(Android M) made other networking library obsolete.
No other single library do each and everything like making request, downloading any type of file, uploading file, loading image from network in ImageView, etc. There are libraries but they are outdated.
No other library provided simple interface for doing all types of things in networking like setting priority, cancelling, etc.
As it uses Okio , No more GC overhead in android application. Okio is made to handle GC overhead while allocating memory. Okio do some clever things to save CPU and memory.
As it uses OkHttp , most important it supports HTTP/2.
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(new UploadProgressListener() { @Override publicvoidonProgress(long bytesUploaded, long totalBytes){ // do anything with progress } }) .getAsJSONObject(new JSONObjectRequestListener() { @Override publicvoidonResponse(JSONObject response){ // below code will be executed in the executor provided // do anything with response } @Override publicvoidonError(ANError error){ // 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(new DownloadProgressListener() { @Override publicvoidonProgress(long bytesDownloaded, long totalBytes){ // do anything with progress } }) .startDownload(new DownloadListener() { @Override publicvoidonDownloadComplete(){ // do anything after completion } @Override publicvoidonError(ANError error){ // handle error } });
Cancelling a request.
Any request with a given tag can be cancelled. Just do like this.
1 2 3 4 5 6
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.
// Adding Listener AndroidNetworking.setConnectionQualityChangeListener(new ConnectionQualityChangeListener() { @Override publicvoidonChange(ConnectionQuality currentConnectionQuality, int currentBandwidth){ // do something on change in connectionQuality } });
// Enabling GZIP for Request (Not needed if your server doesn't support GZIP Compression), anyway responses // from server are automatically unGzipped if required. So enable it only if you need your request to be // Gzipped before sending to server(Make sure your server support GZIP Compression). OkHttpClient okHttpClient = new OkHttpClient().newBuilder() .addInterceptor(new GzipRequestInterceptor()) .build(); AndroidNetworking.initialize(getApplicationContext(),okHttpClient);
IMPORTANT NOTE
Use IMMEDIATE Priority with caution - use is at appropriate place only when 1 or 2 (at max 2)IMMEDIATE request is required at instant.Otherwise use HIGH Priority.
Known Bug : As present if you are using GZIP Interceptor from client to server, Upload progress is not working perfectly in Multipart.
If you are using Proguard with Gradle build system (which is usually the case), you don’t have to do anything. The appropriate Proguard rules will be automatically applied. If you still need the rules applied in proguard-rules.pro, it is as follows:
1
-dontwarn okio.**
Fast Android Networking Library supports
Fast Android Networking Library supports all types of HTTP/HTTPS request like GET, POST, DELETE, HEAD, PUT, PATCH
Fast Android Networking Library supports downloading any type of file
Fast Android Networking Library supports uploading any type of file (supports multipart upload)
Fast Android Networking Library supports cancelling a request
Fast Android Networking Library supports setting priority to any request (LOW, MEDIUM, HIGH, IMMEDIATE)
Copyright (C) 2016 Amit Shekhar Copyright (C) 2011 Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.