B4J Question [SOLVED] New at Json Post

makis_best

Well-Known Member
Licensed User
Longtime User
Hello

I am new at JSON and I want to start using them.
I have this one from manual.

Can someone point me to the correct direction how to do or one example?
Thank you

manual:
Login(Post Method)

It is used to Login the web account defined by urlName in request URL. It returns a temporary access token
(clientID) that can be later used in the “authenticate” method. It also returns the available set of login permissions
for the User.

Request URL
http://urlName.com/login

Request Sample
{
"service": "login",
"username": "demo",
"password": "demo",
"appId": "8002"
}

Request Parameters
Property Type Description
service string Defines the name of the method that will be executed. Current method is login.
username string User name (Code) defined in Web Accounts
password string Password defined in Web Accounts
appId string ID of the Service defined in Web Accounts

Response Sample
{
"success": true,
"clientID": "9J8pJ6LGa5mHG",
"objs": [ {
"COMPANY": "demo company",
"COMPANYNAME": "Standard Company",
"BRANCH": "9999",
"BRANCHNAME": "Head Offices",
"MODULE": "0",
"MODULENAME": "<Blank>",
"REFID": "123",
"REFIDNAME": "demo",
"USERID": "500",
"FINALDATE": "1899-12-30 00:00:00",
"ROLES": ""
}],
"ver": "4.00.514.10630",
"sn": "01234567890123"
}
 
Last edited:

mcqueccu

Well-Known Member
Licensed User
Longtime User
Process the response here https://b4x.com:51041/json/index.html
Depends on Json library

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim jRoot As Map = parser.NextObject
Dim ver As String = jRoot.Get("ver")
Dim clientID As String = jRoot.Get("clientID")
Dim success As String = jRoot.Get("success")
Dim sn As String = jRoot.Get("sn")
Dim objs As List = jRoot.Get("objs")
For Each colobjs As Map In objs
 Dim BRANCH As String = colobjs.Get("BRANCH")
 Dim MODULENAME As String = colobjs.Get("MODULENAME")
 Dim BRANCHNAME As String = colobjs.Get("BRANCHNAME")
 Dim MODULE As String = colobjs.Get("MODULE")
 Dim ROLES As String = colobjs.Get("ROLES")
 Dim USERID As String = colobjs.Get("USERID")
 Dim FINALDATE As String = colobjs.Get("FINALDATE")
 Dim COMPANYNAME As String = colobjs.Get("COMPANYNAME")
 Dim REFIDNAME As String = colobjs.Get("REFIDNAME")
 Dim REFID As String = colobjs.Get("REFID")
 Dim COMPANY As String = colobjs.Get("COMPANY")
Next
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
test
B4X:
    Dim Link As String = "http://urlName.com/login"
    
    Dim Body As Map = CreateMap( "service" : "login", "username" : "demo", "password" : "demo", "appId" : "8002")
    Dim Parameters As String = Body.As(JSON).ToString
    Log(Parameters)
    
    Wait For (PostURL(Link, Parameters)) Complete (DataResult As String)
    Log(DataResult)

    If DataResult.Length = 0 Then Return
    
    Dim mRoot As Map = DataResult.As(JSON).ToMap
    Dim ver As String = mRoot.Get("ver")
    Dim clientID As String = mRoot.Get("clientID")
    Dim success As String = mRoot.Get("success")
    Dim sn As String = mRoot.Get("sn")
    Dim objs As List = mRoot.Get("objs")
    For Each colobjs As Map In objs
        Dim BRANCH As String = colobjs.Get("BRANCH")
        Dim MODULENAME As String = colobjs.Get("MODULENAME")
        Dim BRANCHNAME As String = colobjs.Get("BRANCHNAME")
        Dim MODULE As String = colobjs.Get("MODULE")
        Dim ROLES As String = colobjs.Get("ROLES")
        Dim USERID As String = colobjs.Get("USERID")
        Dim FINALDATE As String = colobjs.Get("FINALDATE")
        Dim COMPANYNAME As String = colobjs.Get("COMPANYNAME")
        Dim REFIDNAME As String = colobjs.Get("REFIDNAME")
        Dim REFID As String = colobjs.Get("REFID")
        Dim COMPANY As String = colobjs.Get("COMPANY")
    Next
B4X:
Public Sub PostURL(URL As String, Parameters As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString (URL, Parameters)
        j.GetRequest.SetContentType("application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        End If
    Catch
        Log(LastException)
    End Try
    j.Release
    Return Result
End Sub
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
Thank you @TILogistic your code was very helpful to me
Now my code look like that but I keep getting error.
I will like to have your opinion or any improvement.
Login Code:
Dim Link As String = "https://demo.ondemo.com/login"
        Dim j As HttpJob
        j.Initialize("", Me)
        
        Dim Body As Map = CreateMap( "service" : "login", "username" : "demo", "password" : "demo", "appId" : "8002")
        Dim Parameters As String = Body.As(JSON).ToString
        Log(Parameters)
        
        j.PatchString(Link, Parameters)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Dim dataresult As String = j.GetString
'            Wait For (j.PatchString(Link, Parameters)) Complete (DataResult As String)
            Log("Dataresult: " & dataresult & " - Length: " & dataresult.Length)
            If dataresult.Length = 0 Then Return
    
            Dim mRoot As Map = dataresult.As(JSON).ToMap
            Dim sss As String = mRoot.Get("success")
            If  sss = "false" Then
                Log("False")
                Return
            End If
            Dim ver As String = mRoot.Get("ver")
            Dim clientID As String = mRoot.Get("clientID")
            Dim success As String = mRoot.Get("success")
            Dim sn As String = mRoot.Get("sn")
            Dim objs As List = mRoot.Get("objs")
            For Each colobjs As Map In objs
                Dim BRANCH As String = colobjs.Get("BRANCH")
                Dim MODULENAME As String = colobjs.Get("MODULENAME")
                Dim BRANCHNAME As String = colobjs.Get("BRANCHNAME")
                Dim MODULE As String = colobjs.Get("MODULE")
                Dim ROLES As String = colobjs.Get("ROLES")
                Dim USERID As String = colobjs.Get("USERID")
                Dim FINALDATE As String = colobjs.Get("FINALDATE")
                Dim COMPANYNAME As String = colobjs.Get("COMPANYNAME")
                Dim REFIDNAME As String = colobjs.Get("REFIDNAME")
                Dim REFID As String = colobjs.Get("REFID")
                Dim COMPANY As String = colobjs.Get("COMPANY")
            Next
            Log("BRANCH: " & BRANCH)
            Log("MODULENAME: " & MODULENAME)

        End If
        
        j.Release
    Catch
        Log(LastException)
    End Try

Error code says wrong credentials but when I try them on website login page then working just fine.

Error:
Dataresult: {"success":false, "errorcode":-1, "error":"Login fails due to invalid login credentials."}
 
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
You should use job.PostString
Thank you @aeric now login done correctly but another problem show up.
All string return back like
B4X:
BRANCH: 1000
MODULENAME: ��������
BRANCHNAME: ��������

On manual says something about encoding 1032. That's the reason?
How I can do that?
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
the "?" inside a diamond means the wrong charater set is being used to print out the result. android is assuming UTF-8.

code page 1032 is an 8-bit character set (similar to ocr-a). it handles capital letters, digits and some special symbols
used for financial transactions. it recognizes the standard ascii character set, so ISO-8859-1 would be a good place to
start. so long as you don't try to print the special characters recognized by the code page, you shouldn't need a special
font.

don't ask for any special treatment from the server. the response should come back as a string of bytes. take the bytes
and convert to ascii (ISO-8859-1) with the byteconverter library, eg:

B4X:
Dim bc As ByteConverter
Dim b() As Byte = Bit.InputStreamToBytes(j.getinputstream)
dim output as string = bc.StringFromBytes(b, "ISO-8859-1")

what, exactly, is being said regarding 1032? it normally involve printing the bottom line on cheques/checks with a special machine-readable font.
 
Last edited:
Upvote 0

makis_best

Well-Known Member
Licensed User
Longtime User
the "?" instead a diamond means the wrong charater set is being used to print out the result. android is assuming UTF-8.

code page 1032 is an 8-bit character set (similar to ocr-a). it handles capital letters, digits and some special symbols
used for financial transactions. it recognizes the standard ascii character set, so ISO-8859-1 would be a good place to
start. so long as you don't try to print the special characters recognized by the code page, you shouldn't need a special
font.

don't ask for any special treatment from the server. the response should come back as a string of bytes. take the bytes
and convert to ascii (ISO-8859-1) with the byteconverter library, eg:

B4X:
Dim bc As ByteConverter
Dim b() As Byte = Bit.InputStreamToBytes(j.getinputstream)
dim output as string = bc.StringFromBytes(b, "ISO-8859-1")

what, exactly, is being said regarding 1032? it normally involve printing the bottom line on cheques/checks with a special machine-readable font.
Dosen't say anything much more. Just that encoding need to be in 1032.
I try your way and changing from "ISO-8859-1" to "ISO-8859-7" fix the problem.

Thank you.
 
Upvote 0
Top