0

What is the best way to make a deferred simple okhttp3 web request like:

Request request = Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();

that gets executed as soon as a client goes online and only then to handle result?

For example, client makes change to the data locally and this change should be posted on server. But there are cases when user does it offline, but we still have to handle it.

ildar ishalin
  • 685
  • 1
  • 9
  • 19

1 Answers1

1

You can use a broadcast receiver which listen for internet connectivity changes, and Deferred library for deferring the request Jdeferred.

If use of deferred is too much work and overkill, You can use a conutdownlatch and wait on separate thread until, the connectivity change, but the wait time can be huge, so chose wisely.

You can also consider CompletableFuture. But this is available from api 24 only

Let me know if you need more clarification.

Community
  • 1
  • 1
Nishant Verma
  • 110
  • 2
  • 8