简体   繁体   English

如何取消Google Cloud Endpoint端点请求

[英]How to cancel Google Cloud Endpoint Endpoint request

I'm using Google Cloud Endpoint Java api backend and connected Android application in android studio. 我在android studio中使用Google Cloud Endpoint Java api后端并连接了Android应用程序。

I'm making call to my backend api from the android app to check username availability. 我正在从android应用程序调用我的后端api,以检查用户名的可用性。

username is checked on every text change event. 在每个文本更改事件中都会检查用户名。

So when the text is updated, I want the previous request to be cancelled. 因此,在更新文本时,我希望取消上一个请求。

How do I achieve it? 我该如何实现?

One method I thought of was sending a random string with the request to server, and ignore all the received responses, except the latest. 我想到的一种方法是将带有请求的随机字符串发送到服务器,并忽略所有接收到的响应(最新的响应除外)。 But this doesn't cancel the request, and network is still receiving the response. 但这不会取消请求,并且网络仍在接收响应。 Also I have to send some extra data to keep track of requests. 另外,我还必须发送一些额外的数据来跟踪请求。

So, how to cancel it? 那么,如何取消呢? Is there any way to achieve it with google cloud endpoint api? 有什么方法可以使用Google Cloud Endpoint API实现它吗?

Additional info: I'm making google cloud endpoint request using an AsyncTask in android. 附加信息:我正在使用Android中的AsyncTask发出Google Cloud端点请求。 I read something about cancelling the async task. 我读了一些有关取消异步任务的内容。 Can that be used here somehow? 可以在这里使用吗?

Cancelling a request on the API is not a choice because it will most likely be processed and on the way back by the time your cancellation order reaches the server. 取消对API的请求不是一种选择,因为它很可能会在您的取消订单到达服务器时被处理,并在返回途中被处理。 Cancelling the Asynctask might be an option but you'll still have to account for threading and network latency hiccups. 取消Asynctask可能是一种选择,但是您仍然必须考虑线程和网络延迟问题。

I think the safest and easiest way is the one you already proposed, having a sequence added to your request and only update the UI if you receive the latest reply in the sequence. 我认为最安全,最简单的方法是您已经提出的方法,将序列添加到请求中,并且仅当您收到序列中的最新答复时才更新UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM