根据 RESTful 查询结果,启动特定的 activity
Based on RESTful query results, Launch a specific activity
我在 Android Studio 中有一个 Cloud Endpoints 生成的库。 Endpoints 库与我拥有的 Cloud SQL 数据库交互。
在我的 Android 应用程序中,我有一个带有按钮的 MainActivity。单击此按钮会将您带到 ActivityA。
但是,我的云 SQL 数据库中有一个 table,我想在单击此按钮时查询它。如果 table 有某个条目,我想改为启动 ActivityB。
因此默认情况下单击按钮会启动 ActivityA,但如果数据库中存在某些内容,我想启动 ActivityB。
我应该如何设置它,以便当您单击我的按钮时,端点 API(这是一个 REST API)查询数据库,并根据查询结果启动 ActivityA或活动 B?
我问的原因是因为我知道您应该将 Restful 调用放入 AsyncTask 以防止 UI 挂断。但是,AsyncTasks 运行 在后台,所以当它处理查询时,可能为时已晚,ActivityA 将启动。
如果您想根据 Google Cloud Endpoint ApiMethod 响应的结果开始新的 activity 您可以使用 onPostExecute(Result) like HelloEndpoints 示例等待显示 Toast。在该示例中 android 客户端等待 sayHi apiMethod 的 MyBean 响应的字符串值。
但是根据你的评论。
But there may be another situation where the database entry may change right in between the MainActivity being loaded and the user clicking the button.
我认为您尝试实现的目标无法通过使用 REST 服务来完成,因为将响应发送回您的 android 客户端所花费的时间。假设 Endpoint 向您的客户端发送响应,您的客户端在 1 秒后收到响应。在那一秒的时间段内,可以更改数据库值。
我在 Android Studio 中有一个 Cloud Endpoints 生成的库。 Endpoints 库与我拥有的 Cloud SQL 数据库交互。
在我的 Android 应用程序中,我有一个带有按钮的 MainActivity。单击此按钮会将您带到 ActivityA。
但是,我的云 SQL 数据库中有一个 table,我想在单击此按钮时查询它。如果 table 有某个条目,我想改为启动 ActivityB。
因此默认情况下单击按钮会启动 ActivityA,但如果数据库中存在某些内容,我想启动 ActivityB。
我应该如何设置它,以便当您单击我的按钮时,端点 API(这是一个 REST API)查询数据库,并根据查询结果启动 ActivityA或活动 B?
我问的原因是因为我知道您应该将 Restful 调用放入 AsyncTask 以防止 UI 挂断。但是,AsyncTasks 运行 在后台,所以当它处理查询时,可能为时已晚,ActivityA 将启动。
如果您想根据 Google Cloud Endpoint ApiMethod 响应的结果开始新的 activity 您可以使用 onPostExecute(Result) like HelloEndpoints 示例等待显示 Toast。在该示例中 android 客户端等待 sayHi apiMethod 的 MyBean 响应的字符串值。
但是根据你的评论。
But there may be another situation where the database entry may change right in between the MainActivity being loaded and the user clicking the button.
我认为您尝试实现的目标无法通过使用 REST 服务来完成,因为将响应发送回您的 android 客户端所花费的时间。假设 Endpoint 向您的客户端发送响应,您的客户端在 1 秒后收到响应。在那一秒的时间段内,可以更改数据库值。