无效的 Key PB:没有元素

Invalid Key PB: no elements

对于我的 android 应用程序,我正在为后端编写端点代码。

在我的 appengine 数据存储方法中,当我尝试使用密钥访问我的实体时,我得到

java.lang.IllegalArgumentException: Invalid Key PB: no elements

以下是我试过的代码片段

Key key = KeyFactory.stringToKey(keyId);

key = KeyFactory.stringToKey(URLEncoder.encode(keyId, "utf-8"));

keyId 是字符串类型。

如何解决?

试试这个代码:

key = KeyFactory.stringToKey(""+keyId);

我自己解决了这个问题!
我没有使用 stringToKey,而是使用了 createKey 作为我想要的类型。
将密钥作为 String

传递
 Key key = KeyFactory.createKey(<KIND>,keyId);