Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to queue API calls when you're offline while using Retrofit

I'm using Retrofit in combination with rx-java and have the following question:

Is there a way to queue API calls to a server when the device is offline so that they can be fired when the device is back online again.

like image 857
Ben Groot Avatar asked Aug 05 '15 11:08

Ben Groot


People also ask

Is retrofit a REST API?

Retrofit is type-safe REST client for Android and Java which aims to make it easier to consume RESTful web services.

What is enqueue in retrofit?

enqueue(Callback<T> callback) Asynchronously send the request and notify callback of its response or if an error occurred talking to the server, creating the request, or processing the response. Response<T> execute() Synchronously send the request and return its response.

Is retrofit REST?

Retrofit is a REST Client for Java and Android allowing to retrieve and upload JSON (or other structured data) via a REST based You can configure which converters are used for the data serialization, example GSON for JSON.


2 Answers

EDIT: March 2021 you should now use Work Manager instead.


You need to add queue to your project in the first place and pipe Retrofit jobs thru that queue. So either create own queue or check if libraries like android-priority-jobqueue fit your requirements (that one also handles persistency of queued jobs, so app kill/reboot is less painful)

like image 58
Marcin Orlowski Avatar answered Sep 21 '22 12:09

Marcin Orlowski


I think Marcin Orlowski's answer was good. But if you want to go with rxjava, you can use .retry() with check, are you online, inside of it.

rxjava: Can I use retry() but with delay?

like image 26
Stas Parshin Avatar answered Sep 22 '22 12:09

Stas Parshin