Google App Engine Java和Android入门

5

我一直在努力运行下面链接中的示例:

https://developers.google.com/eclipse/docs/getting_started

我遇到的第一个问题是,在Android SDK中没有安装“Google Cloud Messaging for Android Library”(显然我知道)。

但现在我在Android项目中的两个文件中遇到了自动生成代码的问题: GCMIntentService.java和RegisterActivity.java

错误如下:

  • The method getDeviceInfo(String) is undefined for the type Deviceinfoendpoint GCMIntentService.java
  • The method listMessages() is undefined for the type MessageEndpoint RegisterActivity.java
  • The method insertDeviceInfo(DeviceInfo) is undefined for the type Deviceinfoendpoint GCMIntentService.java
  • The method removeDeviceInfo(String) is undefined for the type Deviceinfoendpoint GCMIntentService.java

我使用的是Ubuntu上的Java SDK v1.7.0_15,但我也尝试过Windows 7上的Java SDK v1.6,并且出现了相同的问题。最新的Android平台版本为4.2.2,Google App Engine版本为1.7.7。Eclipse是Juno Service Release 2。

问题看起来像是他们做了一些错误的类型转换,因为在Deviceinfoendpoint内部类DeviceInfoEndpoint中有一个名为getDeviceInfo的方法(大小写不同)。

我可以尝试修复它,但只是想知道我是否在设置方面有什么问题导致出现这种情况?

任何帮助都将不胜感激。


你的问题出现在应用程序项目还是应用引擎项目上?你能否发布一些关于这些错误的堆栈跟踪信息? - Deividi Cavarzan
问题出在应用程序项目上。由于尚未编译,因此没有堆栈跟踪。 - reubenb87
昨晚我也在苦苦挣扎着解决完全相同的问题。我还尝试了两台不同软件设置的机器。 - Heigo
2个回答

3
在你的GCMIntentService.java类中,在具有错误的行中的端点对象后添加.deviceInfoEndpoint(),如下所示:
DeviceInfo existingInfo = endpoint.getDeviceInfo(registration)
DeviceInfo existingInfo = endpoint.deviceInfoEndpoint().getDeviceInfo(registration)

在 RegisterActivity.java 中更改该行:
messageEndpoint.listMessages().setLimit(5).execute();

to

messageEndpoint.messageEndpoint().listMessages().setLimit(5).execute();

非常感谢您!他们的自动生成代码肯定有错误。 - reubenb87
没问题。但是你真的让示例项目工作了吗?我让 Android 应用程序工作了,而且它注册得很好,但后端没有看到任何设备。 - Heigo

2

请确保您正在使用与JAR文件相同版本的GCM API。已经有相当多的修订。

我正在使用大小为19718字节的gcm-server.jar,以下是我成功发送GCM消息到设备的代码:

public void sendMessage() {
    String notificationToken = mobileDevice.getPushNotificationCode();
    String deviceType = mobileDevice.getDeviceType();

    Sender sender = new Sender(BROWSER_API_KEY);
    Message message = new Message.Builder().addData("message", "blah blah").build();
    String device = "<the key for the device you are sending to goes here>";

    try {
        System.out.println("Sending message...");
        Result result = sender.send(message, device, 5);
        System.out.println("Done sending message");
        if (result.getMessageId() != null) {
            System.out.println("Got message ID: " + result.getMessageId());
            System.out.println("Got error code name: " + result.getErrorCodeName());
            System.out.println("result: " + result);
            String canonicalRegId = result.getCanonicalRegistrationId();
            if (canonicalRegId != null) {
                // Database has more than one record for this device.
                // Replace all of this device's records with this new id
                System.out.println("Got new canonical reg id: " + canonicalRegId);
            }
        } else {
            String error = result.getErrorCodeName();
            if (error.equals(com.google.android.gcm.server.Constants.ERROR_NOT_REGISTERED)) {
                // application has been removed from device - unregister from database
                System.out.println("Got error: " + error);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接