Android Question Retrieve simple value from web Server

CrunkFX

Member
Licensed User
Hey there,

I'm struggeling with a very, kinda too simple Task. How should i retrieve a value from an external source?

I have a Button whitch needs to be clicked in Order for the value to bei shown. The value is a simple String. Should i Just Put that String in a static html file hosted in a server, or using mqtt or even firebase as database?

How do you get some simple value from another source?

The value will be an coupon code generated by another Software. But what ist the best way to get the Coupon to the App?

Maybe you already have some experience with this Task.

Kind regards
CrunkFX
 
Solution
I think you can have a text file in your web server and in your B4A download the text using OkHttpUtils2.
B4X:
job.Download("https://mywebsite.com/coupon.txt")

A slightly-more comprehensive example:

B4X:
Sub DownloadString(URL As String) As ResumableSub

    Log("Downloading from >>>" & URL & "<<<")

    Dim S As String = ""
 
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(URL)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        S = j.GetString
    End If
    j.Release

    Log("Downloaded " & S.Length &" Chars = >>>" & S & "<<<")

    Return S
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
 
    'get link from Google Sheets, File, Share, Share with others, Anyone with the link, Viewer...

CrunkFX

Member
Licensed User
I think you can have a text file in your web server and in your B4A download the text using OkHttpUtils2.
B4X:
job.Download("https://mywebsite.com/coupon.txt")

But I prefer to use PHP echo.
But with PHP Echo it will be the same as a simple html file which ist read right?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
But with PHP Echo it will be the same as a simple html file which ist read right?
I think so.
I usually don’t read a simple value. I will think that my system will have flexibility for more complicated values. If your case is just a simple value, then there are many ways to do it. Text file is just one of the ways.
 
Upvote 0

emexes

Expert
Licensed User
I think you can have a text file in your web server and in your B4A download the text using OkHttpUtils2.
B4X:
job.Download("https://mywebsite.com/coupon.txt")

A slightly-more comprehensive example:

B4X:
Sub DownloadString(URL As String) As ResumableSub

    Log("Downloading from >>>" & URL & "<<<")

    Dim S As String = ""
 
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(URL)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        S = j.GetString
    End If
    j.Release

    Log("Downloaded " & S.Length &" Chars = >>>" & S & "<<<")

    Return S
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
 
    'get link from Google Sheets, File, Share, Share with others, Anyone with the link, Viewer, Copy
    Dim URL As String = "https://docs.google.com/spreadsheets/d/1kwo_h0lH4ezu4ImPHdbkoxS4kz63n8qAlp__qlB93PA/edit?usp=sharing"
 
    URL = URL.Replace("/edit?usp=sharing", "/export?format=tsv")    'change from edit to export tsv (NOT csv, which adds quotes we don't be needing)

    wait for (DownloadString(URL)) complete (DownloadedCoupon As String)

    DownloadedCoupon = DownloadedCoupon.Replace("""", "")    'just in case you decided to use csv anyway
 
    '''Dim DownloadedCoupon As String = "{thisisa=demo, number=271828, expiry=1668653179748, centsperlitre=4}"

    If DownloadedCoupon.Length <> 0 Then
        Dim JSON As JSONParser
        JSON.Initialize(DownloadedCoupon)
        Dim CouponMap As Map = JSON.NextObject

        Log("DownloadedCoupon = """ & DownloadedCoupon & """")
        Log("CouponMap = " & CouponMap)
        Log("Coupon number = " & CouponMap.Get("number"))
        Log("Coupon expiry = " & DateTime.Date(CouponMap.Get("expiry")))
    
        Dim DaysToGo As Int = (CouponMap.Get("expiry") - DateTime.Now) / DateTime.TicksPerDay
        Log(DaysToGo & " days until coupon expires")
    Else
        Log("Could not download coupon... early bird catchs the worm 🍻")
    End If
 
End Sub

Log output:
Logger connected to:  HMD Global Nokia C01 Plus
--------- beginning of system
--------- beginning of main
Downloading from >>>https://docs.google.com/spreadsheets/d/1kwo_h0lH4ezu4ImPHdbkoxS4kz63n8qAlp__qlB93PA/export?format=tsv<<<
Downloaded 70 Chars = >>>{thisisa=coupon, number=314159, expiry=1668653179748, centsperlitre=4}<<<
DownloadedCoupon = >>>{thisisa=coupon, number=314159, expiry=1668653179748, centsperlitre=4}<<<
CouponMap = (MyMap) {thisisa=coupon, number=314159, expiry=1668653179748, centsperlitre=4}
Coupon number = 314159
Coupon expiry = 11/17/2022
29 days until coupon expires


The coupon text / number / code / JSON is in a shared read-only Google Sheets spreadsheet eg:

1666067751783.png


but if you want to store it on your own website, go for it. 🍻
 
Last edited:
Upvote 1
Solution

CrunkFX

Member
Licensed User
A slightly-more comprehensive example:

B4X:
Sub DownloadString(URL As String) As ResumableSub

    Log("Downloading from >>>" & URL & "<<<")

    Dim S As String = ""
 
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(URL)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        S = j.GetString
    End If
    j.Release

    Log("Downloaded " & S.Length &" Chars = >>>" & S & "<<<")

    Return S
 
End Sub

Sub Activity_Create(FirstTime As Boolean)
 
    'get link from Google Sheets, File, Share, Share with others, Anyone with the link, Viewer, Copy
    Dim URL As String = "https://docs.google.com/spreadsheets/d/1kwo_h0lH4ezu4ImPHdbkoxS4kz63n8qAlp__qlB93PA/edit?usp=sharing"
 
    URL = URL.Replace("/edit?usp=sharing", "/export?format=tsv")    'change from edit to export tsv (NOT csv, which adds quotes we don't be needing)

    wait for (DownloadString(URL)) complete (DownloadedCoupon As String)

    DownloadedCoupon = DownloadedCoupon.Replace("""", "")    'just in case you decided to use csv anyway
 
    '''Dim DownloadedCoupon As String = "{thisisa=demo, number=271828, expiry=1668653179748, centsperlitre=4}"

    If DownloadedCoupon.Length <> 0 Then
        Dim JSON As JSONParser
        JSON.Initialize(DownloadedCoupon)
        Dim CouponMap As Map = JSON.NextObject

        Log("DownloadedCoupon = """ & DownloadedCoupon & """")
        Log("CouponMap = " & CouponMap)
        Log("Coupon number = " & CouponMap.Get("number"))
        Log("Coupon expiry = " & DateTime.Date(CouponMap.Get("expiry")))
   
        Dim DaysToGo As Int = (CouponMap.Get("expiry") - DateTime.Now) / DateTime.TicksPerDay
        Log(DaysToGo & " days until coupon expires")
    Else
        Log("Could not download coupon... early bird catchs the worm 🍻")
    End If
 
End Sub

Log output:
Logger connected to:  HMD Global Nokia C01 Plus
--------- beginning of system
--------- beginning of main
Downloading from >>>https://docs.google.com/spreadsheets/d/1kwo_h0lH4ezu4ImPHdbkoxS4kz63n8qAlp__qlB93PA/export?format=tsv<<<
Downloaded 70 Chars = >>>{thisisa=coupon, number=314159, expiry=1668653179748, centsperlitre=4}<<<
DownloadedCoupon = >>>{thisisa=coupon, number=314159, expiry=1668653179748, centsperlitre=4}<<<
CouponMap = (MyMap) {thisisa=coupon, number=314159, expiry=1668653179748, centsperlitre=4}
Coupon number = 314159
Coupon expiry = 11/17/2022
29 days until coupon expires


The coupon text / number / code / JSON is in a shared read-only Google Sheets spreadsheet eg:

View attachment 134940

but if you want to store it on your own website, go for it. 🍻
Thanks for that, didn't even think about JSON. I kinda Like the Idea with Sheets and will definitely give it a try.
 
Upvote 0
Top