How can I send information to my client (Google App Engine, Cloud Endpoints)?

I have an Android app that is stored in the cloud using the Google App Engine. I am using Cloud Endpoints. My problem is that I canโ€™t send data from the server to my client (Android device), or rather, I donโ€™t know yet how to do it.

So far, I have managed to insert data into the data warehouse by creating an endpoint and calling the method that is responsible for adding a record to the database (which is located on the server side in myProject - AppEngine) using the following code (on the client): \

Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder( AndroidHttp.newCompatibleTransport(), new JacksonFactory(), new HttpRequestInitializer() { public void initialize(HttpRequest httpRequest) { } }); Noteendpoint endpoint = CloudEndpointUtils.updateBuilder( endpointBuilder).build(); try { // Construct the note. Note note = new Note().setDescription("Note DescriptionRoxana"); String noteID = new Date().toString(); note.setId(noteID); note.setEmailAddress("E-Mail AddressRoxana"); // Insert the Note, by calling a method that on the server side - insertNote(); Note result = endpoint.insertNote(note).execute(); } catch (IOException e) { e.printStackTrace(); } 

But I do not see a way to retrieve data from the data warehouse and display it on the server side. I tried to do the same thing, create an endpoint that would call a method that retrieves all the records in the database (the method that is on the server), but my application crashes.

The code for the method that retrieves data from the data warehouse is as follows:

  public CollectionResponse<Note> listNote( @Nullable @Named("cursor") String cursorString, @Nullable @Named("limit") Integer limit) { EntityManager mgr = null; Cursor cursor = null; List<Note> execute = null; try { mgr = getEntityManager(); Query query = mgr.createQuery("select from Note as Note"); if (cursorString != null && cursorString != "") { cursor = Cursor.fromWebSafeString(cursorString); query.setHint(JPACursorHelper.CURSOR_HINT, cursor); } if (limit != null) { query.setFirstResult(0); query.setMaxResults(limit); } execute = (List<Note>) query.getResultList(); cursor = JPACursorHelper.getCursor(execute); if (cursor != null) cursorString = cursor.toWebSafeString(); // Tight loop for fetching all entities from datastore and accomodate // for lazy fetch. for (Note obj : execute) ; } finally { mgr.close(); } return CollectionResponse.<Note> builder().setItems(execute) .setNextPageToken(cursorString).build(); } 

You see, the return type is the response of the collection. You have access to this type of data after performing the following import:

  import com.google.api.server.spi.response.CollectionResponse; 

I assumed that this is a data type specific to the server side, so I have no idea how I can pass it to a List, ArrayList or any other type of collection that can be used on the client side.

How can I do it? Since adding data was so simple and so straightforward, I assumed that the data would be received in the same way, but apparently I was missing something substantial for this issue.

Thank you in advance!

+4
source share
2 answers

The classes that you use in the backend do not match the classes that you will use in the client. Endpoints will create a set of libraries for you, either on the command line or using tools such as Google Plugin for Eclipse. See Using endpoints in an Android client .

The generated class representing the Note collection in your example will be called NotesCollection . This object will have a getItems method that provides you with a List<Note> that you can repeat in your Android app.

+4
source

Similar to having an endpoint to insert data into the data warehouse model (methods of type POST), you must have an end point to request data from the data warehouse model (methods of type GET). After you define both of these methods, you need to create a discovery document and a client library so that clients know about all these methods and can call them. If you are talking about displaying data in the website itself, you can create a Javascript client using the necessary client library.

0
source

All Articles