Javascript 文件阅读器&;文件编写器问题(cordova 2.0.0 js)

Javascript 文件阅读器&;文件编写器问题(cordova 2.0.0 js),javascript,android,cordova,Javascript,Android,Cordova,我一直在为Android*设备编写这段PhoneGap(cordova-2.0.0.js)代码,以保存一些数据。我收到一个奇怪的错误代码,文件似乎没有被写入。我从示例代码开始,可以成功地将内联字符串写入文件句柄,因此我确信我的权限和所有权限都是正确的 也许我没有正确处理所有的回调?有很多东西要听!它可能是带有truncate(0)的东西;我很难找到很多关于它的文档。我是否需要多次调用window.requestFileSystem?我做了2次来注册不同的回调。如果不是这样,是什么导致了错误 关于

我一直在为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;
} 
var-CREDENTIALS\u-FILE\u-NAME=“CREDENTIALS.json”;
风险值凭证;
//INIT--等待PhoneGap加载
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
函数ondevicerady(){
requestFileSystem(LocalFileSystem.PERSISTENT,0,initCredentialReader,fail);
}
函数initCredentialReader(文件系统){
getFile(凭证文件名,{create:true},gotfilentryreader,fail);
}
函数gotFileEntryReader(fileEntry){
fileEntry.file(gotFileToRead,fail);
}
函数gotFileToRead(文件){
var reader=new FileReader();
reader.onloadend=函数(e){
log(“--文件读取器:+e.target.result”);
如果(如目标、结果、长度<1){
credentials=newCredentials();
}否则{
credentials=JSON.parse(e.target.result);
}
};
reader.readAsText(文件);
}
//端链
函数initCredentialWriter(文件系统){
getFile(凭证文件名,{create:true},gotfilentrywriter,fail);
}
函数gotFileEntryWriter(fileEntry){
createWriter(gotFileWriter,失败);
}
函数gotFileWriter(writer){
writer.onwrite=函数(e){
console.log(“--write success”);
};
var toWrite=JSON.stringify(凭证);
console.log(“--toWrite:+toWrite”);
writer.truncate(0);
writer.seek(0);
写作(toWrite);
}
功能失败(错误){
console.log(“--write FAIL:+error.code”);
}
函数newCredentials(){
log(“返回新凭据!”);
返回{
“用户名”:“”,
“密码”:“,
“组织”:“,
“cookieValue”:“};
}
函数getCredentials(){
console.log(“--getCredentials:+credentials);
返回凭证;
}
函数saveCredentials(jsonCredentials){
log('--saveCredentials-jsonCredentials:'+jsonCredentials);
credentials=JSON.stringify(jsonCredentials);
console.log('---要保存的凭据:'+凭据)
requestFileSystem(LocalFileSystem.PERSISTENT,0,initCredentialWriter,fail);
返回凭证;
} 
错误 08-14 18:41:38.839:I/Web控制台(2678):成功回调中出错: 文件9= {“代码”:7,“行”:2863,“表达式开始偏移”:91407,“表达式内部偏移”:91455,“源ID”:4122528,“源URL”:file:///android_asset/www/cordova-2.0.0.js"} 在file:///android_asset/www/cordova-2.0.0.js:258


因此,调用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