Android Question How to use the methode PUT

appie21

Active Member
Licensed User
Longtime User
Hello

I try to command my philips Hue lights
To comminucate with them I need the PUT command

How can I do this!

I have
B4X:
Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  Dim aan As String
  Dim lampstatus As String = "true"
  aan = "{""on"": " & lampstatus & "}"
  If Job.Success = True Then
      ToastMessageShow(Job.GetString,True)
Job.PostString("http://192.168.1.5/api/newdeveloper/lights/2/state", aan )
  Else
      Log("Error: " & Job.ErrorMessage)
      ToastMessageShow("Error: " & Job.ErrorMessage, True)
  End If
  Job.Release

  End Sub
 
Sub Button1_Click
      job1.Initialize("Job1", Me)
  job1.Download("http://192.168.1.5/api/newdeveloper/lights/2/state")
'job1.Download("http://192.168.1.5/api/newdeveloper/lights/1")
 
End Sub

What do i worng

De put data wgat i try to sent is : {"on":true}
and the link is : http://192.168.1.5/api/newdeveloper/lights/2/state

How can I fix sthis
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The compiled HttpUtils2 library only provides methods for GET or POST requests.

PUT request is very similar to a POST request.

You have two options:
1. Remove HttpUtils2 and use HttpClient directly.
2. Remove the reference to HttpUtils2 library. Add HttpUtils2 two code modules (and the libraries it depends on).
Modify HttpJob.PostBytes to:
B4X:
Public Sub PostBytes(Link As String, Data() As Byte)
   req.InitializePut2(Link, Data) '<-- InitializePut2 instead of InitializePost2
   CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
 
Upvote 0

appie21

Active Member
Licensed User
Longtime User
1. didn't work
can you exoplane option 2 in more detail?
and what can I do with
InitializePut (URL As String, InputStream As java.io.InputStream, Length As Int)

 
Last edited:
Upvote 0

appie21

Active Member
Licensed User
Longtime User
My program is broken:(

It will do this

B4X:
Dim lampstatus As String = "true"
aan = "{""on"": " & lampstatus & "}"
Dim URL As String
URL = "http://192.168.1.5/api/newdeveloper/lights/2/state"

This is what I want to sent with (aan) a put methode

what must i use now?
HttpJob or HttpRequest or..

Can you write a little code to understand?

Let me know!
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Erel is suggesting that either you use HTTPClient, directly, which is a bit more fiddly OR you remove the lib reference to HTTPUtils2, place the two modules directly in your code and edit the PostBytes code as per his recommendation.

The choice is yours, one or the other.
 
Upvote 0

appie21

Active Member
Licensed User
Longtime User
Hi I am beginner with basic4Android
I came from appinventor

This is how the code look like in appinventor and now I want to made it in B4A

Attached you se a image how it works in appinventor can some one make this for me work in B4A

(also I not understood you all because my english is not so good)
 

Attachments

  • appi.png
    appi.png
    92.9 KB · Views: 210
Upvote 0

appie21

Active Member
Licensed User
Longtime User
  • But it is my same question as in the first post?
Can some one make a sample use put methode with this information

B4X:
Dim lampstatus As String = "true"
aan = "{""on"": " & lampstatus & "}"
Dim URL As String
URL = "http://192.168.1.5/api/newdeveloper/lights/2/state"
 
Upvote 0

appie21

Active Member
Licensed User
Longtime User
I have this?

But i do not really understand

B4X:
Sub Globals
Dim req As HttpRequest

    Dim aan As String
    Dim lampstatus As String = "true"
    Dim URL As String
    aan = "{""on"": ""true""}"
    URL    = "http://192.168.1.5/api/newdeveloper/lights/2/state"
   
End Sub

Sub Activity_Create(FirstTime As Boolean)

    End Sub
Public Sub PostBytes(Link As String, Data() As Byte)
  req.InitializePut2(URL, aan) '<-- InitializePut2 instead of InitializePost2
  CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
 
Upvote 0

appie21

Active Member
Licensed User
Longtime User
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim req As HttpRequest
    Dim Button1 As Button
    Dim aan As String
    Dim url As String
    aan = "{""on"": ""true""}"
    url   = "http://192.168.1.41/api/newdeveloper/lights/2/state"
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)

    req.InitializePut2(Link, Data)
    CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub


Sub Button1_Click
    PutBytes(url, aan.GetBytes("UTF8"))
End Sub

I have now this and i get message b4a is stopped when i press button 1


what do i wrong
 

Attachments

  • appie.zip
    7.1 KB · Views: 183
Last edited:
Upvote 0

appie21

Active Member
Licensed User
Longtime User
Hi here my log

and app says : B4A is stoppend with a ok button!

B4X:
anywheresoftware.b4a.http.HttpClientWrapper$HttpUriRequestWrapper@41ef8510


startService: class anywheresoftware.b4a.samples.httputils2.httputils2service


** Service (httputils2service) Create **


** Service (httputils2service) Start **


java.lang.Exception: Sub submitjob signature does not match expected signature.


    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:172)
    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:958)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: java.lang.RuntimeException: java.lang.Exception: Sub submitjob signature does not match expected signature.


    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:961)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.Exception: Sub submitjob signature does not match expected signature.
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:196)
    at anywheresoftware.b4a.keywords.Common$5.run(Common.java:958)
    ... 9 more
Caused by: java.lang.Exception: Sub submitjob signature does not match expected signature.
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:172)
    ... 10 more
 
Upvote 0

eps

Expert
Licensed User
Longtime User
The SubmitJob, the code for that isn't included... I guess that's your Service for the lights / switches you wish to control.

The error indicates that you are sending info. that it isn't expecting or not in the right format.
java.lang.Exception: Sub submitjob signature does not match expected signature.
 
Upvote 0
Top