Android Question Getting http content and put into a variable.

Franck Danard

Member
Licensed User
Longtime User
Hi all.

I try to find a way to get HTTP content and put the result into a variable.
I enabled http / httputils2 libraries and trying to read some example but no way.
The result to get will be only a simple JSON format.

Can you give me a hand for that?

Thanks
 

KMatle

Expert
Licensed User
Longtime User
There are tons of examples here. If you search for "httputils" the first result is a good example to get contents from an URL (server) via GET/POST methods.

Can you give us some details about the URL you want to call (exactly: Is it a php script or something else? What input does this script expect? Did you write that script on your own?). You can take a look at my examples in my signature to get an idea what to do.
 
Upvote 0

Grant Fullen

Member
Licensed User
You'll end up using:

B4X:
Dim parser As JSONParser
parser.Initialize(Job.GetString)
Dim root As Map = parser.NextObject

Log your Job.GetString to see if you're receiving the correct json response as well. A little searching homework on the forums should fill in the rest.
 
Upvote 0

Franck Danard

Member
Licensed User
Longtime User
More specifically do a search on the forums for using the JSONParser and doing a PostString with HttpJob.
Yep, i saw something about json parser. But at first, i need to get some data to parse them.
Once the content from http request in my variable, i could parse it sure.
 
Upvote 0

Franck Danard

Member
Licensed User
Longtime User
You'll end up using:

B4X:
Dim parser As JSONParser
parser.Initialize(Job.GetString)
Dim root As Map = parser.NextObject

Log your Job.GetString to see if you're receiving the correct json response as well. A little searching homework on the forums should fill in the rest.

Yep correct, i saw this kind of stuff somewhere in the forum.
Thanks a lots to put this part here. ;)
 
Upvote 0

Grant Fullen

Member
Licensed User
The content from your http request goes in Job.GetString.

B4X:
Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "job name here"
                'print the result to the logs
                Log(Job.GetString)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        Starter.xui.MsgboxAsync(Job.ErrorMessage, "Error")
    End If
    Job.Release
End Sub
 
Upvote 0

Franck Danard

Member
Licensed User
Longtime User
Ho, it was missing job.GetString for me as information.
I'll try this tomorrow morning.
My workstation is closed now. ;)

Thanks guys, i'll let you know anyway.
It's nice of you.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
I'll try to make this as simple as possible to follow. All you need is the OkHttpUtils2 and JSON libraries selected in your libraries tab.

Create a Global String variable, lets call it JSONResult.

Use the following code to get your JSON feed, just replace the URL with your own.
B4X:
'Test URL
     Dim j As HttpJob
          j.Initialize(Null, Me)
          j.Download("http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22")
     Wait For (j) JobDone(j As HttpJob)

     If j.Success Then
          JSONResult = j.GetString     'Store the contents of j.GetString into JSONResult.
     Else
          Log($"Error: ${j.ErrorMessage}"$)
     End If

     j.Release

Copy the JSON j.GetString results and paste them into the following webpage page created by Erel, then click the Parse button.

http://basic4ppc.com:51042/json/index.html

The webpage that Erel created automatically generates the B4A code for you, all you have to do is copy and paste the code into your B4A project and manipulate the code.

For the above example URL, the JSON Tree code generator created the following code.
B4X:
'Code generated by the JSON Forum page, thank you Erel.

     Dim parser As JSONParser
          parser.Initialize(JSONResult)

     Dim root As Map = parser.NextObject
     Dim dt As Int = root.Get("dt")
     Dim coord As Map = root.Get("coord")
     Dim lon As Double = coord.Get("lon")
     Dim lat As Double = coord.Get("lat")
     Dim visibility As Int = root.Get("visibility")
     Dim weather As List = root.Get("weather")

     For Each colweather As Map In weather
          Dim icon As String = colweather.Get("icon")
          Dim description As String = colweather.Get("description")
          Dim main As String = colweather.Get("main")
          Dim id As Int = colweather.Get("id")
     Next

     Dim name As String = root.Get("name")
     Dim cod As Int = root.Get("cod")
     Dim main As Map = root.Get("main")
     Dim temp As Double = main.Get("temp")
     Dim temp_min As Double = main.Get("temp_min")
     Dim humidity As Int = main.Get("humidity")
     Dim pressure As Int = main.Get("pressure")
     Dim temp_max As Double = main.Get("temp_max")
     Dim clouds As Map = root.Get("clouds")
     Dim all As Int = clouds.Get("all")
     Dim id As Int = root.Get("id")
     Dim sys As Map = root.Get("sys")
     Dim country As String = sys.Get("country")
     Dim sunrise As Int = sys.Get("sunrise")
     Dim sunset As Int = sys.Get("sunset")
     Dim id As Int = sys.Get("id")
     Dim type As Int = sys.Get("type")
     Dim message As Double = sys.Get("message")
     Dim base As String = root.Get("base")
     Dim wind As Map = root.Get("wind")
     Dim deg As Int = wind.Get("deg")
     Dim speed As Double = wind.Get("speed")

Basically no complicated coding needed, everything you need to know is in this post ;)

Enjoy...
 
Last edited:
Upvote 0
Top