BlackBerry - 从持久对象向量写入和读取

时间:2013-02-13 10:09:06

标签: blackberry vector read-write persistent-storage

我有一个BlackBerry应用程序,它以固定的时间间隔向服务器发送消息。使用任何可用的连接方法通过Web服务发送消息; Wifi,BIS,TCP / IP等。

由于邮件是连续发送的,因此我需要一种机制来在互联网不可用时对邮件进行排队,并在互联网可用时发送邮件。出于这个原因,我希望首先在持久存储中保存任何传出消息,然后读取持久存储并循环遍历它以发送所有未决消息。任何新消息都应保存在持久存储中的最后一个位置。

点击“发送”时,我正在调用以下两种方法:

    public static void saveMessage(String msg){
        Hashtable hashtable=new Hashtable();
        persistentObject = PersistentStore.getPersistentObject(KEY);
        hashtable.put("MessageToSend", msg);
        persistentObject.commit();
    }

    public static void sendMessage(String msg){
        Hashtable hashtable=new Hashtable();
        persistentObject = PersistentStore.getPersistentObject(KEY);
        Vector msgVector = (Vector)persistentObject.getContents();
        Enumeration eMsgs=msgVector.elements();;
            /*synchronized(poObject )*/{
                persistentObject.setContents(msgVector);
                persistentObject.commit();
            }
            int i=0;
            while(eMsgs.hasMoreElements()){
                hashtable=(Hashtable)eMsgs.nextElement();
                String encryptedMessage=(String)hashtable.get("MessageToSend");
                if(!encryptedMessage.equals("")){
                    //check internet connection
                    String C0NNECTION_EXTENSION = checkInternetConnection();
                    if(C0NNECTION_EXTENSION==null)
                    {
                        Dialog.alert("Check internet connection and try again");
                        return;
                    }
                    else
                    {
                        MyScreen.PostMsgToServer(encryptedMessage);
                        hashtable.remove(encryptedMessage);
                    }
                }
                i++;
            }
    }

这只是我遇到的教程/示例的尝试。请帮助。

1 个答案:

答案 0 :(得分:1)

您显示的保存方法实际上并未将哈希表放入PersistentObject。尝试这样的事情:

public static void saveMessage(String msg){
    Hashtable hashtable = new Hashtable();
    persistentObject = PersistentStore.getPersistentObject(KEY);
    hashtable.put("MessageToSend", msg);
    persistentObject.setContents(hashtable);   // <- you were missing this
    persistentObject.commit();
}

那仍然可能不是伟大的实现,因为我假设你可能想多次调用saveMessage(),并向持久存储添加多条消息(?) 。这是对的,还是只能保存一条消息? (如果这是真的,你可以忽略saveMessage())的下一个建议

public static void saveMessage(String msg){
    persistentObject = PersistentStore.getPersistentObject(KEY);
    Hashtable hashtable = (Hashtable) persistentObject.getContents();
    if (hashtable == null) {
        // lazily initialize the store contents
        hashtable = new Hashtable();
        hashtable.put("MessagesToSend", new Vector());
    }
    Vector queuedMessages = (Vector) hashtable.get("MessagesToSend");
    queuedMessages.addElement(msg);
    // write the store contents to device storage
    persistentObject.setContents(hashtable);
    persistentObject.commit();
}


/** 
 * @param msg TODO: I'm not sure why msg needs to be passed, if 
 *                  saveMessage(msg) was called first? 
 */
public static void sendMessage(String msg){
    // TODO: you could choose to save the message here, so that the caller
    //   need not remember to call both sendMessage() and saveMessage()
    // saveMessage(msg);
    persistentObject = PersistentStore.getPersistentObject(KEY);
    Hashtable hashtable = (Hashtable) persistentObject.getContents();
    if (hashtable != null) {
         // check for saved messages first, and send them
         Vector msgVector = (Vector) hashtable.get("MessagesToSend");
         Enumeration eMsgs = msgVector.elements();
         Vector toDelete = new Vector();
         while (eMsgs.hasMoreElements()) {
              String encryptedMessage = (String)eMsgs.nextElement();

              // if the send was successful, you should delete message from the store
              toDelete.addElement(encryptedMessage);
         }

         eMsgs = toDelete.elements();
         while (eMsgs.hasMoreElements()) {
              // we can delete this sent message now
              msgVector.removeElement((String)eMsgs.nextElement());
         }
         // re-write the persistent store to the device
         persistentObject.setContents(hashtable);
         persistentObject.commit();
    }
}

我一般也希望引导你完成所有事情static ...那就是说,这真的是一个更大的,无关的问题,并且确实会发生你的持久存储对象可能是一个应用程序中唯一的全局对象(但是,更好的实现可能会避免所有这些static声明)。

更新:我对你如何称呼这两种方法有点不清楚。根据您的说明,当用户点击发送时,您似乎都会调用 saveMessage(msg)然后sendMessage(msg)。如果您首先使用我的实施方式保存邮件,则无需将msg传递到sendMessage(),因为sendMessage()将发送所有保存,队列中的未发送消息(向量)。因此,sendMessage()的API有一个不必要的参数。或者,我认为您可以将sendMessage(String)作为唯一的公开方法,并sendMessage(String)首先调用saveMessage(String)

无论如何,这取决于您,以及您希望方法调用语义的方式。存储和检索持久对象的基本问题应该通过上面的代码解决。