Android Question [SOLVED] Problem: Widget using a Web Service does not run

Rene Barrera

Member
Licensed User
Hi, I know all of you are very busy, but i dare to distract you because this is ver important to me, so a beg to you to take a look to my problem and see if you can give me a suggestion.
The problem is that the code showed below is mounted in a service module so I can create a widget to get the weather from a web service. Is important to say that I implemented this code in a standar b4a application and it run correctly as espected, but now in the service module i can not get the same result
Don Manfred kindly suggested me to track the log, but i realy dont know how the log can be displayed from a widget. If more information is needed, please let me know.
Thanks in advance
B4X:
#Region  Service Attributes
    #StartAtBoot: True
#End Region

'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, "A-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

    ConsumirWebService

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...")
    
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_Temperature

End Sub

Sub Set_Temperature

'    lblTemperature.Text = strTemperature
rv.SetText("lblTemperatura", "strTemperatura")

 
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
 

DonManfred

Expert
Licensed User
i have inserted the widget into a testproject. after removing all the UI code (Alertboxes and so on) it works for me... i have put some logs into the code to see what happen....

Connect your device with usb-debuging to see the logs in B4A when running the app/widget
 

Attachments

Rene Barrera

Member
Licensed User
The project is splitted because of the size, so, please put the bin inside the Objects folder and then put the objects folder into the b4a - TIM-W_Weather project in order to work properly with this project.
Please let me know if it works with you or what is mi mistake
Thanks in advance
 

Attachments

Rene Barrera

Member
Licensed User
You should use HttpUtils2 instead of HttpClient. Your current code will not work on Android 4+ because of the call to HttpResponse.GetString.
Hi Erel, thank you for your answer, i must say that practically i am new in this, I read somewhere that httputils2 is easier to get web services, but in an example, I saw at least two more modules and honestly I do not understand what they do and how they do, if you or someone else have an easier example I would appreciate if can give it to me. This does not mean I do not want to research how httputils2 works, but now time is biting me. What I am trying to do is to get weather from a web services in a widget using basic4android.
Thank you very much
 
Last edited:

Rene Barrera

Member
Licensed User
i have inserted the widget into a testproject. after removing all the UI code (Alertboxes and so on) it works for me... i have put some logs into the code to see what happen....

Connect your device with usb-debuging to see the logs in B4A when running the app/widget
Hi Don, the zip (weatherwidget.zip) you attached is empty, please could you resend it to me, Thanks
 

Rene Barrera

Member
Licensed User
You should use HttpUtils2 instead of HttpClient. Your current code will not work on Android 4+ because of the call to HttpResponse.GetString.
Hi Erel,

I am trying to use the httputils2 to create this widget as you suggested, I am wondering how can I implement the web service (http://www.webservicex.net/globalweather.asmx) for request and get the response; this WS uses two parameters, City and Country, how do I do to set these parameters and even more the timeout and the Header where should they be set, I have been lookin around the forum and there is not too much information, I see some examples that does not have nothing to see with what i am looking for, I see two main examples (fickerviewer and httputils2). Erel, I know you are very busy but I really need your support. Please if it is possible provide me with a real example that is closer to what I need.

Thank you very much
 
Last edited:

Rene Barrera

Member
Licensed User
Hi Erel,

I am trying to use the httputils2 to create this widget as you suggested, I am wondering how can I implement the web service (http://www.webservicex.net/globalweather.asmx) for request and get the response; this WS uses two parameters, City and Country, how do I do to set these parameters and even more the timeout and the Header where should they be set, I have been lookin around the forum and there is not too much information, I see some examples that does not have nothing to see with what i am looking for, I see two main examples (fickerviewer and httputils2). Erel, I know you are very busy but I really need your support. Please if it is possible provide me with a real example that is closer to what I need.

Thank you very much
Hi Erel,

I have another doubt, you said that my code will not work on Android 4+.I do not know what is wrong because I run the same web service in a normal basic4android app and it does run succesfully, the problem is when I try to run it into a widget.

Thanks
 

Rene Barrera

Member
Licensed User
See HttpResponse.GetString documentation. It will not work on Android 4+ devices.

You need to use HttpUtils2.

I actually see many threads about HttpUtils2 in the forum. Specifically about headers:
http://www.basic4ppc.com/search?query=HttpUtils Headers
See the second result.
Hi Erel,

Thanks for your response, I saw the second result, but please believe me, I do not even have a bit of idea of how to request the web service in this way (httputils2), please, I beg to you that send me an example, or let me know who could I reach to ask for this. As far as i know in this forum we can get support, but i really got just suggestions, do not misunderstand, i like to research but i haven't found anything that can help me or I could understand, i reiterate i am new in this matter and do not have to much experience.

Thanks in advance
 

Rene Barrera

Member
Licensed User
Getting support here does not mean that someone other writes the code for your needs
Hi Don,

That´s exactly what i didn't want you all think about my support requests, i just wanted a closer example; I must say that i have been researching but i could find nothing. But no problem, anyway i appreciate your responses, i am so sorry for distracting you


Thank you
 
Last edited:

Rene Barrera

Member
Licensed User
Hi Erel, thank you for your answer, i must say that practically i am new in this, I read somewhere that httputils2 is easier to get web services, but in an example, I saw at least two more modules and honestly I do not understand what they do and how they do, if you or someone else have an easier example I would appreciate if can give it to me. This does not mean I do not want to research how httputils2 works, but now time is biting me. What I am trying to do is to get weather from a web services in a widget using basic4android.
Thank you very much
Hi Erel,

Thanks for your response, I saw the second result, but please believe me, I do not even have a bit of idea of how to request the web service in this way (httputils2), please, I beg to you that send me an example, or let me know who could I reach to ask for this. As far as i know in this forum we can get support, but i really got just suggestions, do not misunderstand, i like to research but i haven't found anything that can help me or I could understand, i reiterate i am new in this matter and do not have to much experience.

Thanks in advance
Hi Erel,

At last i got to get web service using HttpUtils2 with soap12, this way, I execute it into a b4a application and it runs good in my phone (Samsung Galaxy S4 Mini with Android 4.2), but once I try to run it as a widget it does not show anything in the screen of my phone. I attach both the app and the widget, and i beg for a couple of minutes of your time in order that you see if you can realize what is going on with the widget.

Thank you very much
 

Attachments

DonManfred

Expert
Licensed User
try this

Some things...
You did dim in processglobal.
It should not be dimmed here.

I moved the dim to sub ConsumirServiciSOAP. So everytime the sub runs it will create a new job

I also changed
B4X:
Sub rv_RequestUpdate
    ConsumirServiciSOAP
    rv.UpdateWidget
End Sub
to
B4X:
Sub rv_RequestUpdate
    ConsumirServiciSOAP
End Sub
Please note: At this point you started the asynchronous Httpjob... rv.updatewidget will not have anything to do


and i moved the rv.UpdateWidget
into the GetInfo-sub
B4X:
rv.SetText("lblTemperatura", Temperatura)
rv.UpdateWidget
 

Attachments

Last edited:

Rene Barrera

Member
Licensed User
try this

Some things...
You did dim in processglobal.
It should not be dimmed here.

I moved the dim to sub ConsumirServiciSOAP. So everytime the sub runs it will create a new job

I also changed
B4X:
Sub rv_RequestUpdate
    ConsumirServiciSOAP
    rv.UpdateWidget
End Sub
to
B4X:
Sub rv_RequestUpdate
    ConsumirServiciSOAP
End Sub
Please note: At this point you started the asynchronous Httpjob... rv.updatewidget will not have anything to do


and i moved the rv.UpdateWidget
into the GetInfo-sub
B4X:
rv.SetText("lblTemperatura", Temperatura)
rv.UpdateWidget
Hi Don,

I will test this, I really appreciate your support.
I´ll let you know the result.

Thank you very much
 

Rene Barrera

Member
Licensed User
Hi Don,

I will test this, I really appreciate your support.
I´ll let you know the result.

Thank you very much
Hi Don,

Now I am afraid that the web service is out of order, because it sent messages saying that There is "Interrnal Server Error" in both application and the widget as well as in SOAPUI, so I think I have to wait until WS starts over. I will let you know when finish.

Thanks again
 

Attachments

Top