How does an Android and App Engine Actually commun

2019-06-25 06:45发布

问题:

Ok, this may seem as a dumb question, but I am really new when it comes to Cloud Computing/Google App Engine etc. In order to get more familiarized with it, I started to work with some tutorials from developers.google.com, basically following the tutorials and then trying to perform small changes to the provided pieces of code, in order to make sure that I actually understood the way it works, not just copy/paste and take everything for granted.

The problem is that I got a little bit stuck at the following aspect: the way Android and App Engine actually communicate. I am currently doing this tutorial( https://developers.google.com/eclipse/docs/endpoints-addentities). The problem is the following piece of code (client-side, on Android):

      public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
    protected Long doInBackground(Context... contexts) {

           Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
          AndroidHttp.newCompatibleTransport(),
          new JacksonFactory(),
          new HttpRequestInitializer() {
          public void initialize(HttpRequest httpRequest) { }
          });
  Noteendpoint endpoint = CloudEndpointUtils.updateBuilder(
  endpointBuilder).build();
  try {
      Note note = new Note().setDescription("Note Description");
      String noteID = new Date().toString();
      note.setId(noteID);

      note.setEmailAddress("E-Mail Address");          
      Note result = endpoint.insertNote(note).execute();
  } catch (IOException e) {
    e.printStackTrace();
  }
      return (long) 0;
    }
}

As far as my understanding helps me, at this moment, in terms of Cloud Computing, I inferred that the communication between Android and the Cloud is performed via endpoint object, where endpoint is:

        Noteendpoint endpoint = CloudEndpointUtils.updateBuilder (endpointBuilder).build();

Also, the updateBuilder() method looks like this:

        public static <B extends AbstractGoogleClient.Builder> B updateBuilder(
  B builder) {
if (LOCAL_ANDROID_RUN) {
  builder.setRootUrl(LOCAL_APP_ENGINE_SERVER_URL_FOR_ANDROID
      + "/_ah/api/");
}

// only enable GZip when connecting to remote server
final boolean enableGZip = builder.getRootUrl().startsWith("https:");

builder.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
  public void initialize(AbstractGoogleClientRequest<?> request)
      throws IOException {
    if (!enableGZip) {
      request.setDisableGZipContent(true);
    }
  }
});

return builder;

}

I do understand that the insertion in the Data Storage is performed via insertNote() - which, basically, performs basic a standard insert method.

My problem is that I cannot really understand where, in the cloud, the information that I've sent from my Android device is caught. To be more specific, I am sending an object, and I cannot really see where that object is received in the Cloud. Probably at this kind of basic application, is not that relevant, but I want to develop an application with the following structure: I am sending data from my Android device using REST. I am developing my server-side code )(which will be in the Cloud). In my server-side code will receive the data I am sending from Android -> process that data -> add something in the database (database stored in the cloud) (this is the basic principle explained in VERY primitive terms). That's why I really want to understand the way this works and so far, I really cannot see where my data is received on the server side. I have assumed that there is probably some automatic mechanism behind this ? If so, I am really interested, if you could indicate me, how can I do that programatically.

Also, I would like to mention that this code works really good, so there are no errors in it, I just have problems in understanding all the details related to it.

Thank you.

LATER EDIT: My database will be App Engine Datastore. The main problem is that I cannot really understand the way the communication between my Android Application and the Google App Engine Application (where I will be making all the necessary computations with the data I receive from Android) is made. I could really use a more "obvious"/explainatory (for dummies) piece of code where I actually see that the object I send from Android is received in the Google App Engine Application. Of course, I saw the result, using Datastore Viewer, which shows that the data is inserted in the database. What interests me is how I can actually just send the data in my Google App Engine Application, receive it there and perform some operations on it, and ONLY after I will add it in the database.

回答1:

The updateBuilder() method is not on the server side. It's part of the android code. CloudEndpointUtils is part of android. It's a class you create to handle the boilerplate code for you so you don't have to type it each time you need to access the server. You see the code

Noteendpoint.Builder endpointBuilder = new Noteendpoint.Builder(
      AndroidHttp.newCompatibleTransport(),
      new JacksonFactory(),
      new HttpRequestInitializer() {
      public void initialize(HttpRequest httpRequest) { }
      });

It's assuming your api is called Noteendpoint and that you are "building" an object to access it. You could have called endpointBuilder.build() to start querying your api. If you look in the CloudEndpointUtils.updateBuilder method closely, you will see that what it is doing is redirect your calls to your localhost as opposed to your deployed code on appengine.

Let me know if you need clarification.

TO ANSWER YOUR EDIT:

Forget for a moment that this is a Google-endpoint application. You are basically designing a system that takes inputs, work on the inputs, and then save the result to a database.

For now let's pretend it does not matter whether the input is coming from a terminal or a file or a call to an api endpoint. What you absolutely must do is create a layer that will manipulate the data and then pass that data to your persistence layer (ie database). So you will need (still ignoring all things Google/appengine):

  • JPA entity (POJO with getters and setters and JPA annotations only)

  • Data access layer: This is a class with methods to perform queries on your JPA POJO (using EntityManagerFactory).

  • Business layer: This is a class where you manipulate the data you receive, then pass the result to the data access layer.

Hence you have Business logic layer => Data access layer => JPA POJO. So create all that not worring whether it's going to run on your local glassfish or wherever.

AFTER you are done, add endpoints annotations to your Business layer methods. Those annotations basically means that after you generate your android endpoint library, your android will be able to call your Business layer methods as if they were right inside your android application codes.

You get it? It will be as if your android and your server were one. Are you using the Google Eclipse plugin for this project?



回答2:

I really don't understand your question. The "cloud" is represented by a Google App Engine application. That lives on Google's servers, and is served under the URL that you assign when you create the project. The database will presumably be either the App Engine datastore, or the Google Cloud SQL service which uses a version of MySQL.