API call works on livecode, but not on android

The place to discuss anything and everything about running your LiveCode on Android

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
paulalsmith1000
Posts: 52
Joined: Sat Jun 15, 2019 10:09 am

API call works on livecode, but not on android

Post by paulalsmith1000 » Wed Aug 21, 2019 7:39 pm

Hi Everyone

Could someone possibly shed light on this for me I am absolutely stumped.

Is there any reason why a command that calls an API would work in livecode, but not when tested on a connected android phone?

The phone is connected via usb.

It basically calls the API , converts the json response and puts it in a field.

I will upload the script etc... if needed, but just wondered if I was missing something obvious?

Kindest regards

Paul

Klaus
Posts: 11742
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: API call works on livecode, but not on android

Post by Klaus » Wed Aug 21, 2019 7:43 pm

Hi Paul,

what API are you talking about?


Best

Klaus

ClipArtGuy
Posts: 226
Joined: Wed Aug 19, 2015 4:29 pm

Re: API call works on livecode, but not on android

Post by ClipArtGuy » Wed Aug 21, 2019 10:12 pm

Do you have the internet permission selected in your standalone settings?

paulalsmith1000
Posts: 52
Joined: Sat Jun 15, 2019 10:09 am

Re: API call works on livecode, but not on android

Post by paulalsmith1000 » Thu Aug 22, 2019 6:57 am

Hi everyone

In answer to a few of the questions:-

I couldn't find an internet permissions bit in the standalone settings, but there is a command on the page that checks for a connection ( below) and this works fine.

command checkInternetConnection

put url "http://www.google.com" into tURL

if tURL is empty then
answer "no internet connection found, please try again later"
else
answer "testing internet connection........connection established"
end if

end checkInternetConnection

-----------------------------------------------------------------------------------------

The API is world tides.info see some of the script below. It seems to stop when it trys to run 'command json'.

Any help would be fab.

kind regards

Paul



command request_tide

put tmylat into tlat
put tmylong into tlon
put "https://www.worldtides.info/api?datum=Lat&extremes&lat=" & tlat & "&lon=" & tlon & "start=" & tepochtide & "&length=86400&maxcalls=5&key=" & WT_Key into ttidal_request
answer ttidal_request

end request_tide

command json
#put empty into the field "arraydata"
put URL ttidal_request into ttidal_response
put jsonimport(ttidal_response) into tArray


combine tArray by return and tab
put tArray into field "arraydata"

#put tArray[responseDatum] into field "arrayspecifics"
#put depthvariable into field "arrayspecifics"
end json



command json2
#put empty into the field "arrayspecifics"
put URL ttidal_request into ttidal_response
put jsonimport(ttidal_response) into tArray
put tArray[station] into station_id
put tArray[extremes] into tSubArray1
put tSubArray1[1] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"

else
put tSubArray1[2] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"
else
put tSubArray1[3] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"


else
put tSubArray1[4] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"


else
put "can't determine low tide " into the field "arrayspecifics"

end if
end if
end if
end if

#put tSubArray3 into tsub




end json2

Klaus
Posts: 11742
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: API call works on livecode, but not on android

Post by Klaus » Thu Aug 22, 2019 8:56 am

Hi Paul,

just to be sure, -> ttidal_request is declared as a local or global variable?


Best

Klaus

paulalsmith1000
Posts: 52
Joined: Sat Jun 15, 2019 10:09 am

Re: API call works on livecode, but not on android

Post by paulalsmith1000 » Thu Aug 22, 2019 11:40 am

Hi Klaus

Sorry I just realized it would be better to post the whole page script-

(the request tide command returns for example - https://www.worldtides.info/api?datum=L ... 996f0645d4)

Thanks v much ;)

local tlat
local tlon
local ttidal_request
local tlowtidelevel
local tlowtidetime
local tlowtidetime1
local tepochtide
local tmylat
local tmylong
global tDatabaseID
local station_id
local t_tidetime

constant WT_Key = "1449737d-0844-46a7-a344-9d996f0645d4"




on opencard

put empty into field "arraydata"
put empty into field "arrayspecifics"

databaseConnect


answer question "Please confirm that you have a wifi or 3G connection before continuing " with "Yes" or "No"

if it is "no" then

answer "please wait untill you have a signal, then log back into the app"
exit to top

else

checkInternetConnection
read_epochtime
read_latandlong
request_tide
json
json2
databaseInsert_tidelevel
databaseInsert_tidetime


end if


end opencard


command request_tide

put tmylat into tlat
put tmylong into tlon
put "https://www.worldtides.info/api?datum=Lat&extremes&lat=" & tlat & "&lon=" & tlon & "start=" & tepochtide & "&length=86400&maxcalls=5&key=" & WT_Key into ttidal_request
answer ttidal_request

end request_tide

command json
put URL (ttidal_request) into ttidal_response
#put URL "https://www.worldtides.info/api?datum=L ... 996f0645d4" into ttidal_response
if ttidal_response is not empty then
answer "api seems to contain response"
end if
put jsonimport(ttidal_response) into tArray
combine tArray by return and tab
put tArray into field "arraydata"
end json



command json2
#put empty into the field "arrayspecifics"
put URL ttidal_request into ttidal_response
put jsonimport(ttidal_response) into tArray
put tArray[station] into station_id
put tArray[extremes] into tSubArray1
put tSubArray1[1] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"

else
put tSubArray1[2] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"
else
put tSubArray1[3] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"


else
put tSubArray1[4] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"


else
put "can't determine low tide " into the field "arrayspecifics"

end if
end if
end if
end if

#put tSubArray3 into tsub



#combine tSubArray2 by return and tab
#put tSubArray3 into field "arrayspecifics"
#put tsub into field "arrayspecifics"


#combine tArray by return and tab
#put tArray into field "arraydata"

#put tArray[responseDatum] into field "arrayspecifics"
#put tArray[extremes] [dt] into field "arrayspecifics"
#put depthvariable into field "arrayspecifics"
end json2



command checkInternetConnection

put url "http://www.google.com" into tURL

if tURL is empty then
answer "no internet connection found, please try again later"
else
answer "testing internet connection........connection established"
end if

end checkInternetConnection


command read_epochtime
set the defaultFolder to specialFolderPath("documents")
put URL ("file:epochofsurvey.txt") into tepochtide

end read_epochtime

command read_latandlong
set the defaultFolder to specialFolderPath("documents")
put URL ("file:currentlat.txt") into tmylat
put URL ("file:currentlong.txt") into tmylong

end read_latandlong

command databaseConnect
if tDatabaseID is a number then
answer "the database connection was successful, your connection id is" && tDatabaseID
else
answer "Unable to connect to the database, no existing connection"
end if
end databaseConnect


on databaseInsert_tidelevel
put tlowtidelevel && "(LAT from tide station;" && station_id &")" into t_tidelevel
put "UPDATE survey_details SET lowtide_level = :1 WHERE rowid =1" into tSQL
revExecuteSQL tDatabaseID, tSQL, "t_tidelevel"
end databaseInsert_tidelevel


on databaseInsert_tidetime
put tlowtidetime1 && "(UTC from tide station;" && station_id &")" into t_tidetime
put "UPDATE survey_details SET lowtide_time = :1 WHERE rowid =1" into tSQL
revExecuteSQL tDatabaseID, tSQL, "t_tidetime"
end databaseInsert_tidetime


command test_save
set the defaultFolder to specialFolderPath("documents")
put t_tidetime into URL ("file:tide_test.txt")
end test_save

sphere
Posts: 1019
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: API call works on livecode, but not on android

Post by sphere » Thu Aug 22, 2019 12:07 pm

What do you get when you use:
Answer the result
after you send something to an url?

I would try to put your whole or partly url url in a variable then post it:

Code: Select all

Put "/?=extrastuff" into tThis
post tThis to URL "https://website.com"
answer the result
Now the result is empty or contains an error
Or the 'it' variable contains what you want or an error string from the website

Also i would start globals with g not a t, it may work but it's better to recoqnise when reading the script.

On the Android tab in the standalone setting, then in the right below corner is Internet Permission which you have to tick(maybe it was already ticked)

Maybe you need to set a http Header first when using the whole string as put URL "your whole websitestring with extra stuff" into tMydata, or something i don't know
Last edited by sphere on Thu Aug 22, 2019 1:47 pm, edited 2 times in total.
https://www.jj-s.nl
https://www.multifoodshop.nl
--------------------------------------
1 Check and search the LiveCode User Guide.pdf
2 Check and search the Dictionary
3 Check and search this forum
4 Ask on this forum
5 Ask on the List
6 Help others too

bogs
Posts: 4620
Joined: Sat Feb 25, 2017 10:45 pm

Re: API call works on livecode, but not on android

Post by bogs » Thu Aug 22, 2019 1:20 pm

Hey Paul,
I don't have any solution or advice for your problem per se like Klaus and sphere, but I do hope you will go back and edit your previous posts so that the code portions are put into code display mode, so that others (like me) who are reading the thread have it a tad easier.

Short runs of code like:
on mouseUp
answer "Hiya !"
end mouseUp

are easy to read inline, the long run multiple handler sets like this one above:

Code: Select all

local tlat
local tlon
local ttidal_request
local tlowtidelevel
local tlowtidetime
local tlowtidetime1
local tepochtide
local tmylat
local tmylong
global tDatabaseID
local station_id
local t_tidetime

constant WT_Key = "1449737d-0844-46a7-a344-9d996f0645d4"




on opencard

put empty into field "arraydata"
put empty into field "arrayspecifics"

databaseConnect


answer question "Please confirm that you have a wifi or 3G connection before continuing " with "Yes" or "No"

if it is "no" then

answer "please wait untill you have a signal, then log back into the app"
exit to top

else

checkInternetConnection
read_epochtime
read_latandlong
request_tide
json
json2
databaseInsert_tidelevel
databaseInsert_tidetime


end if


end opencard


command request_tide

put tmylat into tlat
put tmylong into tlon
put "https://www.worldtides.info/api?datum=Lat&extremes&lat=" & tlat & "&lon=" & tlon & "start=" & tepochtide & "&length=86400&maxcalls=5&key=" & WT_Key into ttidal_request
answer ttidal_request

end request_tide

command json
put URL (ttidal_request) into ttidal_response
#put URL "https://www.worldtides.info/api?datum=L ... 996f0645d4" into ttidal_response
if ttidal_response is not empty then
answer "api seems to contain response"
end if
put jsonimport(ttidal_response) into tArray
combine tArray by return and tab
put tArray into field "arraydata"
end json



command json2
#put empty into the field "arrayspecifics"
put URL ttidal_request into ttidal_response
put jsonimport(ttidal_response) into tArray
put tArray[station] into station_id
put tArray[extremes] into tSubArray1
put tSubArray1[1] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"

else
put tSubArray1[2] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"
else
put tSubArray1[3] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"


else
put tSubArray1[4] into tSubArray2
put tSubArray2["type"] into tlowtide
put tSubArray2["height"] into tlowtidelevel
put tSubArray2["date"] into tlowtidetime
if tlowtide is "Low" then
put tlowtidetime into t_time
set itemdel to "T"
put item 2 of t_time into tlowtidetime1
set itemdel to "+"
delete item 2 of tlowtidetime1
put "low tide-" & tlowtidelevel & "m," && "low tide time-" & tlowtidetime1 &"(UTC)," && "tidal station-" & station_id into the field "arrayspecifics"


else
put "can't determine low tide " into the field "arrayspecifics"

end if
end if
end if
end if

#put tSubArray3 into tsub



#combine tSubArray2 by return and tab
#put tSubArray3 into field "arrayspecifics"
#put tsub into field "arrayspecifics"


#combine tArray by return and tab
#put tArray into field "arraydata"

#put tArray[responseDatum] into field "arrayspecifics"
#put tArray[extremes] [dt] into field "arrayspecifics"
#put depthvariable into field "arrayspecifics"
end json2



command checkInternetConnection

put url "http://www.google.com" into tURL

if tURL is empty then
answer "no internet connection found, please try again later"
else
answer "testing internet connection........connection established"
end if

end checkInternetConnection


command read_epochtime
set the defaultFolder to specialFolderPath("documents")
put URL ("file:epochofsurvey.txt") into tepochtide

end read_epochtime

command read_latandlong
set the defaultFolder to specialFolderPath("documents")
put URL ("file:currentlat.txt") into tmylat
put URL ("file:currentlong.txt") into tmylong

end read_latandlong

command databaseConnect
if tDatabaseID is a number then
answer "the database connection was successful, your connection id is" && tDatabaseID
else
answer "Unable to connect to the database, no existing connection"
end if
end databaseConnect


on databaseInsert_tidelevel
put tlowtidelevel && "(LAT from tide station;" && station_id &")" into t_tidelevel
put "UPDATE survey_details SET lowtide_level = :1 WHERE rowid =1" into tSQL
revExecuteSQL tDatabaseID, tSQL, "t_tidelevel"
end databaseInsert_tidelevel


on databaseInsert_tidetime
put tlowtidetime1 && "(UTC from tide station;" && station_id &")" into t_tidetime
put "UPDATE survey_details SET lowtide_time = :1 WHERE rowid =1" into tSQL
revExecuteSQL tDatabaseID, tSQL, "t_tidetime"
end databaseInsert_tidetime


command test_save
set the defaultFolder to specialFolderPath("documents")
put t_tidetime into URL ("file:tide_test.txt")
end test_save
...are not only condensed in space, but far easier to copy and look at in an editor, or read in chunks. As a side benefit for readability even in the code box, the block is formatted in a way that easily simulates the original formatting of the editor as far as indentation goes, like so:

Code: Select all

on databaseInsert_tidetime
	put tlowtidetime1 && "(UTC from tide station;" && station_id &")" into t_tidetime
	put "UPDATE survey_details SET lowtide_time = :1 WHERE rowid =1" into tSQL
	revExecuteSQL tDatabaseID, tSQL, "t_tidetime"
end databaseInsert_tidetime
Just a suggestion.
Image

AndyP
Posts: 470
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: API call works on livecode, but not on android

Post by AndyP » Thu Aug 22, 2019 5:29 pm

Tried with the same results as you over usb

Then made these changes

In Standalone Application Settings - Android
Make sure Internet is checked in Application Permissions

In Standalone Application Settings - General
Check Select inclusions for the standalone

In Standalone Application Settings - Inclusions
check
JSON library
URL parser
Internet
Answer Dialogue
Ask Dialogue

I then got this result
Screenshot_20190822-181555.png
There seems to be a general problem with automatic inclusions missing out some functions.
Andy Piddock

http://2108.co.uk/tinyide/ - Mini LC IDE alternative

http://2108.co.uk/script-editor-themer/ - Script Editor colour theming

http://livecodeshare.runrev.com/stack/897/LiveCode-Multi-Search - Search across 8 LC resources

Post Reply

Return to “Android Deployment”