samedi 25 juin 2016
GAPI insert call works on Android, but fails on iOS (using PhoneGap, and Javascript)
Before reading on: Please keep in mind that this code works on Android devices but not on iPhones, so my question is what I need to adapt to make it work on both.
I am working on an application for both Android and iOS, using PhoneGap as the bridge. The goal is to fill out a table and click submit, and have it save a file to Google Drive with the data from the table.
I currently have the sign-in portion working and tested both on Android and iOS. The next step is to get the call to Google's Insert method (from gapi) working.
Below is the code that I have. I will comment it so it is (hopefully) easily read.
// print the message to the console, and also add it to the fake log
// so that the message can be seen in the application (because mobile
// devices don't have a console).
function log(mess) {
$('#logs').prepend('<pre>' + mess + '</pre>');
console.log(mess);
}
// This function is called when the 'Create File' button is clicked.
// For now, it only creates a blank file, but will be modified to
// create a file with a particular title and contents.
// Note that it currently only calls insertNewFile
function createFile() {
log("Called createFile!");
log("Calling insertNewFile...");
insertNewFile();
}
// This function is the callback function, which is called by the
// gapi Insert request once the request finishes.
// All it does is print the returning file (to make sure it was
// created)
function createFileCallback(file) {
log("Called createFileCallback!");
log(JSON.stringify(file));
}
// This is the function called when the button is clicked.
// Currently, it creates a file with a single space character,
// and sets the type to be "text/plain"
function insertNewFile() {
log("Called insertNewFile!");
// Create a variable with the intended contents
var content = " ";
// Create a variable to store the converted contents
var contentArray = new Array(content.length);
for (var i = 0; i < contentArray.length; i++) {
contentArray[i] = content.charCodeAt(i);
}
var byteArray = new Uint8Array(contentArray);
// create a variable with the contents type
var conType = "text/plain";
// Create a 'Blob'
// Not entirely sure how this works, but it stores the file
// data, and is passed to the GAPI Insert method
var blob;
// On some verions of android, the Blob() constructor is illegal,
// so catch the error and use the old version of it (BlobBuilder)
try {
blob = new Blob([byteArray], {type: conType});
} catch (error) {
log("Creating the Blob failed!");
log("Trying with a BlobBuilder...");
BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
var tmpBlob = new BlobBuilder();
tmpBlob.append(byteArray);
blob = tmpBlob.getBlob(conType);
}
log("Calling insertFile!");
insertFile(blob, createFileCallback);
}
// The following is the function provided by Google (see link to
// documentation below
// The only difference is the 'log()' calls that I added for testing
function insertFile(fileData, callback) {
log("called insertFile!");
const boundary = '-------314159265358979323846';
const delimiter = "rn--" + boundary + "rn";
const close_delim = "rn--" + boundary + "--";
log("Generating FileReader...");
var reader = new FileReader();
reader.readAsBinaryString(fileData);
log("Setting FileReader.onload...");
reader.onload = function(e) {
log("Called FileReader.onload!");
var contentType = fileData.type || 'application/octet-stream';
var metadata = {
'title': "untitled.txt",
'mimeType': contentType
};
var base64Data = btoa(reader.result);
var multipartRequestBody =
delimiter +
'Content-Type: application/jsonrnrn' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + 'rn' +
'Content-Transfer-Encoding: base64rn' +
'rn' +
base64Data +
close_delim;
log("Request is:");
log(multipartRequestBody);
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
log(file);
};
}
log("Excuting request...");
log(JSON.stringify(request, null, 4));
request.execute(callback);
}
}
Here is the link to Google's documentation, and the exact function you see above.
As I said above, this works on Android devices. The log("Called createFileCallback!"); message is printed, and the file is created in Google drive.
However, on iOS devices, the last thing printed is the log(JSON.stringify(request, null, 4)); (4 lines from the end). The callback message is never printed, and the file is never created in drive.
Any idea what I am doing wrong?
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire