Send a PUT request with HttpUtils2

avalle

Active Member
Licensed User
Longtime User
Hello,
I'm struggling with using the HttpUtils2 classes to send an HTTP PUT request.
I don't see a PutString method to use like in the following:

B4X:
Dim job1 As HttpJob
job1.Initialize("UpdateData", Me)
job1.PutString("http://myurl", "MyData")

Can anyone help? :BangHead:

Thanks
Andrea
 
Last edited:

avalle

Active Member
Licensed User
Longtime User

Attachments

  • HttpJob.bas
    4.2 KB · Views: 492
Upvote 0

avalle

Active Member
Licensed User
Longtime User
Grab the HTTP2utils library from post #1 in this thread:
http://www.b4x.com/android/forum/threads/httputils2-web-services-are-now-even-simpler.18992/

You can use the Flicker sample from the same post as a starting point. If you need the PUT function in your code just take the HttpJob.bas file modified by me as per post #3 above, and replace the file with the same filename in your project.

Looking at your other thread on Philips Hue lights control, the call you need to set your light state using PUT would look like this:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim job1 As HttpJob

    'Send a PUT request to set light state
    job1.Initialize("SetLightState", Me)
    job1.PutString("http://192.168.1.5/api/newdeveloper/lights/2/state", "...your JSON string...")
   
End Sub

Hope it helps, regards
Andrea
 
Upvote 0

appie21

Active Member
Licensed User
Longtime User
Hithanks but job1.putstring isa unknown member

how to do

I have

B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim job1 As HttpJob
    Dim req As HttpRequest
    Dim Button1 As Button
    Dim aan As String
    Dim url As String
    aan = "{""on"": ""true""}"
    url  = "http://192.168.1.42/api/newdeveloper/lights/2/state"
    Dim mLink As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Public Sub PutString(Link As String, Text As String)
  PutBytes(Link, Text.GetBytes("UTF8"))
End Sub

Public Sub PutBytes(Link As String, Data() As Byte)
  mLink = Link
  req.InitializePut2(Link, Data)
  CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub


Sub Button1_Click

    'Send a PUT request to set light state
    job1.Initialize("SetLightState", Me)
      job1.PutString("http://192.168.1.41/api/newdeveloper/lights/2/state", "{""on"": ""true""}")
End Sub
Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  If Job.Success = True Then
 
  Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
  End If
  Job.Release
End Sub
 
Last edited:
Upvote 0

avalle

Active Member
Licensed User
Longtime User
Hithanks but job1.putstring isa unknown member

how to do

As I said, you have to add the modified HttpJob.bas source to your project.
It contains the definition of pustring method.
Get it from post #3 in this thread.
 
Upvote 0

appie21

Active Member
Licensed User
Longtime User
by button1 click eveni have remov job1. and it works
(but my app stopped)
but in your code you talk about
mLink = Link
but what is mlink do/?

It looks like you module is not loaded i have add it but it not working must i put a extra line in my code to load your module too?

I have add my project
 

Attachments

  • alb.zip
    8.6 KB · Views: 263
Last edited:
Upvote 0
Top