客户端的Google App Engine类

时间:2022-11-15 18:19:22

I am developing an Android app using GAE on Eclipse.
On one of the EndPoint classes I have a method which returns a "Bla"-type object:

我正在Eclipse上使用GAE开发一个Android应用程序。在其中一个EndPoint类中,我有一个返回“Bla”类型对象的方法:

public Bla foo()
    {
        return new Bla();
    }

This "Bla" object holds a "Bla2"-type object:

这个“Bla”对象包含一个“Bla2”类型的对象:

public class Bla {
    private Bla2 bla = new Bla2();
    public Bla2 getBla() {
        return bla;
    }
    public void setBla(Bla2 bla) {
        this.bla = bla;
    }
}

Now, my problem is I cant access the "Bla2" class from the client side. (Even the method "getBla()" doesn't exist)

现在,我的问题是我无法从客户端访问“Bla2”类。 (甚至方法“getBla()”也不存在)

I managed to trick it by creating a second method on the EndPoint class which return a "Bla2" object:

我设法通过在EndPoint类上创建第二个方法来欺骗它,该方法返回一个“Bla2”对象:

public Bla2 foo2()
    {
        return new Bla2();
    }

Now I can use the "Bla2" class on the client side, but the "Bla.getBla()" method still doesn't exit. Is there a right way to do it?

现在我可以在客户端使用“Bla2”类,但“Bla.getBla()”方法仍然不会退出。有没有正确的方法呢?

2 个解决方案

#1


1  

This isn't the 'right' way, but keep in mind that just because you are using endpoints, you don't have to stick to the endpoints way of doing things for all of your entities.

这不是'正确'的方式,但请记住,仅仅因为您使用端点,您不必坚持端点为所有实体做事的方式。

Like you, I'm using GAE/J and cloud endpoints and have an ANdroid client. It's great running Java on both the client and the server because I can share code between all my projects.

和你一样,我正在使用GAE / J和云端点,并拥有一个ANdroid客户端。它在客户端和服务器上运行Java都很棒,因为我可以在所有项目之间共享代码。

Some of my entities are communicated and shared the normal 'endpoints way', as you are doing. But for other entities I still use JSON, but just stick them in a string, send them through a generic endpoint, and deserialize them on the other side, which is easy because the entity class is in the shared code.

正如您所做的那样,我的一些实体与正常的“端点方式”进行通信和共享。但是对于其他实体,我仍然使用JSON,但只是将它们粘贴在字符串中,通过通用端点发送它们,并在另一端反序列化它们,这很容易,因为实体类在共享代码中。

This allows me to send 50 different entity types through a single endpoint, and it makes it easy for me to customize the JSON serializing/deserializing for those entities.

这允许我通过单个端点发送50种不同的实体类型,这使我可以轻松地为这些实体自定义JSON序列化/反序列化。

Of course, this solution gets you in trouble if decide to add an iOS or Web (unless you use GWT) client, but maybe that isn't important to you.

当然,如果决定添加iOS或Web(除非您使用GWT)客户端,此解决方案会让您遇到麻烦,但这可能对您来说并不重要。

(edit - added some impl. detail)

(编辑 - 添加一些impl。细节)

Serializing your java objects (or entities) to/from JSON is very easy, but the details depend on the JSON library you use. Endpoints can use either Jackson or GSON on the client. But for my own JSON'ing I used json.org which is built-into Android and was easy to download and add to my GAE project.

将Java对象(或实体)序列化到JSON或从JSON序列化非常简单,但细节取决于您使用的JSON库。端点可以在客户端上使用Jackson或GSON。但是对于我自己的JSON,我使用内置于Android的json.org,并且易于下载并添加到我的GAE项目中。

Here's a tutorial that someone just published:
http://www.survivingwithandroid.com/2013/10/android-json-tutorial-create-and-parse.html

这是一个刚刚发布的教程:http://www.survivingwithandroid.com/2013/10/android-json-tutorial-create-and-parse.html

Then I added an endpoint like this:

然后我添加了一个这样的端点:

@ApiMethod(name = "sendData")
public void sendData( @Named("clientId") String clientId, String jsonObject )

(or something with a class that includes a List of String's so you can send multiple entities in one request.)

(或者包含一个包含String列表的类的东西,这样你就可以在一个请求中发送多个实体。)

And put an element into your JSON which tells the server which entity the JSON should be de serialized into.

并在您的JSON中放入一个元素,告诉服务器JSON应该被序列化到哪个实体。

#2


0  

Try using @ApiResourceProperty on the field.

尝试在该字段上使用@ApiResourceProperty。

#1


1  

This isn't the 'right' way, but keep in mind that just because you are using endpoints, you don't have to stick to the endpoints way of doing things for all of your entities.

这不是'正确'的方式,但请记住,仅仅因为您使用端点,您不必坚持端点为所有实体做事的方式。

Like you, I'm using GAE/J and cloud endpoints and have an ANdroid client. It's great running Java on both the client and the server because I can share code between all my projects.

和你一样,我正在使用GAE / J和云端点,并拥有一个ANdroid客户端。它在客户端和服务器上运行Java都很棒,因为我可以在所有项目之间共享代码。

Some of my entities are communicated and shared the normal 'endpoints way', as you are doing. But for other entities I still use JSON, but just stick them in a string, send them through a generic endpoint, and deserialize them on the other side, which is easy because the entity class is in the shared code.

正如您所做的那样,我的一些实体与正常的“端点方式”进行通信和共享。但是对于其他实体,我仍然使用JSON,但只是将它们粘贴在字符串中,通过通用端点发送它们,并在另一端反序列化它们,这很容易,因为实体类在共享代码中。

This allows me to send 50 different entity types through a single endpoint, and it makes it easy for me to customize the JSON serializing/deserializing for those entities.

这允许我通过单个端点发送50种不同的实体类型,这使我可以轻松地为这些实体自定义JSON序列化/反序列化。

Of course, this solution gets you in trouble if decide to add an iOS or Web (unless you use GWT) client, but maybe that isn't important to you.

当然,如果决定添加iOS或Web(除非您使用GWT)客户端,此解决方案会让您遇到麻烦,但这可能对您来说并不重要。

(edit - added some impl. detail)

(编辑 - 添加一些impl。细节)

Serializing your java objects (or entities) to/from JSON is very easy, but the details depend on the JSON library you use. Endpoints can use either Jackson or GSON on the client. But for my own JSON'ing I used json.org which is built-into Android and was easy to download and add to my GAE project.

将Java对象(或实体)序列化到JSON或从JSON序列化非常简单,但细节取决于您使用的JSON库。端点可以在客户端上使用Jackson或GSON。但是对于我自己的JSON,我使用内置于Android的json.org,并且易于下载并添加到我的GAE项目中。

Here's a tutorial that someone just published:
http://www.survivingwithandroid.com/2013/10/android-json-tutorial-create-and-parse.html

这是一个刚刚发布的教程:http://www.survivingwithandroid.com/2013/10/android-json-tutorial-create-and-parse.html

Then I added an endpoint like this:

然后我添加了一个这样的端点:

@ApiMethod(name = "sendData")
public void sendData( @Named("clientId") String clientId, String jsonObject )

(or something with a class that includes a List of String's so you can send multiple entities in one request.)

(或者包含一个包含String列表的类的东西,这样你就可以在一个请求中发送多个实体。)

And put an element into your JSON which tells the server which entity the JSON should be de serialized into.

并在您的JSON中放入一个元素,告诉服务器JSON应该被序列化到哪个实体。

#2


0  

Try using @ApiResourceProperty on the field.

尝试在该字段上使用@ApiResourceProperty。