适用于Android,J2ME和PC的通用简单数据存储接口

时间:2012-03-22 12:28:46

标签: android java-me data-storage rms

我已经阅读了很多关于Android,J2ME和RecordStore的问题,但我仍然找不到能让我满意的答案。

我需要实现应该在不同平台上运行的Java应用程序的低级部分,现在这是Android和J2ME,并且将来它也应该在PC上运行。我需要存储简单的数据集,这与J2ME中的RecordStore几乎相似:

应用程序应拥有多个记录存储记录,每条记录都有:

  • id(但它应该是“我的”ID,而不是RecordStore中自动返回的那个),
  • 数据(只是一个字节数组)。

我认为我应该用必要的方法编写Interface,每个平台都应该有自己的Interface实现。

但是这个任务似乎很常见(至少对于Android + J2ME而言),所以,也许已经有一些轻量级的实现?我问的只是因为我不喜欢重新发明轮子。

也许有些建议?

2 个答案:

答案 0 :(得分:1)

所以,我编写了满足我要求的界面,有两个实现:Android和J2ME。

以下是界面的外观:

public interface ISDataStore {


   /**
    * Get number of records in data store.
    */
   public int getNumRecords() throws SDataStoreException;

   /**
    * Get size of one record with specified id in bytes.
    *
    * @param record_id id of the record
    */
   public int getRecordSize(int record_id) throws SDataStoreException;

   /**
    * Get record.
    *
    * @param record_id id of the record to read
    * @param data byte array where to put the data
    * @param offset offset in 'data' array from which should start to copy
    */
   public void getRecord(int record_id, byte[] data, int offset) throws SDataStoreException;

   /**
    * Get record.
    *
    * @param record_id id of the record to read
    */
   public byte[] getRecord(int record_id) throws SDataStoreException;

   /**
    * Resolves is record with specified id exists or not.
    *
    * @param record_id id of the record
    * @return true if record exists, otherwise false
    */
   public boolean isRecordExists(int record_id) throws SDataStoreException;

   /**
    * Put new record or update existing one.
    *
    * @param record_id id of the record
    * @param data byte array of data
    * @param offset offset in the data byte array
    * @param length number of bytes to store
    *
    * @return true if operation was successful, otherwise false
    *
    */
   public boolean setRecord(int record_id, byte[] data, int offset, int length) throws SDataStoreException;

   /**
    * Delete the record.
    *
    * @param record_id id of the record
    *
    * @return true if operation was successful, otherwise false
    */
   public boolean deleteRecord(int record_id) throws SDataStoreException;

   /**
    * Clear all the records.
    */
   public void deleteAll() throws SDataStoreException;

   /**
    * Close the data store.
    */
   public void close() throws SDataStoreException;

}

还有一个数据存储工厂:

public interface ISDataStoreFactory {

   /**
    * @param dataStoreId id of the data store
    * @return ISDataStore with given id. Type of this id depends on target platform
    */
   public ISDataStore getDataStore(Object dataStoreId) throws SDataStoreException;

   /**
    * Destroys data store with given id.
    * @param dataStoreId id of the data store. Type of this id depends on target platform
    */
   public void destroyDataStore(Object dataStoreId) throws SDataStoreException;

}

可以找到Doxygen自动生成的文档here

带有Interface的Mercurial存储库和所有实现都可以找到here

我如何使用它:

正如我在我的问题中已经说过的,我有Android应用程序和J2ME应用程序,这两个应用程序都做类似的事情。 (如果有兴趣,他们通过蓝牙与远程嵌入式设备进行通信)

两个应用程序都有共同的低级部分,可以完成主要工作。

我有interface IMainApp,类似的东西:

public interface IMainApp {

   public ISDataStoreFactory getDataStoreFactory();

   /*
    * ... some other methods
    */
}

这两个应用程序(适用于Android和J2ME)都有自己的此接口实现,并将其引用传递给低级部分。当低级部件想要打开某个数据存储时,它使用ISDataStoreFactory返回的IMainApp.getDataStoreFactory。它就像我希望它一样工作。

希望它对任何人都有用。

答案 1 :(得分:0)

告诉你我做了什么

创建配置文件..这是我想要存储在数据库中的值

示例资料

    public class NewsProfile {

        public String uniqid = "", category = "", title = "" ;

        public NewsProfile(String uniqid) {
            this.uniqid = uniqid;
        }
    }

创建接受新个人资料的新闻商店

public void saveProfile(int id, NewsProfile profile) {
    try {
        if (rs != null) {
            profile.id = id;
            byte[] bytes = toByteArray(profile);
            setRecord(profile.id, bytes);
            System.err.println("Exists = " + profile.catKey + String.valueOf(profile.status));
        }
    } catch (Exception e) {
        System.err.println("ERROR: saveUpdateProfile" + e.getMessage());
    }
}

public NewsProfile getProfileint id) throws RecordStoreException, IOException {
    byte[] bytes = rs.getRecord(id);
    DataInputStream is = new DataInputStream(new ByteArrayInputStream(bytes));
    String uniqid = is.readUTF();
    OptionsProfile profile = new NewsProfile (uniqid);
    profile.id = id;
    profile.catKey = uniqid;
    profile.category = is.readUTF();
    profile.title = is.readUTF();
    return profile;
}

private byte[] toByteArray(NewsProfile profile) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream os = new DataOutputStream(baos);
    os.writeUTF(profile.uniqid);
    os.writeUTF(profile.category);
    os.writeUTF(profile.title);
    return baos.toByteArray();
}

这意味着任何时候我想将数据保存到数据库......在任何时候保存的是NewsProfile ....你不能为你想要的不同存储实现.. SQLite,RMS,甚至是Web服务

由于 :)