B4J Question HTTP Response Printing on Console

mbenam

Member
Hello all,

I am trying to write a console application. This connects to a website and brings back json. Response can be two type. If there is a valid json, it will send it. If there is none, it will send an xml with error. The following code is working fine if the response is a valid json. However, if the response is an error, it is printing the whole response even though I do not have a log command. Also, it seems to ignore the if statement that prints "Yes" if there is an error. Will appreciate any help regarding this.

Thanks.

B4A Console App Code:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub
Sub AppStart (Args() As String)
    DownloadQuote
    StartMessageLoop
End Sub

Sub DownloadQuote
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download("https://cdn.cboe.com/api/global/delayed_quotes/term_structure/2021/VIX_2021-07-31.json")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Dim s As String = j.GetString
    End If
    If s.Contains("<Error>") Then
        Log("Yes")
    End If

    j.Release
    ExitApplication
End Sub
 

mbenam

Member
Answering my own question. It seems that if the response status code is 403, the response will be printed any way. I did not find any way to suppress it. But I modified the code to read the status code and branch out to subsequent code based on response status 403 or 200. Also if the status code is 200 then j.success will be true and j.getstring will work. if the code is 403, j.getstring throws error Here is the code.

B4X:
Sub Process_Globals
    
End Sub
Sub AppStart (Args() As String)
    DownloadQuote
    StartMessageLoop
End Sub
Sub DownloadQuote
    Dim j As HttpJob
    Dim parser As JSONParser
    j.Initialize("", Me)
    j.Download("https://cdn.cboe.com/api/global/delayed_quotes/term_structure/2021/VIX_2021-07-30.json")
    Wait For (j) JobDone(j As HttpJob)
    
    If j.Response.StatusCode = "200" Then
        parser.Initialize(j.GetString)
    Else
        Return 'status code is 403
    End If


    j.Release
    ExitApplication
End Sub
 
Upvote 0
Top