Android Question [SOLVED] Newbie JSON

makis_best

Well-Known Member
Licensed User
Longtime User
Hi

I try to learn how the JSON works but I can't pass over this point.
So I use a simple API from ipgeolocation.io to retrieve geolocation

The JSON Look like
B4X:
{"ip":"8.8.8.8","continent_code":"NA","continent_name":"North America","country_code2":"US",
"country_code3":"USA","country_name":"United States","country_capital":"Washington, D.C.","state_prov":"Louisiana",
"district":"","city":"Monroe","zipcode":"71203-2041","latitude":"32.58997","longitude":"-92.06862","is_eu":false,"calling_code":"+1"
,"country_tld":".us","languages":"en-US,es-US,haw,fr","country_flag":"https://ipgeolocation.io/static/flags/us_64.png",
"geoname_id":"4343811","isp":"Level 3 Parent, LLC","connection_type":"","organization":"Level 3 Parent, LLC",
"currency":{"code":"USD","name":"US Dollar","symbol":"$"},
"time_zone":{"name":"America/Chicago","offset":-6,"current_time":"2022-03-07 05:38:42.207-0600","current_time_unix":1646653122.207,"is_dst":false,"dst_savings":1}}

And my code is

B4X:
Dim parser As JSONParser
    parser.Initialize($"https://api.ipgeolocation.io/ipgeo?apiKey=xxxxxx&ip=${EditText1.Text}"$)
    Dim TheRoot As Map = parser.NextObject
    Dim continent_name As String = TheRoot.Get("continent_name")
    Dim country_tld As String = TheRoot.Get("country_tld")
    Dim calling_code As String = TheRoot.Get("calling_code")
    Dim languages As String = TheRoot.Get("languages")
    Dim connection_type As String = TheRoot.Get("connection_type")
    Dim city As String = TheRoot.Get("city")
    Dim ip As String = TheRoot.Get("ip")
    Dim latitude As String = TheRoot.Get("latitude")
    Dim isp As String = TheRoot.Get("isp")
    Dim continent_code As String = TheRoot.Get("continent_code")
    Dim time_zone As Map = TheRoot.Get("time_zone")
    Dim offset As Int = time_zone.Get("offset")
    Dim dst_savings As Int = time_zone.Get("dst_savings")
    Dim thename As String = time_zone.Get("name")
    Dim current_time_unix As Double = time_zone.Get("current_time_unix")
    Dim current_time As String = time_zone.Get("current_time")
    Dim is_dst As String = time_zone.Get("is_dst")
    Dim geoname_id As String = TheRoot.Get("geoname_id")
    Dim zipcode As String = TheRoot.Get("zipcode")
    Dim district As String = TheRoot.Get("district")
    Dim organization As String = TheRoot.Get("organization")
    Dim country_code2 As String = TheRoot.Get("country_code2")
    Dim country_name As String = TheRoot.Get("country_name")
    Dim is_eu As String = TheRoot.Get("is_eu")
    Dim country_code3 As String = TheRoot.Get("country_code3")
    Dim state_prov As String = TheRoot.Get("state_prov")
    Dim currency As Map = TheRoot.Get("currency")
    Dim symbol As String = currency.Get("symbol")
    Dim code As String = currency.Get("code")
    Dim name As String = currency.Get("name")
    Dim country_capital As String = TheRoot.Get("country_capital")
    Dim country_flag As String = TheRoot.Get("country_flag")
    Dim longitude As String = TheRoot.Get("longitude")

When I execute my app I get the error like

B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
** Activity (main) Resume **
Error occurred on line: 32 (B4XMainPage)
java.lang.RuntimeException: JSON Object expected.
    at anywheresoftware.b4a.objects.collections.JSONParser.NextObject(JSONParser.java:67)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
    at android.view.View.performClick(View.java:7171)
    at android.view.View.performClickInternal(View.java:7148)
    at android.view.View.access$3500(View.java:802)
    at android.view.View$PerformClick.run(View.java:27409)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7626)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
** Activity (main) Pause event (activity is not paused). **

Something is wrong with the map return from string?
I am wrong?
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
And my code is
Not when this error raises. Based on the error you where uing NextArray and not NextObject

BTW you can not use the jsonparser like this!

You first need to use okhttputils2 to download it, put the result into a string and give this string to the parser.
 
Upvote 1
Top