Android Question http post and parse xml info

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
B4X:
<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.


B4X:
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.
 

walterf25

Expert
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
B4X:
<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.


B4X:
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.
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
 

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.........
 

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 ?
 

grant1842

Active Member
Licensed User
Ok one snag.
I get an main.java:623: error: inconvertible types.
here is code.
B4X:
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 String, Attributes 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:

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.

B4X:
    Key = Job.GetString
    Dim In As InputStream
    Dim data() As Byte = Key.GetBytes("UTF8")
    In.InitializeFromBytesArray(data, 0, data.Length)
    Parser.Parse(In, "Parser")
 

fixit30

Active Member
Licensed User
You will also need to change the following for your code to work correctly...

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

B4X:
If Parser.Parents.IndexOf("Session") > -1 Then
 

grant1842

Active Member
Licensed User
Thanks a lot for your help.
looks like an null ponter.
B4X:
** 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.
B4X:
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 String, Attributes 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
 

DonManfred

Expert
Licensed User
i´m not sure but i would try it like this

B4X:
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
 

walterf25

Expert
Licensed User
Thanks a lot for your help.
looks like an null ponter.
B4X:
** 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.
B4X:
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 String, Attributes 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
You forgot to initialize the Parser variable that's why you're getting this error.

Cheers,
Walter
 

walterf25

Expert
Licensed User
Try this should work.

B4X:
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 String, Attributes 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
 
Top