Widget and HTTP Client

alfcen

Well-Known Member
Licensed User
Longtime User
This simple widget should download an HTML file, however there is no response, but an error in the unfiltered log (no error with filter):

** Service (keplerwidget) Start **
java.lang.NullPointerException
at anywheresoftware.b4a.http.HttpClientWrapper$3.run(HttpClientWrapper.java:211)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)

For a widget, is it perhaps mandantory to place the HC routines in a separate service module?

B4X:
'Service module
Sub Process_Globals
   Dim hc As HttpClient   
   Dim rv As RemoteViews
   Dim wm As String
End Sub

Sub Service_Create
   rv = ConfigureHomeWidget("layout_widget", "rv", 1440, "Kepler")
'   DownloadData
'   SetWidget
End Sub

Sub Service_Start (StartingIntent As Intent)
   If rv.HandleWidgetEvents(StartingIntent) Then Return
End Sub

Sub rv_RequestUpdate
   DownloadData
   SetWidget
End Sub

Sub rv_Disabled
   StopService("")
End Sub

Sub Service_Destroy

End Sub

Sub SetWidget
   rv.SetText("labData",wm)
   rv.UpdateWidget
End Sub

Sub Panel1_Click
   DownloadData
   SetWidget
End Sub

Sub DownloadData
   Dim Request As HttpRequest      
   Request.InitializeGet("http://kepler.nasa.gov")
   Request.Timeout = 30000
   If hc.Execute(Request, 1) = False Then Return 'Will be false if there is already a running task with the same TaskID.
End Sub

Sub hc_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   Select Case TaskID
      Case 1         
         Result = Response.GetString("UTF8")
         wm = Result
         Log(wm)
         SetWidget
   End Select
End Sub

Sub hc_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
   If StatusCode = -1 Then Reason = "Phone is offline"
   ToastMessageShow (Reason, True)
    If Response <> Null Then Response.Release   
End Sub
 

alfcen

Well-Known Member
Licensed User
Longtime User
Sorted!

Good that nobody tried to find an answer. Me senile idiot forgot 'hc.Initialize("hc")'. Added and all is perfect.
 
Upvote 0
Top