Android Question http post and parse xml info

Discussion in 'Android Questions' started by grant1842, Apr 23, 2015.

  1. grant1842

    grant1842 Active Member Licensed User

    I need to make a post to
    http://xmldata.qrz.com/xml/current/?username=xx1xxx;password=abcdef;
    the result will be in xml
    Code:
    <QRZDatabase xmlns="http://xmldata.qrz.com" version="1.33">
    <Session>
    <Key>640ff538f6f1bd3490614f0f9c7e6e01</Key>
    <Count>
    9387</Count>
    <SubExp>Mon Nov 
    23 00:00:00 2015</SubExp>
    <GMTime>Wed Apr 
    22 23:24:17 2015</GMTime>
    <Remark>cpu: 
    0.024s</Remark>
    </Session>
    </QRZDatabase>
    I need to get the Key and use it to make a different request.
    I am useing HttpUtils2.
    Here is the code i have started.
    I am trying to figure out how to post to the url and get the key from the xml.


    Code:
    Dim getsessionkey As HttpJob
    Dim key As String
        getsessionkey.Initialize(
    "getsession","Me")
        getsessionkey.PostString(
    "http://xmldata.qrz.com/xml/current/?username=someuser;password=somepassword;agent=gdf.0","key")
        
    'key = get key from xml response from post
    Thanks for your help.
     
  2. walterf25

    walterf25 Well-Known Member Licensed User

    You can use the xml parser library, take a look at the tutorial here: http://www.b4x.com/android/forum/threads/xml-parsing-with-the-xmlsax-library.6866/#content

    Good luck!
    Walter
     
  3. grant1842

    grant1842 Active Member Licensed User

    Thanks for the comment Walter.
    I have to figure out how to do the post first to get the xml.........
     
  4. DonManfred

    DonManfred Expert Licensed User

    Are you sure you need to do a POSTstring instead of a DOWNLOAD?
     
  5. grant1842

    grant1842 Active Member Licensed User

    Not sure about that. Need to post to that url and get the key from the xml.
    Should I be using get ?
     
  6. DonManfred

    DonManfred Expert Licensed User

    Yes, i would try a job.download call
     
  7. grant1842

    grant1842 Active Member Licensed User

  8. grant1842

    grant1842 Active Member Licensed User

    Ok one snag.
    I get an main.java:623: error: inconvertible types.
    here is code.
    Code:
    Sub Button1_Click
    ToastMessageShow("sdfsd",True)

        
    'getsessionkey.Download("http://xmldata.qrz.com/xml/current/?username=someuser;password=somepassword;agent=gdf.0")
        Dim job1, job2, job3 As HttpJob
       job1.Initialize(
    "Job1", Me)
       
    'Send a GET request
       job1.Download2("http://xmldata.qrz.com/xml/current/?username=11111;password=44444;agent=gdf.0", _
          
    Array As String("first key""first value :)""second key""value 2"))
     


    End Sub


    Sub JobDone (Job As HttpJob)
       
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
       
    If Job.Success = True Then
          
    Select Job.JobName
             
    Case "Job1""Job2"
                
    'print the result to the logs
                Log(Job.GetString)
                
    ToastMessageShow(Job.GetString,True)
                key= Job.GetString
              
                
    'parse the xml file
                Dim In As InputStream
                
    In = key   <<<<<<<<<<<<<<<<<<<<<<<<<<<<<error
                parser.Parse(
    In"Parser")
                              
          
    End Select
       
    Else
          
    Log("Error: " & Job.ErrorMessage)
          
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
       
    End If
       Job.Release
    End Sub

    Sub Parser_StartElement (Uri As String, Name As StringAttributes As Attributes)

    End Sub
    Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
        
    If parser.Parents.IndexOf("item") > -1 Then
            
    If Name = "Key" Then
                Title = Text.ToString
            
    Else If Name = "Count" Then
                Link = Text.ToString
          
            
    End If
        
    End If
        
    If Name = "Key" Then
      
        
    ToastMessageShow(Name,True)
          
        
    End If
    End Sub
    The error is on In= Key.
    Do I need to change Dim In As InputStream to something else.
     
    Last edited: Apr 23, 2015
  9. fixit30

    fixit30 Active Member Licensed User

    You cannot directly assign a string to an inputstream. You will need to convert it to a byte array first.

    Code:
    Key = Job.GetString
        
    Dim In As InputStream
        
    Dim data() As Byte = Key.GetBytes("UTF8")
        
    In.InitializeFromBytesArray(data, 0, data.Length)
        Parser.Parse(
    In"Parser")
     
    DonManfred likes this.
  10. fixit30

    fixit30 Active Member Licensed User

    You will also need to change the following for your code to work correctly...

    Code:
    If Parser.Parents.IndexOf("item") > -1 Then
    To

    Code:
    If Parser.Parents.IndexOf("Session") > -1 Then
     
    Almora likes this.
  11. grant1842

    grant1842 Active Member Licensed User

    Thanks a lot for your help.
    looks like an null ponter.
    Code:
    ** Activity (main) Create, isFirst = true **
    ** 
    Activity (main) Resume **
    ** 
    Service (httputils2service) Create **
    ** 
    Service (httputils2service) Start **
    JobName = Job1, Success = 
    true
    <?xml version=
    "1.0" encoding="iso-8859-1" ?>
    <QRZDatabase version=
    "1.33" xmlns="http://xmldata.qrz.com">
    <Session>
    <Key>ae408468a3aab37fc1372735d8402255</Key>
    <Count>
    9387</Count>
    <SubExp>Mon Nov 
    23 00:00:00 2015</SubExp>
    <GMTime>Thu Apr 
    23 15:18:13 2015</GMTime>
    <Remark>cpu: 
    0.031s</Remark>
    </Session>
    </QRZDatabase>
    Error occurred on line: 
    187 (main)
    java.lang.NullPointerException
        at anywheresoftware.b4a.objects.SaxParser.parse(
    SaxParser.java:78)
        at anywheresoftware.b4a.objects.SaxParser.Parse(
    SaxParser.java:73)
        at gra.du.fu.main._jobdone(main.java:
    630)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:636)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:305)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:238)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    121)
        at anywheresoftware.b4a.BA$
    3.run(BA.java:320)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    157)
        at android.app.ActivityThread.main(ActivityThread.java:
    5633)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    896)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    712)
        at dalvik.system.NativeStart.main(Native Method)
    ** 
    Activity (main) Create, isFirst = true **
    ** 
    Activity (main) Resume **
    ** 
    Service (httputils2service) Create **
    ** 
    Service (httputils2service) Start **
    JobName = Job1, Success = 
    true
    <?xml version=
    "1.0" encoding="iso-8859-1" ?>
    <QRZDatabase version=
    "1.33" xmlns="http://xmldata.qrz.com">
    <Session>
    <Key>ae408468a3aab37fc1372735d8402255</Key>
    <Count>
    9387</Count>
    <SubExp>Mon Nov 
    23 00:00:00 2015</SubExp>
    <GMTime>Thu Apr 
    23 15:20:21 2015</GMTime>
    <Remark>cpu: 
    0.037s</Remark>
    </Session>
    </QRZDatabase>
    Error occurred on line: 
    187 (main)
    java.lang.NullPointerException
        at anywheresoftware.b4a.objects.SaxParser.parse(
    SaxParser.java:78)
        at anywheresoftware.b4a.objects.SaxParser.Parse(
    SaxParser.java:73)
        at gra.du.fu.main._jobdone(main.java:
    630)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:636)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:305)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:238)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    121)
        at anywheresoftware.b4a.BA$
    3.run(BA.java:320)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    157)
        at android.app.ActivityThread.main(ActivityThread.java:
    5633)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    896)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    712)
        at dalvik.system.NativeStart.main(Native Method)
    ** 
    Activity (main) Create, isFirst = true **
    ** 
    Activity (main) Resume **
    ** 
    Service (httputils2service) Create **
    ** 
    Service (httputils2service) Start **
    JobName = Job1, Success = 
    true
    <?xml version=
    "1.0" encoding="iso-8859-1" ?>
    <QRZDatabase version=
    "1.33" xmlns="http://xmldata.qrz.com">
    <Session>
    <Key>ae408468a3aab37fc1372735d8402255</Key>
    <Count>
    9387</Count>
    <SubExp>Mon Nov 
    23 00:00:00 2015</SubExp>
    <GMTime>Thu Apr 
    23 15:22:39 2015</GMTime>
    <Remark>cpu: 
    0.027s</Remark>
    </Session>
    </QRZDatabase>
    Error occurred on line: 
    187 (main)
    java.lang.NullPointerException
        at anywheresoftware.b4a.objects.SaxParser.parse(
    SaxParser.java:78)
        at anywheresoftware.b4a.objects.SaxParser.Parse(
    SaxParser.java:73)
        at gra.du.fu.main._jobdone(main.java:
    630)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.shell.Shell.runMethod(
    Shell.java:636)
        at anywheresoftware.b4a.shell.Shell.raiseEventImpl(
    Shell.java:305)
        at anywheresoftware.b4a.shell.Shell.raiseEvent(
    Shell.java:238)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:
    121)
        at anywheresoftware.b4a.BA$
    3.run(BA.java:320)
        at android.os.Handler.handleCallback(Handler.java:
    733)
        at android.os.Handler.dispatchMessage(Handler.java:
    95)
        at android.os.Looper.loop(Looper.java:
    157)
        at android.app.ActivityThread.main(ActivityThread.java:
    5633)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    896)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    712)
        at dalvik.system.NativeStart.main(Native Method)
    Here is the modified code.
    Code:
    Sub JobDone (Job As HttpJob)
       
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
       
    If Job.Success = True Then
          
    Select Job.JobName
             
    Case "Job1""Job2"
                
    'print the result to the logs
                Log(Job.GetString)
                
    ToastMessageShow(Job.GetString,True)
                key = Job.GetString
                    
    Dim In As InputStream
                    
    Dim data() As Byte = Key.GetBytes("UTF8")
                    
    In.InitializeFromBytesArray(data, 0, data.Length)
                  parser.Parse(
    In"Parser")
                               
          
    End Select
       
    Else
          
    Log("Error: " & Job.ErrorMessage)
          
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
       
    End If
       Job.Release
    End Sub

    Sub Parser_StartElement (Uri As String, Name As StringAttributes As Attributes)

    End Sub
    Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
        
    If parser.Parents.IndexOf("Session") > -1 Then
            
    If Name = "Key" Then
                Title = Text.ToString
            
    Else If Name = "Count" Then
                Link = Text.ToString
           
            
    End If
        
    End If
        
    If Name = "Key" Then
       
        
    ToastMessageShow(Name,True)
           
        
    End If
    End Sub
    Sub Button2_Click
       
    End Sub
     
  12. fixit30

    fixit30 Active Member Licensed User

    Which Line of your code is Line Number 187?
     
  13. DonManfred

    DonManfred Expert Licensed User

    i´m not sure but i would try it like this

    Code:
    Sub JobDone (Job As HttpJob)
       
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
       
    If Job.Success = True Then
          
    Select Job.JobName
             
    Case "Job1""Job2"
                
    'print the result to the logs
                Log(Job.GetString)
                
    ToastMessageShow(Job.GetString,True)
                key = Job.GetString
                    
    Dim In As InputStream = Job.Getinputstream
                    
    'Dim data() As Byte = Key.GetBytes("UTF8")
                    'In.InitializeFromBytesArray(data, 0, data.Length)
                  parser.Parse(In"Parser")
                              
          
    End Select
       
    Else
          
    Log("Error: " & Job.ErrorMessage)
          
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
       
    End If
       Job.Release
    End Sub
     
  14. fixit30

    fixit30 Active Member Licensed User

    The attached very simple example is working for me?
     

    Attached Files:

  15. walterf25

    walterf25 Well-Known Member Licensed User

    You forgot to initialize the Parser variable that's why you're getting this error.

    Cheers,
    Walter
     
    DonManfred and fixit30 like this.
  16. walterf25

    walterf25 Well-Known Member Licensed User

    Try this should work.

    Code:
    Sub Button1_ClickToastMessageShow("sdfsd",True)
    Parser.Initialize   
    'Initialize parser first
    'getsessionkey.Download("http://xmldata.qrz.com/xml/current/?username=someuser;password=somepassword;agent=gdf.0")Dim job1, job2, job3 AsHttpJob
     job1.Initialize("Job1", Me)'Send a GET request job1.Download2("http://xmldata.qrz.com/xml/current/?username=km4bob;password=airfile*1;agent=gdf.0", _ArrayAsString("first key", "first value :)", "second key", "value 2"))
    End Sub


    Sub JobDone (Job As HttpJob)
       
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
       
    If Job.Success = True Then
          
    Select Job.JobName
             
    Case "Job1""Job2"
                
    'print the result to the logs
                Log(Job.GetString)
                
    ToastMessageShow(Job.GetString,True)
                key = Job.GetString
                    
    Dim In As InputStream
                    
    Dim data() As Byte = Key.GetBytes("UTF8")
                    
    In.InitializeFromBytesArray(data, 0, data.Length)
                  parser.Parse(
    In"Parser")
                              
          
    End Select
       
    Else
          
    Log("Error: " & Job.ErrorMessage)
          
    ToastMessageShow("Error: " & Job.ErrorMessage, True)
       
    End If
       Job.Release
    End Sub

    Sub Parser_StartElement (Uri As String, Name As StringAttributes As Attributes)

    End Sub
    Sub Parser_EndElement (Uri As String, Name As String, Text As StringBuilder)
        
    If parser.Parents.IndexOf("Session") > -1 Then
            
    If Name = "Key" Then
                Title = Text.ToString
            
    Else If Name = "Count" Then
                Link = Text.ToString
          
            
    End If
        
    End If
        
    If Name = "Key" Then
      
        
    ToastMessageShow(Text.ToString,True)
          
        
    End If
    End Sub
     
  17. grant1842

    grant1842 Active Member Licensed User

    Solved......
    Thanks for your help.
    Works great.
     
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