Here is an update on my problem:
So basically what I'm trying to do;
Given that I have a host name and a server file path,
host and
serverfile, how can I find out if
GetHTTPFile() was successful?
The code below is a complete working example:
In the
MainLoop() function I set a
host and
serverfile.
getHTTPImageFile() is supposed to return the path of the local file to where the
serverfile was downloaded. If it fails to download the image it should return an empty string.
What happens now:
- If the
host and
serverfile are correct it works.
OK
- If the
host is wrong it returns an empty string.
OK
- If the
host is correct but the
serverfile is wrong, is still returns the local file path.
NOT OK
The code:
SetDisplayAspect( 4.0/3.0 )
MainLoop()
end
function MainLoop()
host as String = "images.freeimages.com"
serverfile as String = "images/large-previews/dfa/jungle-1377573a.jpg"
imagepath as String
imagepath = getHTTPImageFile(host, serverfile)
imageID as integer
if len(imagepath) > 0
imageID = LoadImage(imagepath)
else
imageID = CreateImageColor(20, 20, 230, 200)
endif
sprite = createSpritePicture(imageID)
do
if getpointerpressed() then exit
Sync()
SetSpriteAngle(sprite, GetSpriteAngle(sprite) + 0.25)
loop
endfunction
function getHTTPImageFile(host as String, serverFile as String)
//Set up connection:
iHTTP = CreateHTTPConnection()
ret = SetHTTPHost(iHTTP, host, 1)
SendHTTPRequestASync(iHTTP, serverFile)
while not GetHTTPResponseReady(iHTTP)
print("Connecting...")
Sync()
endwhile
//Returns empty string if host name is wrong:
if GetHTTPResponseReady(iHTTP) = -1 then exitfunction ""
//Get image file:
localFile as String = "temp_image.jpg"
f = GetHTTPFile(iHTTP, serverFile, localFile, "")
//If the serverfile was wrong, this will return empty string
//BUT only in debug mode!
if GetHTTPFileProgress(iHTTP) > 99.9 then exitfunction ""
if GetHTTPFileComplete(iHTTP) then exitfunction ""
p as float
while GetHTTPFileComplete(iHTTP) <> 1
p = GetHTTPFileProgress(iHTTP)
//Does not return -1 when the file was wrong:
if GetHTTPFileComplete(iHTTP) = -1 then exitfunction ""
print("Downloading..." + str(p,0) + "%")
Sync()
endwhile
//Does not return -1 when the file was wrong:
if GetHTTPFileComplete(iHTTP) = -1 then exitfunction ""
CloseHTTPConnection(iHTTP)
DeleteHTTPConnection(iHTTP)
if not GetFileExists(localFile) then exitfunction ""
endfunction localFile
function createSpritePicture(imageID as integer)
sprite = CreateSprite(imageID)
setspritepositionbyoffset(sprite, 50, 50)
setspritesize(sprite, 75, -1)
endfunction sprite
By leaving the
host and
serverfile string values as they are, running this code should show a rotating picture of a forest.
By editing the
host, a blue plane image should be shown.
By editing the
serverfile value a default appgamekit error image will be shown. (Here I want the same blue image as when
host is wrong.
If I run in
Debug, and step through the code, this actually works. But running in
Run mode, it does not.