Reputation: 2136
In my endpoint class in my android app, I return a Profile object which is an entity. I want to pass the data back to the frontend.
What is the best way of doing this and where should the model be stored, in the frontend or in the backend, or perhaps in both?
EDIT:
For example in an async task I do the following:
try {
return mApi.saveProfile(firstName, lastName, birthday, userId).execute().getFirstName();
} catch (IOException e) {
return e.getMessage();
}
This calls the endpoints backend method saveProfile
which is located in a separate module in the android app. The method does this:
@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;
}
This method returns a profile entity from the backend endpoints module to where it was called from, in this case, an async task located in the app module (client).
Is it ok for me to use the profile object and add a dependency on the backend module, in order for me to use this object in my client or should I be returning a different object in the backend?
In the method saveProfile
there are many parameters, I would like to pass in an object which contains all these fields directly but how do I do this using endpoints as when I do I would need to add a dependency on the backend module so that the class is recognised? So should the profile entity be stored in the backend and should the profile form (saveProfile parameter) be located in the backend or client?
Upvotes: 0
Views: 516
Reputation: 83181
Instead of passing your parameters one by one to the saveProfile method (as non-entity parameters identified via the @Named annotation) you can directly pass a Profile object. Your method would look like:
@ApiMethod(name = "saveProfile")
public Profile saveProfile(Profile sentProfile) {
Then, in your endpoint, you just get the properties of sentProfile through corresponding getters.
The serialization/deserialization will be done automatically by the endpoints "framework".
Have a look at this tutorial from Romin Irani for more details on how to send an entity from an Android App to the endpoints back-end and vice versa: https://rominirani.com/google-cloud-endpoints-tutorial-part-3-f8a632fa18b1#.ydrtdy17k
For another way of doing that, you could have a look a this Udacity MOOC: https://www.udacity.com/course/progress#!/c-ud859
They use extra entities called XXXXForm to convey data from the front-end to the back-end.
Upvotes: 1
Reputation: 401
Entity/model classes are stored on both the client(front end) and the server(backend). Sending data between the server and client is done through serialization. One form of serialization is 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.
Find a JSON library for your backend services that is easy for you to work with and will serialize your model class to be sent to the android device. Once you receive the JSON data on and Android device you can again use anything that make working with JSON easy. I recommend using the GSON library, it has worked well for us so far.
Upvotes: 0