Description
Fetch is a simple yet powerful Android library that allows you to manage downloads more efficiently in your Android apps. It uses a background service on the device to download and maintain requests.
Features
- Simple and easy to use API.
- Continuous downloading in the background.
- Ability to pause and resume downloads.
- Set the priority of a download request.
- Ability to retry failed downloads.
- Easy progress and status tracking.
- And more...
Fetch alternatives and similar packages
Based on the "Network" category.
Alternatively, view Fetch alternatives based on common mentions on social networks and blogs.
-
okhttp
Square’s meticulous HTTP client for the JVM, Android, and GraalVM. -
AndroidAsync
Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads. -
async-http-client
Asynchronous Http and WebSocket Client library for Java -
AndroidNetworking
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀 -
Android Volley
Official Android HTTP library that makes networking for easier and faster. -
robospice
Repo of the Open Source Android library : RoboSpice. RoboSpice is a modular android library that makes writing asynchronous long running tasks easy. It is specialized in network requests, supports caching and offers REST requests out-of-the box using extension modules. -
unirest-java
Unirest in Java: Simplified, lightweight HTTP client library. -
android-lite-http
LiteHttp is a simple, intelligent and flexible HTTP framework for Android. With LiteHttp you can make HTTP request with only one line of code! It could convert a java model to the parameter and rander the response JSON as a java model intelligently. -
OptimusHTTP
:satellite: [Android Library] Simplified async networking in android -
Rx.Network
Observe Android's CONNECTIVITY_CHANGE broadcasts using RxJava. -
networking
An android asynchronous http client built on top of HttpURLConnection. -
Minimized API Service Library
Minimized API library which is used call the server request in andorid. -
Packetzoom
SDK for optimizing HTTP requests and free analytics service for networking.
Appwrite - The Open Source Firebase alternative introduces iOS support
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of Fetch or a related project?
README
Overview
Fetch is a simple, powerful, customizable file download manager library for Android.
Features
- Simple and easy to use API.
- Continuous downloading in the background.
- Concurrent downloading support.
- Ability to pause and resume downloads.
- Set the priority of a download.
- Network-specific downloading support.
- Ability to retry failed downloads.
- Ability to group downloads.
- Easy progress and status tracking.
- Download remaining time reporting (ETA).
- Download speed reporting.
- Save and Retrieve download information anytime.
- Notification Support.
- Storage Access Framework, Content Provider and URI support.
- And more...
Prerequisites
If you are saving downloads outside of your application's sandbox, you will need to add the following storage permissions to your application's manifest. For Android SDK version 23(M) and above, you will also need to explicitly request these permissions from the user.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Also, as you are going to use Internet to download files. We need to add the Internet access permissions in the Manifest.
<uses-permission android:name="android.permission.INTERNET"/>
How to use Fetch
Using Fetch is easy! Just add the Gradle dependency to your application's build.gradle file.
implementation "com.tonyodev.fetch2:fetch2:3.0.12"
Androidx use:
implementation "androidx.tonyodev.fetch2:xfetch2:3.1.6"
Next, get an instance of Fetch and request a download.
public class TestActivity extends AppCompatActivity {
private Fetch fetch;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this)
.setDownloadConcurrentLimit(3)
.build();
fetch = Fetch.Impl.getInstance(fetchConfiguration);
String url = "http:www.example.com/test.txt";
String file = "/downloads/test.txt";
final Request request = new Request(url, file);
request.setPriority(Priority.HIGH);
request.setNetworkType(NetworkType.ALL);
request.addHeader("clientKey", "SD78DF93_3947&MVNGHE1WONG");
fetch.enqueue(request, updatedRequest -> {
//Request was successfully enqueued for download.
}, error -> {
//An error occurred enqueuing the request.
});
}
}
Tracking a download's progress and status is very easy with Fetch. Simply add a FetchListener to your Fetch instance, and the listener will be notified whenever a download's status or progress changes.
FetchListener fetchListener = new FetchListener() {
@Override
public void onQueued(@NotNull Download download, boolean waitingOnNetwork) {
if (request.getId() == download.getId()) {
showDownloadInList(download);
}
}
@Override
public void onCompleted(@NotNull Download download) {
}
@Override
public void onError(@NotNull Download download) {
Error error = download.getError();
}
@Override
public void onProgress(@NotNull Download download, long etaInMilliSeconds, long downloadedBytesPerSecond) {
if (request.getId() == download.getId()) {
updateDownload(download, etaInMilliSeconds);
}
int progress = download.getProgress();
}
@Override
public void onPaused(@NotNull Download download) {
}
@Override
public void onResumed(@NotNull Download download) {
}
@Override
public void onCancelled(@NotNull Download download) {
}
@Override
public void onRemoved(@NotNull Download download) {
}
@Override
public void onDeleted(@NotNull Download download) {
}
};
fetch.addListener(fetchListener);
//Remove listener when done.
fetch.removeListener(fetchListener);
Fetch supports pausing and resuming downloads using the request's id. A request's id is a unique identifier that maps a request to a Fetch Download. A download returned by Fetch will have have an id that matches the request id that started the download.
Request request1 = new Request(url, file);
Request request2 = new Request(url2, file2);
fetch.pause(request1.getId());
...
fetch.resume(request2.getId());
You can query Fetch for download information in several ways.
//Query all downloads
fetch.getDownloads(new Func<List<? extends Download>>() {
@Override
public void call(List<? extends Download> downloads) {
//Access all downloads here
}
});
//Get all downloads with a status
fetch.getDownloadsWithStatus(Status.DOWNLOADING, new Func<List<? extends Download>>() {
@Override
public void call(List<? extends Download> downloads) {
//Access downloads that are downloading
}
});
// You can also access grouped downloads
int groupId = 52687447745;
fetch.getDownloadsInGroup(groupId, new Func<List<? extends Download>>() {
@Override
public void call(List<? extends Download> downloads) {
//Access grouped downloads
}
});
When you are done with an instance of Fetch, simply release it.
//do work
fetch.close();
//do more work
Downloaders
By default Fetch uses the HttpUrlConnection client via the HttpUrlConnectionDownloader to download requests. Add the following Gradle dependency to your application's build.gradle to use the OkHttp Downloader instead. You can create your custom downloaders if necessary. See the Java docs for details.
implementation "com.tonyodev.fetch2okhttp:fetch2okhttp:3.0.12"
Androidx use:
implementation "androidx.tonyodev.fetch2okhttp:xfetch2okhttp:3.1.6"
Set the OkHttp Downloader for Fetch to use.
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this)
.setDownloadConcurrentLimit(10)
.setHttpDownloader(new OkHttpDownloader(okHttpClient))
.build();
Fetch fetch = Fetch.Impl.getInstance(fetchConfiguration);
RxFetch
If you would like to take advantage of RxJava2 features when using Fetch, add the following gradle dependency to your application's build.gradle file.
implementation "com.tonyodev.fetch2rx:fetch2rx:3.0.12"
Androidx use:
implementation "androidx.tonyodev.fetch2rx:xfetch2rx:3.1.6"
RxFetch makes it super easy to enqueue download requests and query downloads using rxJava2 functional methods.
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this).build();
Rxfetch rxFetch = RxFetch.Impl.getInstance(fetchConfiguration);
rxFetch.getDownloads()
.asFlowable()
.subscribe(new Consumer<List<Download>>() {
@Override
public void accept(List<Download> downloads) throws Exception {
//Access results
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
//An error occurred
final Error error = FetchErrorUtils.getErrorFromThrowable(throwable);
}
});
FetchFileServer
Introducing the FetchFileServer. The FetchFileServer is a lightweight TCP File Server that acts like an HTTP file server designed specifically to share files between Android devices. You can host file resources with the FetchFileServer on one device and have to Fetch download Files from the server on another device. See the sample app for more information. Wiki on FetchFileServer will be added in the coming days.
Start using FetchFileServer by adding the gradle dependency to your application's build.gradle file.
implementation "com.tonyodev.fetch2fileserver:fetch2fileserver:3.0.12"
Androidx use:
implementation "androidx.tonyodev.fetch2fileserver:xfetch2fileserver:3.1.6"
Start a FetchFileServer instance and add resource files that it can serve to connected clients.
public class TestActivity extends AppCompatActivity {
FetchFileServer fetchFileServer;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fetchFileServer = new FetchFileServer.Builder(this)
.build();
fetchFileServer.start(); //listen for client connections
File file = new File("/downloads/testfile.txt");
FileResource fileResource = new FileResource();
fileResource.setFile(file.getAbsolutePath());
fileResource.setLength(file.length());
fileResource.setName("testfile.txt");
fileResource.setId(UUID.randomUUID().hashCode());
fetchFileServer.addFileResource(fileResource);
}
@Override
protected void onDestroy() {
super.onDestroy();
fetchFileServer.shutDown(false);
}
}
Downloading a file from a FetchFileServer using the Fetch is easy.
public class TestActivity extends AppCompatActivity {
Fetch fetch;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this)
.setFileServerDownloader(new FetchFileServerDownloader()) //have to set the file server downloader
.build();
fetch = Fetch.Impl.getInstance(fetchConfiguration);
fetch.addListener(fetchListener);
String file = "/downloads/sample.txt";
String url = new FetchFileServerUrlBuilder()
.setHostInetAddress("127.0.0.1", 6886) //file server ip and port
.setFileResourceIdentifier("testfile.txt") //file resource name or id
.create();
Request request = new Request(url, file);
fetch.enqueue(request, request1 -> {
//Request enqueued for download
}, error -> {
//Error while enqueuing download
});
}
@Override
protected void onResume() {
super.onResume();
fetch.addListener(fetchListener);
}
@Override
protected void onPause() {
super.onPause();
fetch.removeListener(fetchListener);
}
@Override
protected void onDestroy() {
super.onDestroy();
fetch.close();
}
private FetchListener fetchListener = new AbstractFetchListener() {
@Override
public void onProgress(@NotNull Download download, long etaInMilliSeconds, long downloadedBytesPerSecond) {
super.onProgress(download, etaInMilliSeconds, downloadedBytesPerSecond);
Log.d("TestActivity", "Progress: " + download.getProgress());
}
@Override
public void onError(@NotNull Download download) {
super.onError(download);
Log.d("TestActivity", "Error: " + download.getError().toString());
}
@Override
public void onCompleted(@NotNull Download download) {
super.onCompleted(download);
Log.d("TestActivity", "Completed ");
}
};
}
Fetch1 Migration
Migrate downloads from Fetch1 to Fetch2 using the migration assistant. Add the following gradle dependency to your application's build.gradle file.
implementation "com.tonyodev.fetchmigrator:fetchmigrator:3.0.12"
Androidx use:
implementation "androidx.tonyodev.fetchmigrator:xfetchmigrator:3.1.6"
Then run the Migrator.
if (!didMigrationRun()) {
//Migration has to run on a background thread
new Thread(new Runnable() {
@Override
public void run() {
try {
final List<DownloadTransferPair> transferredDownloads = FetchMigrator.migrateFromV1toV2(getApplicationContext(), APP_FETCH_NAMESPACE);
//TODO: update external references of ids
for (DownloadTransferPair transferredDownload : transferredDownloads) {
Log.d("newDownload", transferredDownload.getNewDownload().toString());
Log.d("oldId in Fetch v1", transferredDownload.getOldID() + "");
}
FetchMigrator.deleteFetchV1Database(getApplicationContext());
setMigrationDidRun(true);
//Setup and Run Fetch2 after the migration
} catch (SQLException e) {
e.printStackTrace();
}
}
}).start();
} else {
//Setup and Run Fetch2 normally
}
Contribute
Fetch can only get better if you make code contributions. Found a bug? Report it. Have a feature idea you'd love to see in Fetch? Contribute to the project!
License
Copyright (C) 2017 Tonyo Francis.
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.
*Note that all licence references and agreements mentioned in the Fetch README section above
are relevant to that project's source code only.