0

我一直在为 Android* 设备编写一段 PhoneGap (cordova-2.0.0.js) 代码,以保留一些数据。我收到了这个奇怪的错误代码,而且文件似乎没有被写入。我从示例代码开始,可以成功地将内联字符串写入文件句柄,因此我确定我的权限和所有权限都是正确的。

也许我没有正确处理所有回调?有很多要听的!可能是 truncate(0); 我很难找到很多关于它的文档。我需要多次调用 window.requestFileSystem 吗?我做了 2 次来注册不同的回调。如果不是这样,是什么导致了错误?

也很乐意接受有关减少读取 + 写入操作的总行数的建议...

*模拟器运行Android 2.3.4

这是我的代码:

var CREDENTIALS_FILE_NAME = "credentials.json";
var credentials;

// INIT -- Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, initCredentialReader, fail);
}
function initCredentialReader(fileSystem) {
    fileSystem.root.getFile(CREDENTIALS_FILE_NAME, {create: true}, gotFileEntryReader, fail); 
}
function gotFileEntryReader(fileEntry) {
    fileEntry.file(gotFileToRead, fail);
}
function gotFileToRead(file){
    var reader = new FileReader();
    reader.onloadend = function(e) {
        console.log("--FILE READER: "+e.target.result);

        if( e.target.result.length < 1 ) {
            credentials = newCredentials();
        } else {
            credentials = JSON.parse( e.target.result );
        }
    };
    reader.readAsText(file);
}
// END CHAIN

function initCredentialWriter(fileSystem) {
    fileSystem.root.getFile(CREDENTIALS_FILE_NAME, {create: true}, gotFileEntryWriter, fail); 
}
function gotFileEntryWriter(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
    writer.onwrite = function(e) {
        console.log("--- write success");
    };
    var toWrite = JSON.stringify(credentials);
    console.log("--- toWrite: "+toWrite);
    writer.truncate(0);
    writer.seek(0);
    writer.write(toWrite);
}


function fail(error) {
    console.log("--- write FAIL: "+error.code);
}

function newCredentials() {
    console.log("returning newCredentials!");
    return {
        "username" : "",
        "password" : "",
        "organization" : "",
        "cookieValue" : "" };
}

function getCredentials() {
    console.log("--- getCredentials: "+credentials);
    return credentials;
}

function saveCredentials( jsonCredentials ) {
    console.log('--- saveCredentials jsonCredentials: '+ jsonCredentials);

    credentials = JSON.stringify( jsonCredentials );
    console.log('--- credentials to save: '+credentials)

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, initCredentialWriter, fail);
    return credentials;
} 

错误

08-14 18:41:38.839: I/Web Console(2678): 成功回调错误: File9 = {"code":7,"line":2863,"expressionBeginOffset":91407,"expressionEndOffset":91455," sourceId":4122528,"sourceURL":"file:///android_asset/www/cordova-2.0.0.js"} 在 file:///android_asset/www/cordova-2.0.0.js:258

4

1 回答 1

1

因此,事实证明调用 truncate() 和 write() 不是异步正确的——只是实现了更多回调,如下所示:

function initCredentialTruncate(fileSystem) {
    fileSystem.root.getFile(CREDENTIALS_FILE_NAME, {create: true}, gotFileEntryTruncate, fail); 
}
function gotFileEntryTruncate(fileEntry) {
    fileEntry.createWriter(gotFileTruncate, fail);
}
function gotFileTruncate(writer) {
    writer.onwrite = function(e) {
        console.log("--- truncate success");
    };
    writer.truncate(0);
    //writer.seek(0);
}
// END CHAIN

并在必要时调用 init 函数。谢谢你让我发泄,StackOverflow ......

于 2012-08-16T05:43:31.880 回答