B4J Question B4J JSON response from PHP Script.

Mick96

Member
Licensed User
Hello All. I've been wrestling with an issue where I have a PHP script that does a query on a mySql database and echos the response back in JSON format.
The script works flawlessly. In Fact if you hit the URL from a browser you can see the JSON that comes back from the query. https://oblongapps.com/fetch.php
in Xcode I am able to capture that JSON coming back and get it into arrays and populate a UITableView. I'm trying to do the same thing in B4J but am stumped.
I've tried using job.getstring to no avail and I can't use job.download because that just wants to download the php file and ignores the JSON return.
Can anyone point me in the direction of a tutorial or example. My searches have turned up nothing. In fact If I could just figure out how to capture the JSON in a string variable I think I could take it from there.

Thanks so Much
 

aaronk

Well-Known Member
Licensed User
Longtime User
Works fine..

B4X:
Sub AppStart (Args() As String)
    Process
    StartMessageLoop
End Sub

Sub Process
    Dim job1 As HttpJob
    Dim URL As String = "https://oblongapps.com/fetch.php"
    job1.Initialize("JOB1", Me)
    job1.Download(URL)
       
    Wait for JobDone (Job As HttpJob)
    If Job.Success = True Then
    Log(Job.getstring)
        processJSON(Job.getstring)
    End If
End Sub

Sub processJSON(JSON As String)
    Dim parser As JSONParser
    parser.Initialize(JSON)
    Dim root As Map = parser.NextObject
    Dim slings As List = root.Get("slings")
    For Each colslings As Map In slings
        Dim color As String = colslings.Get("color")
        Dim serial As String = colslings.Get("serial")
        Dim is_out As String = colslings.Get("is_out")
        Dim length As String = colslings.Get("length")
        Dim ID As String = colslings.Get("ID")
        Dim TypeString As String = colslings.Get("type")
        Dim status As String = colslings.Get("status")
    Next
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
You don't need to use WebView.

B4X:
Private Sub Button1_Click
    Dim Job1 As HttpJob
    'WebView1.LoadUrl("http://oblongapps.com/fetch.php")
    Job1.Initialize("", Me)
    Job1.Download("https://oblongapps.com/fetch.php")
    Wait For (Job1) JobDone(Job1 As HttpJob)
    If Job1.Success Then
        Log(Job1.GetString)
        Log("Success")
    End If
    Job1.Release
End Sub

You can check this tutorial. It applies to B4J too.
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
Private Sub Button1_Click
    Dim BaseURL As String = "https://oblongapps.com/fetch.php"
    
    Wait For (APIGetURL(BaseURL)) Complete (DataResult As String)
    If DataResult = "" Then Return
'    Log(DataResult)
    
    Dim mRoot As Map = DataResult.As(JSON).ToMap
    Dim slings As List = mRoot.Get("slings")
    
    For Each colslings As Map In slings
        Dim ID As String = colslings.Get("ID")
'        Dim color As String = colslings.Get("color")
'        Dim serial As String = colslings.Get("serial")
'        Dim is_out As String = colslings.Get("is_out")
'        Dim length As String = colslings.Get("length")
'        Dim Type As String = colslings.Get("type")
'        Dim status As String = colslings.Get("status")
        Log(ID)
    Next
End Sub

Public Sub APIGetURL(URL As String) As ResumableSub
    Dim ResultURL As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.Download(URL)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            ResultURL = j.GetString
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
    Return ResultURL
End Sub

 
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
Sorry, it's my mistake. .This should be the correct line
B4X:
Job.Download(URL)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…