Android Question Help with JSON API call

spv4u

Member
Licensed User
Longtime User
Hi all I'm struggling with getting some code to request and parse a JSON even after reading lots of examples.

I have the following variables set

Dim dataURL As String = "https://hostingserver.com" (unable to publish correct url publically)
Dim BranchID As String = "22"

I need to send the URL the required branch ID as part of the request which should look like {"branchid": "22"}

I should then get back a JSON to parse in the following structure

Response
JSon{
BranchId
Order: [
{
SaleOrder ID
CustomerName
VehicleReg
CollectionBay
Status
},
{
SaleOrder ID
CustomerName
VehicleReg
CollectionBay
Status
}
]
inStoreCollection: [
{
SaleOrder ID
VehicleReg
CollectionBay
Status
},
{
SaleOrder ID
VehicleReg
CollectionBay
Status
}
]

Is anyone able to help me create the code so that I can call this from my Sub to request and parse the data so that I can then update the labels with the data returned

Thanks
 

eps

Expert
Licensed User
Longtime User
Upvote 0

spv4u

Member
Licensed User
Longtime User
Thanks

Perhaps my first part is to get hold of the JSON from the API

I have the following code
Dim job1 As HttpJob
job1.Initialize("Job1",Me)
job1.Download2(dataURL, Array As String ("branchid : 22"))

But the request gives and error, how do I hit the dataURL with just branched : 22 so that the server can return the JSON that I can then parse, using the above throws a java error in the debug
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
I would suggest the following:

B4X:
job1.Download2(dataURL, Array As String ({"branchid" : 22}))
 
Upvote 0

spv4u

Member
Licensed User
Longtime User
Thanks but there is a syntax error on that, documentation states the branch ID has to be passed like {"branchid": "22"}
 
Upvote 0

jimmyF

Active Member
Licensed User
Longtime User
So your branchid is a String and not an Integer.
Makes sense.

Does it work now?
 
Upvote 0

spv4u

Member
Licensed User
Longtime User
There is noting to grab as i'm not able to pass in the required branchid so no data is returned.

Apparently to get the data I need to submit the Jason data of {"branchid": "22"} in the body of the request, if that helps
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
then job.download is not correct. download is a GET request.

But it sounds you need to use POST instead of GET...
try
B4X:
job1.poststring(dataURL, $"{"branchid" : 22}"$))
'or
job1.poststring(dataURL, $"{"branchid" : "22"}"$))
Probably you need to set specific header(s) too
 
Upvote 0

spv4u

Member
Licensed User
Longtime User
I think you are right about the headers, any ideas??

The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim job2 As HttpJob

job2.Initialize("job2",Me) ' Create Person Group
job2.PostString("https://api.projectoxford.ai/face/v0/persongroups/1",JSONstring)
job2.GetRequest.SetHeader("Content-Type","application/json") 
job2.GetRequest.SetHeader("key","xxxxxxxxxxxxxxxxxxxxxxxxxxx")
Note to set the headers AFTER the Poststring line
 
Upvote 0

spv4u

Member
Licensed User
Longtime User
Thanks

I'm now getting the JSON return using code

B4X:
Dim job2 As HttpJob
    job2.Initialize("job2",Me) ' Create Person Group
    job2.PostString(dataURL,$"{"branchid" : "$ & BranchID & $"}"$)   
    job2.GetRequest.SetHeader("User-Agent","Mozilla/5.0")

I've put the returned code into the generator so I just need to use this and add the output to the text labels on the screen
 
Upvote 0

spv4u

Member
Licensed User
Longtime User
OK so now I have another strange one

using this code
B4X:
    Dim getCustOrders As HttpJob
    getCustOrders.Initialize("getCustOrders",Me)
    getCustOrders.PostString(dataURL,$"{"branchid" : "$ & BranchID & $"}"$)
    getCustOrders.GetRequest.SetHeader("User-Agent","Mozilla/5.0")
    getCustOrders.Release

I get the JSON returned from the remote server fine, I then have a timer set for 10 seconds and then loop through exactly the same code but the remote server just returns {} instead of the JSON

Any ideas if I need to clear down anything?

This code is currently in it's own Sub if that helps

Thanks
 
Upvote 0

spv4u

Member
Licensed User
Longtime User
Are you DIM a new job each time? If the dim is inside the sub then all should be ok

Yes I believe so and release moved now

B4X:
'################## GET DATA SECTION ########################################
Sub Get_List
    'Call to get JSON file
    Dim getCustOrders As HttpJob
    getCustOrders.Initialize("getCustOrders",Me)
    getCustOrders.PostString(dataURL,$"{"branchid" : "${BranchID}"}"$)
    getCustOrders.GetRequest.SetHeader("User-Agent","Mozilla/5.0")
   
       
End Sub

Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
      Select Job.JobName
         Case "getCustOrders"
            Log(Job.GetString) ' CHECK THE LOG 
            Dim parser As JSONParser
            parser.Initialize(Job.GetString)
            ' PARSE THE JSON STRING
            Job.Release
        End Select
    Else
    Log ("comms failure !!")
    End If
End Sub
 
Upvote 0

spv4u

Member
Licensed User
Longtime User
Thanks that is moved but I still get the {} after the first response.

** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Timer Set 1506097792068
** Service (httputils2service) Create **
** Service (httputils2service) Start **
{"branchId":"22".......... removed but is correct json data string
Timer Called 1506097800098
Timer Set 1506097800099
{}
Timer Called 1506097808133
Timer Set 1506097808136
{}
Timer Called 1506097816166
Timer Set 1506097816168
{}
** Activity (main) Pause, UserClosed = true **
** Activity (main) Resume **


Is there a way to kill/stop the httputils2service to see it that is doing something strange

Thanks
 
Upvote 0
Top