Android Question Problem managing weather from web service in b4a

Rene Barrera

Member
Licensed User
Hi there,

I need your support in order to solve a problem that has me stuck, the point is that I need to consume the temperature from web service, I found an example from a tutorial but I think it is not complete. The following is part of the code, when try to run it i get the error:

"resultSoapXML = Response.GetString("UTF8")
Java.lang.NullPointerException"

I hope this is clear enough, or if something else is needed, please let me know.

Thanks in advance.

'Service module
Sub Process_Globals
Dim hc As HttpClient 'download the page
Dim hcimage As HttpClient 'download the image
Dim req As HttpRequest
Dim reqimage As HttpRequest
End Sub
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
Sub Globals
Dim temp As String
Dim btnweather As Button
Dim edtweather As EditText
Dim lbltemperature As Label
Dim img As Bitmap
'Set a default start city when there is nothing in the DB
Dim selectedcity As String :selectedcity ="Christchurch"
Dim townpath As String
Dim label1 As Label ‘holds the temp
Dim imgtemp As ImageView
Dim pic As String 'holds the image
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
hc.Initialize("hc")
hcimage.Initialize("hcimage")
End Sub
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
Sub btnweather_click
strplace = "christchurch" 'note its lower case
townpath = "http://m.metservice.com/towns/"& strplace
req.InitializeGet(townpath)
hc.Execute(req, 1)
End Sub
''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''
Sub hc_ResponseSuccess(Response AsHttpResponse, TaskId AsInt)
Dim temp As String
temp = Response.GetString("UTF8") 'Get the whole page
…..
When the bolded line is achieved, i get the mentioned message.
 

DonManfred

Expert
Licensed User
1. Use Code-Tags when posting code! [CODE ]mycode[/CODE ] (without the spaces)
2. If i call this url i did NOT get any XML as result. It´s just html but not xml
3. why are you using httpclient? httputils2 should be better (see example)

B4X:
    Dim strplace As String = "christchurch"
    Dim php As HttpJob
    php.Initialize("gettown",Me)
    php.Download("http://m.metservice.com/towns/"& strplace)

and then
B4X:
Sub JobDone(Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim res As String
        res = Job.GetString
        Log("JobName: "&Job.JobName)
        If Job.JobName = "gettown" Then
            Log("result is: "&res)           
        End If
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

Attachments

  • getchurchtown.zip
    6.4 KB · Views: 86
Upvote 0

Rene Barrera

Member
Licensed User
First of all thanks for your support.
I used httpclient cause the example i saw did this way.
I´ll use the way you say and I will let you all know what the result is as soon as i get it.
Yesterday i downloaded an application named SOAPUI that generates de XML needed to request the web service, and it worked well, I will show how i got the result succesful
Again, thank you very much
 
Upvote 0

Rene Barrera

Member
Licensed User
I share the successful result, this is the code I used in the application in order to get results from the web service of weather

B4X:
Sub Process_Globals
   
    Dim HttpClient1 As HttpClient
End Sub

Sub Globals
   
    Dim URL As String
  Dim XML As String
    Dim Ciudad As String : Ciudad = "acapulco"
    Dim Pais As String   : Pais = "mexico"
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

     Activity.LoadLayout("lyoWeather")

   If FirstTime Then
     HttpClient1.Initialize ("HttpClient1")
   End If 
   ConsumirServicio
    
End Sub

Sub ConsumirServicio()
      
    Dim Request As HttpRequest
       
    URL = "http://www.webservicex.net/globalweather.asmx"
   
    XML = XML & "<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:web='http://www.webserviceX.NET'>"
    XML = XML & "<soap:Header/>"
    XML = XML & "<soap:Body>"
    XML = XML & "<web:GetWeather>"
    XML = XML & "<web:CityName>" & Ciudad & "</web:CityName>"
    XML = XML & "<web:CountryName>" & Pais & "</web:CountryName>"
    XML = XML & "</web:GetWeather>"
    XML = XML & "</soap:Body>"
    XML = XML & "</soap:Envelope>"
               
  Request.InitializePost2(URL, XML.GetBytes("UTF8"))
  Request.SetHeader("Content-Type", "application/soap+xml; charset=utf-8")                                                                   
  Request.Timeout = 60000
  If HttpClient1.Execute(Request, 1) = False Then Return
      
    ProgressDialogShow("Esperando Respuesta...")
    
End Sub

Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
  
    ProgressDialogHide
   
  Dim resultSoapXML As String 
   
  resultSoapXML = Response.GetString("UTF8") 
  Msgbox(resultSoapXML,"") 
   
    GetInfo(resultSoapXML)

End Sub

Sub GetInfo(ResultSoapXML As String)

    Dim intFin As Int
    Dim strLocation As String                     'Trae la Localidad
    Dim strTime As String                           'Trae la Hora
    Dim strWind As String                           'Trae el Viento
    Dim strVisibility As String               'Trae la Visibilidad
    Dim strSkyConditions As String           'Trae las condiciones del Cielo (Condición Amosférica) 
    Dim strTemperature As String               'Trae la temperatura
    Dim strDewPoint As String                   'Trae el Punto de rocío (Humedad)
    Dim strRelativeHumidity As String   'Trae la Humedad Relativa
    Dim strPressure As String                    'Trae la Presión Atmosférica
   
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''INI - substrings para ir obteniendo la Información del Web Service del Clima'''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'Localidad
    strLocation = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Location&gt;")+ 16)
    intFin = strLocation.IndexOf("&lt;/Location&gt;")
    strLocation = strLocation.subString2(0, intFin)

    'Hora
    strTime = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Time&gt;")+ 12)
    intFin = strTime.IndexOf("&lt;/Time&gt;")
    strTime = strTime.subString2(0, intFin)
   
    'Wind (Viento)
    strWind = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Wind&gt;")+ 12)
    intFin = strWind.IndexOf("&lt;/Wind&gt;")
    strWind = strWind.subString2(0, intFin)
   
    'Visibility (Viibilidad)
    strVisibility = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Visibility&gt;")+ 18)
    intFin = strVisibility.IndexOf("&lt;/Visibility&gt;")
    strVisibility = strVisibility.subString2(0, intFin)
   
    'SkyConditions (Condición Atmosférica)
    strSkyConditions = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;SkyConditions&gt;")+ 21)
    intFin = strSkyConditions.IndexOf("&lt;/SkyConditions&gt;")
    strSkyConditions = strSkyConditions.subString2(0, intFin)
   
    'Temperature (Temperatura)
    strTemperature = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Temperature&gt;")+ 19)
    intFin = strTemperature.IndexOf("&lt;/Temperature&gt;")
    strTemperature = strTemperature.subString2(0, intFin)
   
    'DewPoint (Humedad)
    strDewPoint = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;DewPoint&gt;")+ 16)
    intFin = strDewPoint.IndexOf("&lt;/DewPoint&gt;")
    strDewPoint = strDewPoint.subString2(0, intFin)

    'Relative Humidity (Humedad relativa)
    strRelativeHumidity = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;RelativeHumidity&gt;")+ 24)
    intFin = strRelativeHumidity.IndexOf("&lt;/RelativeHumidity&gt;")
    strRelativeHumidity = strRelativeHumidity.subString2(0, intFin)
   
    'Pressure (presión Atmosférica)
    strPressure = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Pressure&gt;")+ 16)
    intFin = strPressure.IndexOf("&lt;/Pressure&gt;")
    strPressure = strPressure.subString2(0, intFin)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''FIN - substrings para ir obteniendo la Información del Web Service del Clima'''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Sub

Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)   
 
    ProgressDialogHide 
  Dim resultSoapXML As String
   
  resultSoapXML = Response.GetString("UTF8")

    Msgbox(resultSoapXML,"") 
 
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0

Rene Barrera

Member
Licensed User
Hi, the above code was implemented in a standar b4a app and i got the results successfuly, now I wanto to create a widget with this same code but widget does not run as i espected, i got a message saying that the widget was stoped.
Could anybody give me a hand with this?
Thanks in advance
 
Upvote 0

Rene Barrera

Member
Licensed User
The following is the code i am using for my widget in the service module, but it does not work, as i said before the message is: widget has stoped.
Hope someone can help with this. I want to find out what i am missing or where i am screwing it
Thanks

B4X:
'Service module
Sub Process_Globals
    Dim rv As RemoteViews
   
    Dim HttpCli1 As HttpClient
   
    Dim URL As String
  Dim XML As String
    Dim Ciudad As String : Ciudad = "acapulco"
    Dim Pais As String   : Pais = "mexico"
   
End Sub
Sub Service_Create

    'Set the widget to update every 60 minutes.
    rv = ConfigureHomeWidget("lyoWeather", "rv", 60, "TIM-W-Clima")
    HttpCli1.Initialize ("HttpCli1")
'    ConsumirWebService
   
End Sub

Sub Service_Start (StartingIntent As Intent)

    Dim intCntr As Int
   
    If rv.HandleWidgetEvents(StartingIntent) Then Return
   

End Sub

Sub rv_RequestUpdate
'    SetTime
    ConsumirWebService
'    SetTime
'    rv.UpdateWidget
End Sub

Sub ConsumirWebService()

    Dim Request As HttpRequest
       
    URL = "http://www.webservicex.net/globalweather.asmx"
   
    XML = XML & "<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:web='http://www.webserviceX.NET'>"
    XML = XML & "<soap:Header/>"
    XML = XML & "<soap:Body>"
    XML = XML & "<web:GetWeather>"
    XML = XML & "<web:CityName>" & Ciudad & "</web:CityName>"
    XML = XML & "<web:CountryName>" & Pais & "</web:CountryName>"
    XML = XML & "</web:GetWeather>"
    XML = XML & "</soap:Body>"
    XML = XML & "</soap:Envelope>"
               
  Request.InitializePost2(URL, XML.GetBytes("UTF8"))
  Request.SetHeader("Content-Type", "application/soap+xml; charset=utf-8")                                                                   
  Request.Timeout = 6000000
  If HttpCli1.Execute(Request, 1) = False Then Return
      
    ProgressDialogShow("Esperando Respuesta...")
   
     rv.SetText("Label2", "SI LLEGA con initialize")
    
End Sub

Sub HttpCli1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
  
    ProgressDialogHide
   
  Dim resultSoapXML As String 
   
  resultSoapXML = Response.GetString("UTF8") 
  Msgbox(resultSoapXML,"") 
   
'    GetInfo(resultSoapXML)

End Sub

Sub HttpCli1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)   
 
    ProgressDialogHide 
  Dim resultSoapXML As String
   
  resultSoapXML = Response.GetString("UTF8")

    Msgbox(resultSoapXML,"") 
 
End Sub

Sub GetInfo(ResultSoapXML As String)

    Dim intFin As Int                                        'Indice del fin de búsqueda del string correspondiente
    Dim strResult As String                       'Trae inf del Resultado para validar si se tiene información
    Dim strLocation As String                     'Trae la Localidad
    Dim strTime As String                           'Trae la Hora
    Dim strWind As String                           'Trae el Viento
    Dim strVisibility As String               'Trae la Visibilidad
    Dim strSkyConditions As String           'Trae las condiciones del Cielo (Condición Amosférica) 
    Dim strTemperature As String               'Trae la temperatura
    Dim strDewPoint As String                   'Trae el Punto de rocío (Humedad)
    Dim strRelativeHumidity As String   'Trae la Humedad Relativa
    Dim strPressure As String                    'Trae la Presión Atmosférica
   
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''INI - substrings para ir obteniendo la Información del Web Service del Clima'''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'Valida si se tiene información para la localidad seleccionada
    strResult = ResultSoapXML.SubString(ResultSoapXML.IndexOf("<GetWeatherResult>")+ 18)
    intFin = strResult.IndexOf("</GetWeatherResult>")
    strResult = strResult.subString2(0, intFin)
   
    If strResult = "Data Not Found" Then
        ToastMessageShow ("No se tiene Información de Temperatura para " & Ciudad & "!", True)
        rv_Disabled
    End If

    'Localidad
    strLocation = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Location&gt;")+ 16)
    intFin = strLocation.IndexOf("&lt;/Location&gt;")
    strLocation = strLocation.subString2(0, intFin)

    'Hora
    strTime = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Time&gt;")+ 12)
    intFin = strTime.IndexOf("&lt;/Time&gt;")
    strTime = strTime.subString2(0, intFin)
   
    'Wind (Viento)
    strWind = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Wind&gt;")+ 12)
    intFin = strWind.IndexOf("&lt;/Wind&gt;")
    strWind = strWind.subString2(0, intFin)
   
    'Visibility (Viibilidad)
    strVisibility = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Visibility&gt;")+ 18)
    intFin = strVisibility.IndexOf("&lt;/Visibility&gt;")
    strVisibility = strVisibility.subString2(0, intFin)
   
    'SkyConditions (Condición Atmosférica)
    strSkyConditions = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;SkyConditions&gt;")+ 21)
    intFin = strSkyConditions.IndexOf("&lt;/SkyConditions&gt;")
    strSkyConditions = strSkyConditions.subString2(0, intFin)
   
    'Temperature (Temperatura)
    strTemperature = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Temperature&gt;")+ 19)
    intFin = strTemperature.IndexOf("&lt;/Temperature&gt;")
    strTemperature = strTemperature.subString2(0, intFin)
   
    'DewPoint (Humedad)
    strDewPoint = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;DewPoint&gt;")+ 16)
    intFin = strDewPoint.IndexOf("&lt;/DewPoint&gt;")
    strDewPoint = strDewPoint.subString2(0, intFin)

    'Relative Humidity (Humedad relativa)
    strRelativeHumidity = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;RelativeHumidity&gt;")+ 24)
    intFin = strRelativeHumidity.IndexOf("&lt;/RelativeHumidity&gt;")
    strRelativeHumidity = strRelativeHumidity.subString2(0, intFin)
   
    'Pressure (presión Atmosférica)
    strPressure = ResultSoapXML.SubString(ResultSoapXML.IndexOf("&lt;Pressure&gt;")+ 16)
    intFin = strPressure.IndexOf("&lt;/Pressure&gt;")
    strPressure = strPressure.subString2(0, intFin)

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''FIN - substrings para ir obteniendo la Información del Web Service del Clima'''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Set_Location

End Sub

Sub Set_Location

'    lblTemperature.Text = strTemperature
   
End Sub

Sub imgWeather_Click
   
End Sub

Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)   
 
    ProgressDialogHide 
  Dim resultSoapXML As String
   
  resultSoapXML = Response.GetString("UTF8")

    Msgbox(resultSoapXML,"") 
 
End Sub

Sub rv_Disabled
    StopService("")
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0
Top