我正在使用 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/