Android Question [SOLVED] Getting the JSON response from an API web request

adrianstanescu85

Active Member
Licensed User
Longtime User
Hello

I tried to obtain the JSON contents that is returned when accessing the results of this MapQuest service. To do it on the web, I write this on a browser

http://open.mapquestapi.com/directi...}},{latLng:{lat:53.9593817,lng:-1.0814175}}]}

and it outputs the JSON format with the info.

How can I capture that output so that I can parse it with JSON?

I read the HttpUtils2 and JSON tutorials but so far haven't made any progress. I still cannot get the actual JSON string at all!

Thank you!
Adrian
 

JTmartins

Active Member
Licensed User
Longtime User
This should give you the Json String in your link. Note that you have to select the httputils2 library and Json library from the libs tab.

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
 
    Dim job1 As HttpJob
    job1.Initialize("Job1",Me)
    job1.PostString("http://open.mapquestapi.com/directions/v2/route","key=Fmjtd%7Cluurnuu1ng%2Cra%3Do5-9wraqf&json={locations:[{latLng:{lat:54.0484068,lng:-2.7990345}},{latLng:{lat:53.9593817,lng:-1.0814175}}]}")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub JobDone (Job As HttpJob)
   If Job.Success = True Then
      Select Job.JobName
         Case "Job1"
            Log(Job.GetString) ' CHECK THE LOG (THE STRING SHOULD BE THERE)
            Dim parser As JSONParser
            parser.Initialize(Job.GetString)
            ' HERE YOU PARSE THE JSON STRING
        End Select
    Else
    Log ("coms failure !!")
    End If
End Sub

hope it helps you to get started.
José
 
Upvote 0
Top