如何在Android和App引擎其实沟通? [英] How does an Android and App Engine Actually communicate?

查看:120
本文介绍了如何在Android和App引擎其实沟通?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这似乎是一个愚蠢的问题,但我真的很新的,当涉及到云计算/谷歌应用程序引擎等。为了得到更多的与它熟悉,我开始从developers.google一些教程工作.COM,基本上遵循了教程,然后试图执行的微小变化所提供的code片,以确保我确实了解它的工作方式,而不仅仅是复制/粘贴和理所当然采取一切。

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.

问题是,我停留在以下方面一点点:Android和App Engine的实际沟通的方式。我目前在做这个教程(
https://developers.google.com/eclipse/docs/endpoints-addentities )。问题是下面这段code的(客户端,Android上):

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;
    }
}

据我的理解帮助我,在这一刻,在云计算方面,我推断,Android和云之间的通信是通过端点对象,其中端点进行:

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();

此外,updateBuilder()方法是这样的:

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;

}

我不明白,在数据存储的插入是通过insertNote()执行 - 这,基本上执行基本标准的插入方法

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

我的问题是,我真的不能明白的地方,在云中,我已经从我的Andr​​oid设备发送的信息被捕获。更具体地讲,我送一个对象,我真的不能看到在云中收到的对象。可能是在这种基本的应用程序,是不是相关的,但我想开发具有以下结构的应用程序:我从我的Andr​​oid设备使用REST发送数据。我发展我的服务器端code)(这将是在云中)。在我的服务器端code将收到我从Android的发送数据 - >处理数据 - >中(存储在云数据库)数据库添加的东西(这是基本的原则非常原始的术语解释)。这就是为什么我真的想了解这个工程,到目前为止,我真的不能看到在服务器端收到我的数据的方式。我假设有可能是这背后的一些自动机制?如果是这样,我真的很感兴趣,如果你能指出我,我怎么能做到这一点编程。

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.

另外,我想提一提,这code的作品真的很好,所以有它没有任何错误,我只是在了解所有与之相关的细节问题。

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.

感谢您。

在以后编辑:
我的数据库将成为App Engine数据存储。主要的问题是,我不能真正理解我的方式Android应用程序与谷歌应用程序引擎提出申请(其中我将与我的Andr​​oid从接收到的数据来进行所有必要的计算)之间的通信。我真的可以使用更多的明显的/ explainatory(傻瓜)件code的,我居然看到我从Android的发送对象,在谷歌的App Engine应用程序被接收。当然,我看到的结果,使用数据存储浏览器,这表明数据插入到数据库中。我感兴趣的是如何我其实只是发送数据在我的谷歌应用程序引擎的应用,收到它存在并在其上执行某些操作,只有后,我会在数据库中添加它。

LATER 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.

推荐答案

updateBuilder()方法不是在服务器端。这是Android的code的一部分。 CloudEndpointUtils 的是Android的一部分。这是你创建来处理样板code给你,让你不必每次需要访问服务器时都键入一个类。你看code

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) { }
      });

这是假设你的API叫做 Noteendpoint 和你是建设一个对象来访问它。你可以打电话 endpointBuilder.build()开始查询您的API。如果你在看 CloudEndpointUtils.updateBuilder 方法观察,你会发现它是什么做的是重新定向您转接至本地主机,而不是在AppEngine上部署的code

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.

回答您的编辑:

念念不忘,这是一个谷歌端点的应用程序。你基本上设计一个系统,该系统采用的输入,在输入上工作,然后将结果保存到数据库中。

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.

现在让我们pretend输入是从终端或文件,或者一个API端点的通话来也没关系。你绝对必须做的是创造条件,操纵数据,然后将数据传递到你的持久层(即数据库)层。所以,你需要(仍然忽略所有的事情谷歌/ AppEngine上):

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实体(POJO与getter和setter和JPA注解只)

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

数据访问层:这是与方法,在您的JPA POJO(使用的EntityManagerFactory

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.

因此​​,你有业务逻辑层= GT;数据访问层= GT; JPA POJO 。因此,创造一切,并非是杞人忧天无论是要在你的本地GlassFish或其它地方运行。

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.

在您完成后,添加终端注释你的业务层方法。这些注释基本上意味着,在您产生 Android的端点库,你的Andr​​oid将能够调用你的业务层方法仿佛他们是对你的Andr​​oid应用程序codeS里面。

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.

您怎么做呢?这将是因为,如果你的Andr​​oid和你的服务器是之一。您是否正在使用谷歌的Eclipse插件,这个项目?

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?

这篇关于如何在Android和App引擎其实沟通?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆