如何在 google 个端点中将数据从后端传递到客户端?
How to pass data from backend to client in google endpoints?
在我的 android 应用程序的端点 class 中,我 return 一个 Profile 对象,它是一个实体。我想将数据传回前端。
这样做的最佳方法是什么,模型应该放在哪里
存储在前端还是后端,或者两者都存储?
编辑:
例如,在异步任务中,我执行以下操作:
try {
return mApi.saveProfile(firstName, lastName, birthday, userId).execute().getFirstName();
} catch (IOException e) {
return e.getMessage();
}
这会调用端点后端方法 saveProfile
,该方法位于 android 应用程序的单独模块中。该方法执行此操作:
@ApiMethod(name = "saveProfile")
public Profile saveProfile(@Named("firstName") String firstName,
@Named("lastName") String lastName,
@Named("birthday") String birthday,
@Named("userId") String userId) {
Profile profile = ofy().load().key(Key.create(Profile.class, userId)).now();
if (profile == null) {
profile = new Profile(userId, firstName, lastName, birthday);
} else {
profile.updateProfile(firstName, lastName, birthday);
}
ofy().save().entity(profile).now();
return profile;
}
此方法return是从后端端点模块到调用它的位置的配置文件实体,在本例中,是位于应用程序模块(客户端)中的异步任务。
我可以使用配置文件对象并添加对后端模块的依赖,以便我在我的客户端中使用这个对象,还是我应该 return 在后端?
在方法saveProfile
中有很多参数,我想直接传入一个包含所有这些字段的对象,但是我如何使用端点来做到这一点,因为当我这样做时我需要添加一个依赖于后端模块以便识别 class?那么profile实体应该存储在后端,profile表单(saveProfile参数)应该放在后端还是客户端?
Entity/model classes同时存储在客户端(前端)和服务器(后端)上。服务器和客户端之间的数据发送是通过serialization完成的。一种序列化形式是 JSON.
JSON is a format that encodes objects in a string. Serialization means
to convert an object into that string, and deserialization is its
inverse operation.
为您的后端服务找到一个 JSON 库,该库易于您使用,并将序列化您的模型 class 以发送到 android 设备。一旦您在 Android 设备上收到 JSON 数据,您就可以再次使用任何使 JSON 工作变得容易的东西。我推荐使用 GSON 库,到目前为止它对我们来说效果很好。
您可以直接传递 Profile 对象,而不是将您的参数一个一个传递给 saveProfile 方法(作为通过 @Named 注释标识的非实体参数)。您的方法如下所示:
@ApiMethod(name = "saveProfile")
public Profile saveProfile(Profile sentProfile) {
然后,在您的端点中,您只需通过相应的 getter 获取 sentProfile 的属性即可。
serialization/deserialization 将由端点 "framework" 自动完成。
查看 Romin Irani 的教程,了解有关如何将实体从 Android 应用程序发送到端点后端以及反之亦然的更多详细信息:https://rominirani.com/google-cloud-endpoints-tutorial-part-3-f8a632fa18b1#.ydrtdy17k
对于另一种方法,您可以看看这个 Udacity MOOC:https://www.udacity.com/course/progress#!/c-ud859
他们使用名为 XXXXForm 的额外实体将数据从前端传送到后端。
在我的 android 应用程序的端点 class 中,我 return 一个 Profile 对象,它是一个实体。我想将数据传回前端。
这样做的最佳方法是什么,模型应该放在哪里 存储在前端还是后端,或者两者都存储?
编辑:
例如,在异步任务中,我执行以下操作:
try {
return mApi.saveProfile(firstName, lastName, birthday, userId).execute().getFirstName();
} catch (IOException e) {
return e.getMessage();
}
这会调用端点后端方法 saveProfile
,该方法位于 android 应用程序的单独模块中。该方法执行此操作:
@ApiMethod(name = "saveProfile")
public Profile saveProfile(@Named("firstName") String firstName,
@Named("lastName") String lastName,
@Named("birthday") String birthday,
@Named("userId") String userId) {
Profile profile = ofy().load().key(Key.create(Profile.class, userId)).now();
if (profile == null) {
profile = new Profile(userId, firstName, lastName, birthday);
} else {
profile.updateProfile(firstName, lastName, birthday);
}
ofy().save().entity(profile).now();
return profile;
}
此方法return是从后端端点模块到调用它的位置的配置文件实体,在本例中,是位于应用程序模块(客户端)中的异步任务。
我可以使用配置文件对象并添加对后端模块的依赖,以便我在我的客户端中使用这个对象,还是我应该 return 在后端?
在方法saveProfile
中有很多参数,我想直接传入一个包含所有这些字段的对象,但是我如何使用端点来做到这一点,因为当我这样做时我需要添加一个依赖于后端模块以便识别 class?那么profile实体应该存储在后端,profile表单(saveProfile参数)应该放在后端还是客户端?
Entity/model classes同时存储在客户端(前端)和服务器(后端)上。服务器和客户端之间的数据发送是通过serialization完成的。一种序列化形式是 JSON.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation.
为您的后端服务找到一个 JSON 库,该库易于您使用,并将序列化您的模型 class 以发送到 android 设备。一旦您在 Android 设备上收到 JSON 数据,您就可以再次使用任何使 JSON 工作变得容易的东西。我推荐使用 GSON 库,到目前为止它对我们来说效果很好。
您可以直接传递 Profile 对象,而不是将您的参数一个一个传递给 saveProfile 方法(作为通过 @Named 注释标识的非实体参数)。您的方法如下所示:
@ApiMethod(name = "saveProfile")
public Profile saveProfile(Profile sentProfile) {
然后,在您的端点中,您只需通过相应的 getter 获取 sentProfile 的属性即可。
serialization/deserialization 将由端点 "framework" 自动完成。
查看 Romin Irani 的教程,了解有关如何将实体从 Android 应用程序发送到端点后端以及反之亦然的更多详细信息:https://rominirani.com/google-cloud-endpoints-tutorial-part-3-f8a632fa18b1#.ydrtdy17k
对于另一种方法,您可以看看这个 Udacity MOOC:https://www.udacity.com/course/progress#!/c-ud859
他们使用名为 XXXXForm 的额外实体将数据从前端传送到后端。