Android Question how to parse this data

PassionDEV

Active Member
Licensed User
i am using this api

http://ip-api.com/php/

using okhttp i grab its data like following

B4X:
Sub getipinfo(gip As String)
   
   
    Dim j As HttpJob
    Dim urllok As String
    Dim getdata As String
   

    urllok = "http://ip-api.com/php/"&gip
    j.Initialize("", Me)
    j.Download(urllok)

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
       
        getdata = j.GetString

    End If
    j.Release
   
    ' parse the out put of getdata
   
End Sub
i wanted to parse out this data like grabing country , region , etc.. which i couldn't do it sense the string is not static each time its change
 
Last edited:

Harris

Expert
Licensed User
i figure it out i used Map to identify the parsed value
Well, good on you...

So was "getdata" (returned value) a Map, string - or what?

We really have no idea what you figured out and hence - it helps no one else (except yourself - answering your own question).

Please be kind enough to post your resolve (in more detail) since we all took (our) time to examine your question, before you "figure it out".

Thanks
 

PassionDEV

Active Member
Licensed User
Well, good on you...

So was "getdata" (returned value) a Map, string - or what?

We really have no idea what you figured out and hence - it helps no one else (except yourself - answering your own question).

Please be kind enough to post your resolve (in more detail) since we all took (our) time to examine your question, before you "figure it out".

Thanks
i used jsonparse and map to parse this string

example

B4X:
Dim parsedip As String = ipdata'data from http job

    Dim parser As JSONParser
    parser.Initialize(parsedip)
  
    Dim mparse As Map = parser.NextObject
  
    Dim country As String = mparse.Get("country")

also the ip-api have a json Api instead of php

http://ip-api.com/json/
 

DonManfred

Expert
Licensed User
Serialized PHP endpoint

Deprecated
Use JSON. Almost all PHP sites now support json_decode(), and it's faster than unserialize()

You should use JSON instead of PHP. It will make things easier using the jsonparser.

http://ip-api.com/json/

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim zip As String = root.Get("zip")
Dim country As String = root.Get("country")
Dim city As String = root.Get("city")
Dim org As String = root.Get("org")
Dim timezone As String = root.Get("timezone")
Dim isp As String = root.Get("isp")
Dim query As String = root.Get("query")
Dim regionName As String = root.Get("regionName")
Dim lon As Double = root.Get("lon")
Dim as As String = root.Get("as")
Dim countryCode As String = root.Get("countryCode")
Dim region As String = root.Get("region")
Dim lat As Double = root.Get("lat")
Dim status As String = root.Get("status")
 

Harris

Expert
Licensed User

Harris

Expert
Licensed User
{"as":"AS852 TELUS Communications Inc.","city":"Kamloops","country":"Canada","countryCode":"CA","isp":"TELUS Communications Inc.","lat":50.5437,"lon":-120.0937,"org":"TELUS-HSIA-KMLPBC03","query":"75.156.105.155","region":"BC","regionName":"British Columbia","status":"success","timezone":"America/Vancouver","zip":"V2C"}

Result of : http://ip-api.com/json/


That's ME!
 

joaomoraes83

Member
Licensed User
Tanks Manfred!
in the present example I want to include the result obtained from the line: [Dim country As String = root.Get ("country")] in a label, how to do?
 

joaomoraes83

Member
Licensed User
Very Thanks Manfred!
One last question from beginner,
would the final code look like this?

B4X:
Sub getipinfo(gip As String)
  
  
    Dim j As HttpJob
    Dim urllok As String
    Dim getdata As String
  

    urllok = "http://ip-api.com/php/"&gip
    j.Initialize("", Me)
    j.Download(urllok)

    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim zip As String = root.Get("zip")
Dim country As String = root.Get("country")
Dim city As String = root.Get("city")
Dim org As String = root.Get("org")
Dim timezone As String = root.Get("timezone")
Dim isp As String = root.Get("isp")
Dim query As String = root.Get("query")
Dim regionName As String = root.Get("regionName")
Dim lon As Double = root.Get("lon")
Dim as As String = root.Get("as")
Dim countryCode As String = root.Get("countryCode")
Dim region As String = root.Get("region")
Dim lat As Double = root.Get("lat")
Dim status As String = root.Get("status")
    End If
    j.Release

End sub
 

Almora

Active Member
Licensed User
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
 
    Private Button1 As Button
    Private Label1 As Label
    Private Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("ipinfo")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

 

 


Sub ipinfo
 
    Dim GetAddressJob As HttpJob
    GetAddressJob.Initialize("GetAddress", Me)
 
    GetAddressJob.Download("http://ip-api.com/json/")
    Wait For (GetAddressJob) JobDone(GetAddressJob As HttpJob)
    If GetAddressJob.Success Then
        
        Dim parser As JSONParser
        parser.Initialize(GetAddressJob.GetString)
        Dim root As Map = parser.NextObject
        Dim zip As String = root.Get("zip")
        Dim country As String = root.Get("country")
        Dim city As String = root.Get("city")
        Dim lat As Double = root.Get("lat")
        Dim lon As Double = root.Get("lon")
      
            
        Log($"country: ${country}"$)
        Log($"city: ${city}"$)
        Log($"zip: ${zip}"$)
        Log($"lat: ${lat}"$)
        Log($"lon: ${lon}"$)
 
        Label1.Text=country
        Label2.Text=city
 
    End If
 
    GetAddressJob.Release
 
End Sub




Sub Button1_Click
    
    ipinfo
 
End Sub
 
Last edited:
Top