Does a simple HTTP get requires the HTTP library

neavilag

Member
Licensed User
Longtime User
I just want to call certain url with parameters from the app. I don't need to wait for a response, how is this implemented? should it be used via http library.? I am new to B4A and dont understand well the HTTP library

thanks
 

FinerDesign

Member
Licensed User
Longtime User
I guess you could do it using a webview but seems a waste if its just hidden in background, unless you launch the URL in the android internet browser

I think HTTP library is your best bet
http://www.b4x.com/forum/basic4andr...urrency-converter-http-web-services-more.html

Just keep searching the forums and you will piece it together, I got this answer from copy and pasting from various posts.


add this to your globals
B4X:
Dim HttpClient1 As HttpClient

and providing that you are passing parameters in the URL itself
B4X:
    Dim request As HttpRequest
    request.InitializeGet(YourURL)
    request.Timeout = 10000 'set timeout to 10 seconds
    If HttpClient1.Execute(request, 1) = False Then Return 'Execute request, Will be false if their is already a running task (with the same id).

or if you pass parameters as a post request
B4X:
   Dim request As HttpRequest
   Dim Parameters as String
   Parameters  = "Parameter1=Value&Parameter2=Value"   
   request.InitializePost2(YourURL,Parameters.GetBytes("UTF8"))
   request.Timeout = 10000 'set timeout to 10 seconds
   If HttpClient1.Execute(request, 1) = False Then Return 'Execute request, Will be false if their  is already a running task (with the same id).

add this if you care if it worked or not otherwise ignore:
B4X:
Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Log("ResponseSuccess")
   
   ''add code here to do anything on a successful HTTP request

end sub
 
Last edited:
Upvote 0

neavilag

Member
Licensed User
Longtime User
Please see my implementation borrowed from twitter sample, is this a good approach ?

BTW you can see what I wanted to achieve. send GPS data to a site via httpget. Found some issues.
1. Time data from Location Object is not UTC is this true ?

2. Service works great outdoors, even I check if not good accuracy to abort report and stop gps, I noted that after sometime the service can get looped indoors and then try to report positions every second, even that after a good report a GPS1.stop is issued in LocationChanged I got 7000+ reports of last night and have to kill the service :-((

here is the code.

B4X:
'Service module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim GPS1 As GPS
   Dim count As Int
   Dim HC As HttpClient
   Dim url As String
   Dim hora As String

End Sub
Sub Service_Create
GPS1.Initialize("GPS")
HC.Initialize("HC")
 If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    End If

End Sub

Sub GPS_LocationChanged (Location1 As Location)
   count = count + 1
   If Location1.Accuracy <=100 Then
      'call sub to send data to server
      hora=DateTime.Date(Location1.Time + 6*DateTime.TicksPerHour)
      url = "http://XXXX.XX?UnitId="&"XXXX"&"&date="&hora&"&latitude="&Location1.Latitude&"&longitude="&Location1.Longitude&"&speed="&(Location1.Speed*3.6)&"&rawdata=EcuService"
      ToastMessageShow(url, True)
      Dim req As HttpRequest
      req.InitializeGet(url)
       HC.Execute(req, 1)       
   End If
   GPS1.Stop   
End Sub

Sub HC_ResponseError (Reason As String, StatusCode As Int, TaskId As Int)
   ToastMessageShow("Error: " & Reason, True)
End Sub

Sub HC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
   ToastMessageShow("success", True)
End Sub

Sub Service_Start (StartingIntent As Intent)
     DateTime.DateFormat = "yyyy-MM-dd%20HH:mm:ss" 
      StartServiceAt("", DateTime.Now + 5 * DateTime.TicksPerMinute, False)
     ToastMessageShow("SS Turning GPS ON", True)
      GPS1.Start(0, 0) 'Listen to GPS with no filters.
     count = 0
End Sub

Sub Service_Destroy

End Sub
 
Upvote 0
Top