Cloud Endpoints 在 Android/iOS 个客户端库中生成实体密钥
Cloud Endpoints generate entity key inside of Android/iOS client libs
有没有办法在 Android Java 中创建实体密钥?
例如,在 Cloud Endpoints Java 模块代码中,您可以这样做:
Key<User> userKey= Key.create(User.class, userId);
或与祖先:
Key<Post> postKey= Key.create(userKey, Post.class, postId);
如何在 Android 生成的客户端库中执行上述操作?我想看看我是否可以在 Android 中创建一个密钥并将其传递给 API 方法(可能作为 websafeKey userKey.getString()
)。
红利:如何使用 objective-C Cloud Endpoints 客户端库做到这一点?
我怀疑您想要在 Android 应用程序中使用数据存储或对象化代码。那根本不属于那个地方。所以要走的路是在方法 convertToPb()
.
中查看 KeyFactory. In the method keyToString()
we can see that most of the magic happens in the class KeyTranslator 的来源
这是 convertToPb 的代码:
public static Reference convertToPb(Key key) {
Reference reference = new Reference();
reference.setApp(key.getAppId());
String nameSpace = key.getNamespace();
if (!nameSpace.isEmpty()) {
reference.setNameSpace(nameSpace);
}
Path path = reference.getMutablePath();
while (key != null) {
Element pathElement = new Element();
pathElement.setType(key.getKind());
if (key.getName() != null) {
pathElement.setName(key.getName());
} else if (key.getId() != Key.NOT_ASSIGNED) {
pathElement.setId(key.getId());
}
path.addElement(pathElement);
key = key.getParent();
}
Collections.reverse(path.mutableElements());
return reference;
}
这是 keyToString()
的代码
public static String keyToString(Key key) {
if (!key.isComplete()) {
throw new IllegalArgumentException("Key is incomplete.");
} else {
Reference reference = KeyTranslator.convertToPb(key);
return base64Url().omitPadding().encode(reference.toByteArray());
}
}
现在你要做的是用 "normal" 参数(类型,name/key,父类型,父 name/key)替换 convertToPb 中的关键内容,从而重写在没有实际 Key
对象的情况下创建 websafeKey 的方法。
不过,如果您的应用引擎 API 只是接受 ID,并且您会在应用引擎方面重新创建密钥,那会容易得多。我的 API 的结构通常类似于
/user/<userId>/post/<postId>
如果我假设一个看起来像这样的实体
@Entity public class Post {
@Parent Ref<User> user
@Id id; }
关于奖励:什么是 Objectify Cloud Endpoint?我知道 Cloud Endpoints 和 Objectify,但我还没有听说过结合这两者的产品。
有没有办法在 Android Java 中创建实体密钥?
例如,在 Cloud Endpoints Java 模块代码中,您可以这样做:
Key<User> userKey= Key.create(User.class, userId);
或与祖先:
Key<Post> postKey= Key.create(userKey, Post.class, postId);
如何在 Android 生成的客户端库中执行上述操作?我想看看我是否可以在 Android 中创建一个密钥并将其传递给 API 方法(可能作为 websafeKey userKey.getString()
)。
红利:如何使用 objective-C Cloud Endpoints 客户端库做到这一点?
我怀疑您想要在 Android 应用程序中使用数据存储或对象化代码。那根本不属于那个地方。所以要走的路是在方法 convertToPb()
.
keyToString()
we can see that most of the magic happens in the class KeyTranslator 的来源
这是 convertToPb 的代码:
public static Reference convertToPb(Key key) {
Reference reference = new Reference();
reference.setApp(key.getAppId());
String nameSpace = key.getNamespace();
if (!nameSpace.isEmpty()) {
reference.setNameSpace(nameSpace);
}
Path path = reference.getMutablePath();
while (key != null) {
Element pathElement = new Element();
pathElement.setType(key.getKind());
if (key.getName() != null) {
pathElement.setName(key.getName());
} else if (key.getId() != Key.NOT_ASSIGNED) {
pathElement.setId(key.getId());
}
path.addElement(pathElement);
key = key.getParent();
}
Collections.reverse(path.mutableElements());
return reference;
}
这是 keyToString()
的代码public static String keyToString(Key key) {
if (!key.isComplete()) {
throw new IllegalArgumentException("Key is incomplete.");
} else {
Reference reference = KeyTranslator.convertToPb(key);
return base64Url().omitPadding().encode(reference.toByteArray());
}
}
现在你要做的是用 "normal" 参数(类型,name/key,父类型,父 name/key)替换 convertToPb 中的关键内容,从而重写在没有实际 Key
对象的情况下创建 websafeKey 的方法。
不过,如果您的应用引擎 API 只是接受 ID,并且您会在应用引擎方面重新创建密钥,那会容易得多。我的 API 的结构通常类似于
/user/<userId>/post/<postId>
如果我假设一个看起来像这样的实体
@Entity public class Post {
@Parent Ref<User> user
@Id id; }
关于奖励:什么是 Objectify Cloud Endpoint?我知道 Cloud Endpoints 和 Objectify,但我还没有听说过结合这两者的产品。