<Background> Don't read this if you are just interested in the code
I was a little interested in syncing some data from my PC and android phone to the same site with ease. I realized in APK2 that in Tier1 you cannot write to the external storage (I could be wrong but I found no code as of yet, would be nice to do so thought). So I thought about writing a web service, I found some code and modified it to suit my needs.
So what the code does is to check the media directory for text files (txt) and upload all of them to a web script. If the file has been uploaded it is deleted in the local directory. If the file fails it is left for a next upload session.
Both the application and the web script keeps a log (the application log tends to get big due to the way it is written, you'll see he he)
<Code> Here is the business part
I only wrote one file for this because I wanted to keep it as simple as possible. First is the application code and then the web script.
Application code (main.agc)
// Project: HTTP_Test
// Created: 2015-09-07
// set window properties
SetWindowTitle( "HTTP_Test" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
Wlog("Set Window Properties")
s_FileName$ = "Data_01.txt"
i_FileID = OpenToWrite(s_FileName$, 0)
WriteLine(i_FileID, "Line 01")
CloseFile(i_FileID)
s_FileName$ = "Data_02.txt"
i_FileID = OpenToWrite(s_FileName$, 0)
WriteLine(i_FileID, "Line 02")
CloseFile(i_FileID)
s_FileName$ = "Data_03.txt"
i_FileID = OpenToWrite(s_FileName$, 0)
WriteLine(i_FileID, "Line 03")
CloseFile(i_FileID)
s_FileName$ = "Data_04.txt"
i_FileID = OpenToWrite(s_FileName$, 0)
WriteLine(i_FileID, "Line 04")
CloseFile(i_FileID)
s_FileName$ = "Data_05.txt"
i_FileID = OpenToWrite(s_FileName$, 0)
WriteLine(i_FileID, "Line 05")
CloseFile(i_FileID)
Wlog("Wrote Files")
i_WebConBusy = 0
i_GotFirstFile = 0
s_CurrentFile$ = ""
Wlog("Set Properties")
c_WebCon = OpenWebConnection("192.168.30.178")
CloseWebConnection(c_WebCon)
Wlog("Did Test Connection")
do
Print("FPS: " + str(ScreenFPS()))
if s_CurrentFile$ = ""
if i_WebConBusy = 0
Wlog("Current file = ''")
Wlog("WebConBusy = 0")
Wlog("Getting File Name")
if i_GotFirstFile = 0
Wlog("GotFirstFile = 0")
s_CurrentFile$ = GetFirstFile()
i_GotFirstFile = 1
else
s_CurrentFile$ = GetNextFile()
endif
Wlog("Current File = " + s_CurrentFile$)
// Check for extension to upload txt files only
if GetFileExtension(s_CurrentFile$) = "txt"
i_WebConBusy = 1
c_WebCon = OpenWebConnection("192.168.30.178")
Wlog("Opened Web Connection")
SendHTTPFile(c_WebCon, "upload/upload.php", "", s_CurrentFile$)
Wlog("Sending File")
else
s_CurrentFile$ = ""
endif
endif
endif
Print("Current File: " + s_CurrentFile$)
//Print("Current File Extension: " + GetFileExtension(s_CurrentFile$))
if i_WebConBusy = 1
if GetHTTPResponseReady(c_WebCon) = 1
Wlog("i_WebConBusy = 1")
s_Response$ = RemoveSpaces(GetHTTPResponse(c_WebCon))
Wlog("s_Response = " + s_Response$)
if s_Response$ = "done"
DeleteFile(s_CurrentFile$)
Wlog("DeleteFile: " + s_CurrentFile$)
endif
CloseWebConnection(c_WebCon)
i_WebConBusy = 0
Print("response: " + s_Response$)
Wlog("File Uploaded: " + s_CurrentFile$)
s_Response$ = ""
s_CurrentFile$ = ""
else
Print("Progress: " + str(GetHTTPFileProgress(c_WebCon)))
endif
endif
Sync()
loop
/**********************
* Functions *
**********************/
function OpenWebConnection(s_Domain$)
WebCon = CreateHTTPConnection()
SetHTTPHost(WebCon, s_Domain$ , 0)
endfunction WebCon
function CloseWebConnection(WebCon)
CloseHTTPConnection(WebCon)
DeleteHTTPConnection(WebCon)
endfunction
function GetDateTimeNow()
Date$=GetCurrentDate()
Time$=GetCurrentTime()
ConCat$ = Mid(Date$,1,4) + Mid(Date$,6,2) + Mid(Date$,9,2) + Mid(Time$,1,2) + Mid(Time$,4,2) + Mid(Time$,7,2)
endfunction ConCat$
function GetDateNow()
Date$=GetCurrentDate()
ConCat$ = Mid(Date$,1,4) + Mid(Date$,6,2) + Mid(Date$,9,2)
endfunction ConCat$
function GetTimeNow()
Time$=GetCurrentTime()
ConCat$ = Mid(Time$,1,2) + Mid(Time$,4,2) + Mid(Time$,7,2)
endfunction ConCat$
function GetFileExtension(s_FilePath$)
s_FileExtension$ = ""
s_FileExtension$ = Mid(s_FilePath$,len(s_FilePath$) - 2, 3)
endfunction s_FileExtension$
function Wlog(s_Log$)
s_FileName$ = "System.log"
i_FileID = OpenToWrite("System.log", 1)
WriteLine(i_FileID, GetDateTimeNow() + ":" + s_Log$)
CloseFile(i_FileID)
endfunction
function RemoveSpaces(s_Line$)
s_Return$ = ""
s_Char$ = ""
i_Len_Line = len(s_Line$)
i_Count = 0
for i_Count = 0 to i_Len_Line
s_Char$ = mid(s_Line$,i_Count,1)
if not s_Char$ = " "
s_Return$ = s_Return$ + s_Char$
endif
next i_Count
endfunction s_Return$
Web Script (upload.php)
<?php
$s_FileName = fopen("Log.txt", "a");
fwrite($s_FileName,"<-------NEW UPLOAD------->"."\r\n");
$save_dir_sub = "";
if(isset($_POST["name"])){
$save_dir = "upload/";
$save_dir_sub = $_POST["name"]."/";
if(!file_exists($save_dir.$save_dir_sub)){
mkdir($save_dir.$save_dir_sub);
fwrite($s_FileName, "Sub Directory: ".$save_dir.$save_dir_sub."\r\n");
}
}
foreach ($_FILES as $key => $item) {
//$array[$key + 1] = $item + 2;
//echo "$item\n";
//echo $key."\r\n"."<br />";
//echo $item["name"]."\r\n"."<br />";
$save_dir = "upload/".$save_dir_sub;
$save_path = $save_dir.basename($item["name"]);
$save_info = pathinfo($save_path,PATHINFO_EXTENSION);
$save_temp = $item["tmp_name"];
$save_name = $item["name"];
//echo $save_dir."<br />"."\r\n";
//echo $save_path."<br />"."\r\n";
//echo $save_info."<br />"."\r\n";
//echo $save_temp."<br />"."\r\n";
fwrite($s_FileName,"File: ".$save_path."\r\n");
if(file_exists($save_path)) {
fwrite($s_FileName,"file exists"."\r\n");
echo "exists";
}else{
if(move_uploaded_file($save_temp, $save_path)) {
fwrite($s_FileName,"file uploaded"."\r\n");
echo "done";
}else{
fwrite($s_FileName,"file error"."\r\n");
echo "error";
}
}
}
fclose($s_FileName);
?>