java - 无类 已注册云端点

标签 java android google-app-engine google-cloud-endpoints objectify

我正在使用 Google Cloud 端点和 objectify 来存储应用程序的数据。如果我向创建的 api 发出请求,我将得到:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ {
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ "code": 400,
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ "errors": [
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ {
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ "domain": "global",
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ "message": "java.lang.IllegalArgumentException: No class 'com.nicky.mybackend.Post' was registered",
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ "reason": "badRequest"
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ }
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ ],
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ "message": "java.lang.IllegalArgumentException: No class 'com.nicky.mybackend.Post' was registered"
11-23 09:39:03.711  27884-28100/com.nicky W/System.err﹕ }

我知道我在 ObjectifyService 类中注册了 Post.class,并且我能够在 Android 客户端中创建 Post 对象。这是 objectifyService 类:

public class OfyService {

    static {
        ObjectifyService.register(RegistrationRecord.class);
        ObjectifyService.register(Post.class);
    }

    public static Objectify ofy() {
        return ObjectifyService.ofy();
    }

    public static ObjectifyFactory factory() {
        return ObjectifyService.factory();
    }
}

这是我在 Android 客户端中调用的端点方法:

@ApiMethod(
            name = "insert",
            path = "post",
            httpMethod = ApiMethod.HttpMethod.POST)
    public Post insert(Post post) {
        ofy().save().entity(post).now();
        logger.info("Created Post with ID: " + post.getId());

        return ofy().load().entity(post).now();
    }

我正在从 asynctask 调用后端,如下所示:

        Post result;
        PostEndpoint service = AppConstants.getApiHandle(credential);
        Post temp = new Post();
        c.setTime(new Date());
        temp.setPostTime(c.getTimeInMillis());

        result = service.insert(temp).execute();

我没有保护服务器端的安全,但我正在使用 GoogleAccountCredential 发出请求,该凭据具有手机上 Android 帐户的用户名。我不知道我需要在任何其他地方注册我的端点模型类。感谢任何帮助,谢谢

最佳答案

我不想分配,给出正确的答案,但此评论有正确的答案:

Blockquote Any chance you have the wrong static import for ofy()? It should be OfyService.ofy() and not ObjectifyService.ofy()

因此,当您创建端点时,Android Studio 会自动导入库

import com.googlecode.objectify.ObjectifyService;

当您需要导入 Ofy 类时。

所以,就我而言,我导入了它而不是另一个,甚至我将 Ofy 类的名称设置为:

 OfyService.ofy().save().entity

 OfyService.ofy().delete().entity

关于java - 无类 已注册云端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27092474/

相关文章:

android - 在Android Studio中打开另一个项目后,添加的配置消失

android - onFailure 未被调用 - onFailure(...) 未被覆盖,但已收到回调

python - 带有 Google 身份验证的 Appengine ACL

google-app-engine - GWT 上的 Google App Engine 任务队列

java - 将excel代码写入数据库

java - Hibernate 注解 - 如何连接具有多对多关系的三个表

java - 向下转型的对象指向哪里?

java - 用于将缩略图动画化到全尺寸屏幕的开源库,类似于 Facebook

Spring + Hibernate = "manual"事务操作指南

java - 构建 Codename One 应用程序时的跨平台 Google+ 登录 API key