4

phonegap 1.5.0を使用してAndroidアプリを開発しています

アプリはデータをローカル データベースに保存し、Web サービスを使用して同期します。ワンクリックで同期データ JavaScript 関数を呼び出しています。私が見つけたものは次のとおりです。

この同期ボタンのクリック イベントは 2 回発生します。単一のレコードが 2 回アップロードされ、アップロードされた時間はサーバー データベースで同じです。

これを回避する方法は?

ありがとう

編集:

皆様、お返事が遅くなり申し訳ありません。優先度の高い仕事で少し忙しかった。

ボタンのクリック時にSyncData()関数を呼び出します。

コードは次のとおりです。

document.addEventListener("DOMContentLoaded", onDeviceReady, false);
var db;
var maxrecords = 0;
var recordsprocessed = 0;
var urlstart = "http://www.mywebsite.com/";

function onDeviceReady() {
    db = window.openDatabase("LocalDB", "1.0", "PhoneGap Demo", 50 * 1024 * 1024);
}

function SyncData() {
    maxrecords = 0;
    recordsprocessed = 0;
    db.transaction(queryDB, errorCB, successCB);
}

function queryDB(tx) {
    var entriestoupload = 0;
    var profimagetoupload = 0;

    //check how many entries are to be synchronized
    tx.executeSql('SELECT count(*) as cnt FROM tblEntries WHERE IsUploaded=0', [], function(tx, results) {
        entriestoupload = parseInt(results.rows.item(0)['cnt']);
        //check how many profile images are to be uploaded
        tx.executeSql('SELECT count(*) as cnt FROM tblEntries WHERE IsProfileImageUploaded=0', [], function(tx, results) {
            profimagetoupload = parseInt(results.rows.item(0)['cnt']);

            //synch proceeds if there is any record which is not sychronised
            if (entriestoupload > 0 || profimagetoupload > 0) {
                var dataMsg = '';
                var porofimgMsg = '';
                if (entriestoupload > 0) dataMsg = entriestoupload + ' entry, ';
                if (profimagetoupload > 0) porofimgMsg = profimagetoupload + ' profile image ';

                // give user exact info about what will be synchronized 
                if (confirm(dataMsg + porofimgMsg + ' are not synchronized.\n Do you want to start synchronisation? \n Please wait till synch successfull message appears.')) {

                    //start synchronisation
                    tx.executeSql('SELECT * FROM tblEntries ORDER BY DateOfRegistration DESC', [], uploadData, errorCB);
                }
            } else {
                alert('All records are already Synchronized.');
            }
        }, errorCB);
    }, errorCB);
}

function uploadData(tx, results) {
    var len = results.rows.length;
    maxrecords = len;
    var Synched = 0;
    if (len > 0) {
        for (var i = 0; i < len; i++) {
            var row = results.rows.item(i);
            var LocalId = row['LocalId'];
            var DateOfRegistration = getDateTimeformatMySql(String(row['DateOfRegistration']));
            var DateOption = getDateTimeformatMySql(String(row['DateOption']));
            var VolunteerId = row['VolunteerId'];
            var IsUploaded = row['IsUploaded'];
            var LiveId = row['LiveId'];
            var ProfileImagePath = row['ProfileImagePath'];
            var IsProfileImageUploaded = parseInt(row['IsProfileImageUploaded']);
            var params = null;
            var weburl = null;

            if (IsUploaded == 0) {

                //set parameters for web service
                params = "LocalId=" + LocalId + "&OrganizationName=" + row['OrganizationName'] + "&FirstName=" + row['FirstName'] + "&LastName=" + row['LastName'] + "&EmailAddress=" + row['EmailAddress'] + "&MobileNumber=" + row['MobileNumber'] + "&Country=" + row['Country'] + "&State=" + row['State'] + "&City=" + row['City'] + "&Lattitude=" + row['Lattitude'] + "&Longitude=" + row['Longitude'] + "&Website=" + row['Website'] + "&DateOption=" + DateOption + "&TimeOption=" + row['TimeOption'] + "&NumberOption=" + row['NumberOption'] + +"&RadioOption=" + row['RadioOption'] + "&Details=" + row['Details'] + "&CheckBoxoption=" + row['CheckBoxoption'] + "&DropDownOption=" + row['DropDownOption'] + "&DateOfRegistration=" + DateOfRegistration + "&VolunteerId=" + VolunteerId;

                //web service url
                weburl = urlstart + "mywebserviceurl";

                try {
                    $.ajax({
                        async: false,
                        type: "POST",
                        url: weburl,
                        data: params,
                        dataType: "json",
                        success: function(data, textStatus, jqXHR) {
                            if (data.Success == "0") {
                                alert('web services error:\n' + data.Message);
                            }
                            if (data.Success == "1") {
                                try {
                                    LiveId = parseInt(String(data.LiveId));
                                    IsUploaded = 1;

                                    //Update local database to set IsUploaded and LiveId
                                    tx.executeSql("UPDATE tblEntries SET LiveId= " + LiveId + " ,IsUploaded=1 WHERE LocalId= " + LocalId, [], function(tx, results) {
                                        Synched = Synched + 1; /*alert(LiveId);*/
                                    }, errorCB);
                                    //check if profile image exists or not
                                    if (ProfileImagePath != undefined) {
                                        uploadImage(LiveId, LocalId, ProfileImagePath, '1', '');
                                    }

                                } catch (e) {
                                }
                            }
                        },
                        error: function() {
                            alert("There was an error loading the feed");
                        }
                    });
                } catch (e) {
                }
            } else {
                //check if data is uploaded and image is not uploaded
                if (IsProfileImageUploaded == 0 && ProfileImagePath != undefined) {
                    uploadImage(LiveId, ShopId, ProfileImagePath, '1', '');
                }
            }
        }
    }
} // end of querySucess function

//function to upload image

function uploadImage(LiveId, LocalId, ImagePath, UploadType, CreationDate) {
    try {

        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = ImagePath;
        options.mimeType = "image/jpg";

        var params = new Object();
        params.LiveId = LiveId;
        params.LocalId = LocalId;
        params.UploadType = UploadType;
        params.CreationDate = CreationDate;
        options.params = params;
        options.chunkedMode = false;
        var ft = new FileTransfer();
        var url = urlstart + "mywebservice_url_to_uploadimage";

        ft.upload(ImagePath, url, win, fail, options, false);

    } catch (e) {
        console.error("Survey App Err :" + e.message);
    }
}

function win(r) {
    var jsonresponse = r.response.substring(r.response.indexOf('{'), r.response.indexOf('}') + 1);
    var obj = $.parseJSON(jsonresponse);

    if (obj.Success == "1") {

        var LocalId = parseInt(obj.LocalId);
        var UploadType = parseInt(obj.UploadType);
        if (UploadType == 1) {
            db.transaction(function(tx) {
                tx.executeSql("UPDATE tblEntries SET IsProfileImageUploaded=1 WHERE LocalId=?", [LocalId], function(tx, results) {
                }, errorCB);
            }, errorCB, successCB);
        }
    }
}

function fail(error) {
    alert("There was an error uploading image");
}
4

1 に答える 1