0

我实际上是在使用 GAE 构建聊天室,问题是我将 clientid 和令牌存储在 chatrromservlet 中,并使用数据存储密钥在 sayservlet 中访问它们,但是当我尝试使用密钥从 sayservlet 访问数据存储时,我得到了一个异常找不到带有密钥的实体

我的聊天室servlet

    String clientid = request.getParameter("clientid");
    ChannelService channelService = ChannelServiceFactory.getChannelService();

    String token = channelService.createChannel(clientid);
    channelService.sendMessage(new ChannelMessage(token, "Hello World"));
    System.out.println("token is ="+token);

    JSONObject job=new JSONObject();
    job.put("token",token);

    Key userkey=KeyFactory.createKey("users", "user1");
    System.out.println("user key in chatroom "+ userkey);
    Entity ent=new Entity("user");
    ent.setProperty("clientid", clientid);
    ent.setProperty("token", token);


    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    datastore.put(ent);
    System.out.println(ent);  

并且说servlet是

            String message = request.getParameter("message");
            String clientid = request.getParameter("clientid");
            System.out.println(message);

            Key userkey=KeyFactory.createKey("users", "user1");
            Entity entity;
            DatastoreService datastore = DatastoreServiceFactory
                    .getDatastoreService();
            entity = datastore.get(userkey);
            String token = (String) entity.getProperty("token");
            System.out.println("token frm db ="+token);

            ChannelService channelService = ChannelServiceFactory.getChannelService();
            channelService.sendMessage(new ChannelMessage(token, "Hello World"));
            System.out.println("message is ="+message);
            System.out.println("cid is ="+clientid);

如果访问时有任何错误,请帮助我。

4

1 回答 1

0

看起来在您的 chatroomservlet 中您创建了一个实体,但您不使用生成的密钥。因此,当您在 say servlet 中通过该键查找时,数据存储将无法找到该实体。

您的 chatroomservlet 应该使用以下命令创建实体userkey

    密钥 userkey=KeyFactory.createKey("users", "user1");
    System.out.println("聊天室用户密​​钥"+ userkey);
    实体 ent=新实体(用户密钥);
于 2013-09-27T19:05:25.047 回答