Android Question how to parse this data

Discussion in 'Android Questions' started by PassionDEV, Apr 20, 2019.

  1. PassionDEV

    PassionDEV Active Member Licensed User

    i am using this api

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

    using okhttp i grab its data like following

    Code:
    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: Apr 20, 2019
  2. PassionDEV

    PassionDEV Active Member Licensed User

    i figure it out i used Map to identify the parsed value
     
  3. Harris

    Harris Well-Known 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
     
  4. PassionDEV

    PassionDEV Active Member Licensed User

    i used jsonparse and map to parse this string

    example

    Code:
    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/
     
    Harris likes this.
  5. DonManfred

    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/

    Code:
    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 and PassionDEV like this.
  6. Harris

    Harris Well-Known Member Licensed User

  7. PassionDEV

    PassionDEV Active Member Licensed User

    @Harris i am not Canadian English is not my first language as well
     
  8. Harris

    Harris Well-Known Member 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!
     
    PassionDEV likes this.
  9. PassionDEV

    PassionDEV Active Member Licensed User

    @Harris Yep as a default it grabs your own ip if there is no parameter its good to know where you at
     
  10. Roycefer

    Roycefer Well-Known Member Licensed User

  11. joaomoraes83

    joaomoraes83 Member Licensed User

    how to post the data obtained in a label?
    label.text=...
     
  12. DonManfred

    DonManfred Expert Licensed User

    1. You should always create a new thread for YOUR questions. Do not post to existing threads.
    2. you are not providing enough infos. Additionally i do not understand the question. Be more detailed. What exactly are you trying to archieve?
     
  13. joaomoraes83

    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?
     
  14. DonManfred

    DonManfred Expert Licensed User

    Code:
    label.text = country
     
  15. joaomoraes83

    joaomoraes83 Member Licensed User

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

    Code:
    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
     
  16. Almora

    Almora Active Member Licensed User

    Code:
    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: Apr 26, 2019
    DonManfred likes this.
  17. Almora

    Almora Active Member Licensed User

    code edit..
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice