Cancelling a google app engine endpoint request - android

Im currently using GAE from Android. I have a search operation which performs what might be a long running query on the datastore (called from AsyncTask). The user has an option to logout from the app while this process is still running. Without an option to cancel this request the actual logout has to wait until the process finishes and the control is back to the client (which, as i said, can take a few seconds).
Is there a way to cancel a call to a GAE endpoint while the server is running and return the control to the client ? Thanks.
p.s.
Obviously, cancelling the AsyncTask only is not enough.

Personally, I put the server comm in a service so that requests can (usually) finish regardless of what the user does. In your case, could you let the user log out, but also let the request complete?
Otherwise, I don't know how to cleanly interrupt an endpoint request. Unfortunately, the actual blocking operation is buried in the endpoints (Google API Client library) code and no cancel operation is exposed that I'm aware of.
When you write that cancelling the task - AsyncTask.cancel(boolean mayInterruptIfRunning) - is not enough, is that because you've tried it and it doesn't interrupt a blocked operation.

No, Google Endpoints use servlets and servlets have no (standard) way of telling a running request to cancel it's work. You will need to build something proprietary.
For example: in your long-running process on server you in it's main loop check if user is still logged in (via a flag in the datastore or memcache). If this flag tells you that user is logged out, then you cancel the processing and return. Also a login/logout procedure must appropriately set this flag.

Related

How to cancel google cloud endpoints request in Android using the generated client library?

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.
There are some ways to do it:
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).
try RxJava

Out-of-box solution for Android for guaranteed delivery of http requests

I have an Android application that has a screen for displaying information about any user of the service for which mobile application was developed. Users are able to subscribe to other users on that screen.
Current solution with Rx and Retrofit works fine but when user leave the screen I clear disposables and then the http call under the hood of Retrofit is being canceled. So if user leaves the screen his operation on it (such as subscribing or likinng/disliking) get lost.
I want to keep that requests which modify some data (usually POST-request) and send them until backend of my application will receive it and send a response. Moreover I want to persist that request on disk to survive the app's process death. It means that if user leaves screen and system kills my app then after it launched again it will perform an attempt to send saved requests. Of course if there is an internet connection.
I wonder if out-of-box solution for that purposes exists. I appreciate any advice.
Workmanager can be used to schedule this kind of request.

Long running HTTP task & callback in Android

I have a requirement in my Android app, that makes an HTTP request to the server to perform a long running task. The architecture on the server is that; the HTTP request returns immediately with a transaction ID. What I need next is to poll the server to check whether the job has completed and once completed show the result to the user.
When the first HTTP task completes I show a UI Fragment which has a progress bar asking the user to wait while the task completes. Ideally, I would like to show the result to the user on the same Fragment once the task has been done. But I would also need to consider situations where the user might press the Home button or move out of the app/screen in some way.
My current solution is:
Poll every 10-15s with some sort of exponential backoff, as long as
the activity is still attached.
The server sends a push notification regarding task completion - but
I'm worried about reliability of this.
Is there a better way to achieve this? If so, what would be a good design pattern for this? Are there any libraries available that handles this scenario?

How to call mobile service in an android app via background service or queue them if offline

Detail:
I have developed an android native application on ADT.
App performs ADD , Search and update operations on SQL database hosted on Windows Azure cloud platform.
All the operations are performed by calling a Mobile Service built on Windows Azure Cloud platform.
Problem Description:
Every time I want to ADD a new record via UI of app, my app calls mobile service and get hang till the operation completes.
I want to do this operation but do not want user to wait for it to complete.
Additionally when there is no network, user addition should be kept in queue for later addition.
Ideal Scenario: I want to display Addition operation in pending status to user and make that as a background process till the time it completes.
Note: There can be many ADD operation going on by multiple users. Need to keep that in mind.
Please suggest options to do this.
Free to ask questions in case of any query regarding problem statement.
Thanks
Anshul
You have to use AsyncTask to do work in the background and update the UI for the User or Pop a notification in OnPostExecute which means background work has been done.
For Queuing when No Network:
You would have to user a Service with BroadCastReceiver which listener for Network-Connectivity. Once the BroadCast Receiver Receives that Internet is Connected, You can then start calling the mobile service.
But Let me add that the Queued Data has to be Saved so you would have to create a SQlLite database or save the Data to a file on the Phone, which you can retrieve the data from, when the BroadCastReceiver is notified that connection is back

Android rest process in background

I have a server running on some where and an Android application for end user. From Android application user can delete message, and this delete message will trigger sending a delete request to server through REST and server will delete it.
Does anyone know how the gmail's delete message works? Even if I quit from app or move away from app the send, delete or other operations completes eventually. Are they using AsyncTask or Thread or Service. I guess its not AsyncTask since user can move away from current view or can move away from whole application.
any suggestion is appreciated.
You may want to look at IntentService.
http://developer.android.com/training/run-background-service/create-service.html
"The IntentService class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask"
I'm not sure how Gmail's REST API works, but for REST calls, AsyncTask is definitely not the way to go. Why reinvent the wheel? Take a look at Volley or RetroFit. They are both REST libraries that take into account a lot of pitfalls one encounters in implementing REST calls in Android.

Categories

Resources