Other Help needed converting old app from http to HttpUtils2

JimAvanti

Member
Licensed User
Longtime User
I Had an app working good until I recently tried re-compiling and found out that the http library is no longer supported. I tried OKHttp, however I now get an error when I try to use Response.GetString("UTF8"). I also tried HttpUtils2, but couldn’t figure out how to do what I need to do with that either.
I have a Status textfile (homestatus.txt) on a web-server that gets updated constantly. All I am trying to do is read it into a string so that I can know the status as it changes. The Status is read on a timer.
Does anyone know why and when GetString was removed and if I can go back to using it or not. Or even better, how to do what I need to do with HttpUtils2?
Thanks,
Jim
Following code is what used to work:
Globals:
DimHttpReqAsHttpRequest
DimHttpCAsHttpClient
Sub Get_HTTP_Status
HttpReq.InitializeGet("http://" & IPaddress & "/homestatus.txt")
HttpC.Execute(HttpReq, 1)
End Sub
Sub Activity_Create(FirstTime AsBoolean)
Activity.LoadLayout("Alarm_Control_1")

tmrIP.Initialize("tmrIP", 500)
tmrIP.Enabled = True

If FirstTime Then
hc.Initialize("http")
Get_HTTP_Status
EndIf
End Sub
Sub tmrIP_Tick
Get_HTTP_Status
End Sub
Sub hc_ResponseError (Response AsHttpResponse, Reason AsString, StatusCode AsInt, TaskId AsInt)
Str = "Error: " & Reason & " " & StatusCode
If Response <> NullThen
Response.Release
EndIf
End Sub
Sub http_responsesuccess(Response AsHttpResponse, TaskId AsInt)
Str = Response.GetString("UTF8")
.
.
.
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Here is an example reduced to the bare minimum using HttpUtils2 [not tested!].

B4X:
Sub Globals
   Dim Urljob As HttpJob
End Sub

Sub Activity_Create(FirstTime As Boolean)
   HttpUtils2Service.TempFolder=File.DirRootExternal & "/tmp/"     'Set temp directory [optional]
  
   'Get the URL
Urljob.Initialize("Urljob", Me)  ' "Urljob" is the name of job
Urljob.Download("http://" & IPaddress & "/homestatus.txt")
End Sub

'Event handler for Job Done
Sub JobDone(Job As HttpJob)
   If Job.Success = True Then
   ' Here you can do something with the text file, for example:
   Dim TextReader1 As TextReader
   Dim Line As String
   TextReader1.Initialize(Job.GetInputStream)
   Line = TextReader1.ReadLine  ' Line now contains the first line in the txt file!
   .......

   End If
End Sub
 
Last edited:
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Note that in most cases you do not need to use TextReader.

I use this method to read a HTML file line by line for parsing. There is an easier way??? I am always willing to learn!

Please explain...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Sub Globals
Dim Urljob As HttpJob
End Sub
It is ok when you want to use EXACTLY ONE Request with the Job.

Usually DIMming the job in Gobals is the wrong way to use it.

I suggest putting the dim in the line before
B4X:
Urljob.Initialize("Urljob", Me)  ' "Urljob" is the name of job
in Activity Create (in your example)
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
It is ok when you want to use EXACTLY ONE Request with the Job.

Interesting. I cannot see your reasoning - sorry. I have my job DIMed in globals and use the same job repeatedly (wait for the job to finish and then initialize again). Works fine. What difference does it make?

Usually DIMming the job in Gobals is the wrong way to use it.

If it is wrong, then please explain why, not just for me but for other users as well.

Danke für deine Antwort Manfred.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I´m not sure if I can explain example why.
But: ALL Examples erel posted or any httputils tutorial does NOT use a GLOBAL job. The job is ALWAYS Dimmed where the job is started.
Additional i saw a lot of help-asking-threads where users uses global job and REUSE them.
I´m pretty sure somewhere erel has written that you should NOT reuse a job (but i dont know where and when). Instead a new one should be dimmed. always.
Deep in my brain someone is telling me that the Job.Release do release some things which are not when you are reusing a job... But i dont know if "my brain" is telling the truth. :D
 
Upvote 0
Top