2

In the documentation it shows this example of how to execute a google cloud endpoints api request using the android client lib:

ScoreCollection scores = service.scores().list().execute();

Is there a way to cancel this request? I can't figure out how.

Sometimes I want to cancel long executing requests that I don't need anymore.

I know I could put it into a Thread of its own and cancel the thread, but that won't actually interrupt the .execute() method.

Speedy
  • 1
  • 1
  • 1
  • 14

1 Answers1

0

There are some ways to do it:

  1. wrap each request in a Thread class and interrupt the thread (or take Runnable/Future/ExecutorService these classes are more useful), important note: you have check the interruption status in your code and then exit the task).
  2. try RxJava
Maxim Firsoff
  • 1,134
  • 12
  • 21
  • I tried interrupting the thread using those methods and it does not interrupt it. Patterns like executor do not guarantee to stop if you interrupt them. – Speedy Sep 15 '20 at 00:45
  • Don't forget, JVM has a cooperative interruption mechanism, it means you have to check the interruption flag by your code (inside a task) and if it was set - return from the task. – Maxim Firsoff Sep 15 '20 at 05:36