0

在过去的 6 个小时里,我一直在尝试使用 phonegap 持久化数据,我的第一种方法是使用 localStorate API,但每次应用程序重新启动时都会被杀死,所以它没用。现在我通过将文件写入文件系统来实现它,但存在以下问题:

  • 文件创建好了
  • 该文件具有正确的内容
    adb pull /data/data/[包名]/friends.txt)
    我可以看到文件的内容
  • 但是试图从中读取我总是得到NULL。

如果有人可以帮助我,这是我的代码......我现在没有想法:(

var friends = [];

// Initialize the Facebook SDK
document.addEventListener('deviceready', function() {
  FB.init({
      appId: '[myAppID]',
      nativeInterface: CDV.FB,
      useCachedDialogs: false
  });

  // Check if we already fetched our friends
  readSavedContent();

});

 function readSavedContent() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystemForRead, fail);
 }

 function gotFileSystemForRead(fileSystem) {
     fileSystem.root.getFile("friends.txt", {create: true, exclusive: false}, gotFileEntryForRead, fail);
 }

 function gotFileSystemForWrite(fileSystem) {
     fileSystem.root.getFile("friends.txt", {create: true, exclusive: false}, gotFileEntryForWrite, fail);
 }

  function gotFileEntryForRead(fileEntry) {
      var reader = new FileReader();
          reader.onloadend = function(evt) {
              alert("Data stored is " + evt.target.result);
          };
          reader.readAsText(fileEntry);
  }

 function gotFileEntryForWrite(fileEntry) {
     fileEntry.createWriter(gotFileWriter, fail);
 }

 function gotFileWriter(writer) {
     writer.onwriteend = function(evt) {
        // show a message
        alert(friends.length + " friends fetched, you should be able to see them if you restart the app");
     };
     writer.write(JSON.stringify(friends));
 }

 function fail(error) {
     console.log(error.code);
     alert(error);
 }

 function login() {
     FB.login(function (response) {
        console.log(JSON.stringify(response));

         if (response.status === "connected") {
             alert("logged in: fetching friends now");

             // Fetch my friends
             FB.api("/fql?q=" + encodeURIComponent('SELECT uid, first_name, last_name, email, pic, pic_big, is_app_user, devices FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1= me()) ORDER BY first_name LIMIT 2000'),
                 function(response) {
                     friends = response.data;
                     // Store the data to the disk                  
                     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystemForWrite, fail);
                 }
             );

         } else {
             alert('not logged in');
         }
     }, {
         scope: "email"
     });
 }

4

2 回答 2

2

据我所知,您在文件读取操作期间忘记了一步。

在您的情况下,您有以下订单:

 function gotFileSystemForRead(fileSystem) {
     fileSystem.root.getFile("friends.txt", {create: true, exclusive: false}, gotFileEntryForRead, fail);
 }

 function gotFileEntryForRead(fileEntry) {
     var reader = new FileReader();
     reader.onloadend = function(evt) {
         alert("Data stored is " + evt.target.result);
     };
     reader.readAsText(fileEntry);
 }

什么时候应该是这样的:

function gotFileSystemForRead(fileSystem) {
    fileSystem.root.getFile("friends.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
    fileEntry.file(gotFileEntryForRead, fail);
}

function gotFileEntryForRead(file) {
    var reader = new FileReader();
    reader.onloadend = function(evt) {
        alert("Data stored is " + evt.target.result);
    };
    reader.readAsText(file);
}

要了解更多信息,请查看此官方 Phonegape FileReader文档。

再说一次,您总是可以放弃此解决方案并使用持久性 js进行数据存储。它将为您提供 4+1 种不同的文件/数据存储选项,并且适用于多种平台。

于 2013-05-03T20:07:56.883 回答
2

附带说明一下,cordova(phonegap) 2.6 版中存在本地存储持久性问题,因此,如果您遇到这种情况,请尝试迁移到最近发布的 cordova 2.7 或降级到 2.5 以消除上述错误。

于 2013-05-03T21:45:26.903 回答