没有类注册的云端点

时间:2014-11-23 18:12:03

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

我正在使用带有客观化的google cloud端点来存储应用的数据。如果我向创建的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帐户的用户名。我不知道我需要注册我的端点模型类的任何其他地方。感谢任何帮助

1 个答案:

答案 0 :(得分:2)

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

  

Blockquote fory()有可能出现错误的静态导入吗?它应该是OfyService.ofy()而不是ObjectifyService.ofy()

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

import com.googlecode.objectify.ObjectifyService;

当您需要导入Ofy课程时。

所以,在我的情况下,我已经导入了它而不是另一个,甚至我把我的Ofy类的名称改为:

 OfyService.ofy().save().entity

 OfyService.ofy().delete().entity