Originally published in the TGC Newsletter – June 1, 2014
The question of transferring data from your mobile app to a server was brought up by Parry on the AGK forums and I thought I’d share my process for getting app data from a device. The problem is that Android and iOS put app data in a protected folder on the device. If you want to root or jailbreak the device it is easy to get at, but this process can be dangerous and complex. If you just need a peek at your app’s data then there is an easier way: add all of the files to a ZIP file and send it to a server.
Setup
To start you’ll need a server. You can use your hosted account or a server of your own. If you’re on Windows I highly suggest using XAMPP. The setup is quick and they provide ample information on how to get things going (like allowing outside connections to your server). The next thing you’ll want to do is set up some PHP code on the server to accept files as well as the code in AGK to send the files.
AGK code for sending file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
fileID = OpenToWrite("test.txt" , 0) WriteLine(fileID , "this is a test") CloseFile(fileID) cid = CreateHTTPConnection() SetHTTPHost(cid, "1.1.1.1" , 0) SendHTTPFile(cid , "uploadfile.php" , "" , "test.txt") SetPrintSize(1) repeat if GetHTTPResponseReady( cid ) = 1 print (GetHTTPResponse( cid )) else print ( GetHTTPFileProgress( cid )) endif Sync() until done = 1 CloseHTTPConnection(cid) DeleteHTTPConnection(cid) END |
Ensure that the IP Address 1.1.1.1 is replaced with your own IP address or domain (do not include HTTP://).
PHP code for receiving file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?PHP if ($_FILES["myfile"]["error"] > 0){ echo "Error: " . $_FILES["myfile"]["error"] . "n"; } else { echo "Upload: " . $_FILES["myfile"]["name"] . "n"; echo "Type: " . $_FILES["myfile"]["type"] . "n"; echo "Size: " . ($_FILES["myfile"]["size"] / 1024) . " Kbn"; echo "Stored in: " . $_FILES["myfile"]["tmp_name"] . " n"; if (file_exists("upload/" . $_FILES["myfile"]["name"])) { echo $_FILES["myfile"]["name"] . " already exists. nn"; } else { move_uploaded_file($_FILES["myfile"]["tmp_name"], "upload/" . $_FILES["myfile"]["name"]); echo "Stored in: " . "upload/" . $_FILES["myfile"]["name"] . " nn"; } } ?> |
It’s as simple as that. Just make sure that the “upload” directory is created on your server before trying to send the file. Now that you have all of that set up and tested we’re on to a simple set of code that will get all of the files out of your app’s directory:
AGK Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function ZipDirectory(sZipFileName$) if GetFileExists(sZipFileName$) = 1 DeleteFile(sZipFileName$) endif doOnce = 0 makeZip = 0 repeat if doOnce = 0 doOnce = 1 thisFile$ = GetFirstFile() else thisFile$ = GetNextFile() endif if thisFile$ = “” done = 1 else if makeZip = 0 makeZip = 1 zipFileID = CreateZip(sZipFileName$) endif if GetFileExists(thisFile$) = 1 zipLocation$ = GetFolder() + “/” + thisFile$ AddZipEntry( zipFileID , thisFile$ , zipLocation$) endif endif until done = 1 if zipFileID > 0 CloseZip(zipFileID) endif endfunction |
As you may imagine you can also do this for every directory and subdirectory for you app’s media folder. It is important to note that the directory you are working with is the write directory for the AGK app and not the read directory. The read directory for apps is where all of the media files exist that you package in with your app. On Android and iOS you do not have access to this directory without rooting/jailbreaking the device. This shouldn’t matter because these files are the same that you packed with the app and you already have those. The write directory is a “sandbox” area where the app is allowed to write data. This is where any file you create with the app will be stored such as data files, debug logs, etc.
This method is extremely helpful when developing your app and when it is actually live. When developing your app you can use this to zip up any debug logs and quickly send them to yourself. When the app is live this can be used to send error reports to you when the user experiences issues. In Wordspionage we use this to send error logs whenever a fatal error occurs. This was extremely helpful in the first few months of our release because a few bugs appeared that did not appear in our extensive beta testing. It allowed us to receive data from our users automatically so they didn’t need to be bothered or actually need to hit a button to send the report. When these error reports are sent to us they also include a screen shot of the game so we can see any visual clues to the issue. This method was paramount to smoothing out the flaws in the game.