Android Question HTTP Post to Pushover with Attachment

Licht2002

Member
Licensed User
Longtime User
Hello to All,

I managed to send a HTTP Post to Pushover...
[CODE lang="b4x" title="HTTP Post"] Dim j As HttpJob
j.Initialize("",Me)
Dim values As Map = CreateMap ("token":"APP_TOKEN","user":"USER_KEY","device":"","title":"title","message":"hello world")
'Dekodiere Anfang
Dim sb As StringBuilder
Dim su As StringUtils
sb.Initialize
Dim first As Boolean = True
For Each key In values.Keys
If Not(first) Then
sb.Append("&")
Else
first = False
End If
sb.Append($"${su.EncodeUrl(key, "UTF8")}=${su.EncodeUrl(values.Get(key), "UTF8")}"$)
Next
sb.ToString
'Dekodiere Ende

Log ("Sende encoded Pushover:" & sb)
j.PostString("https://api.pushover.net/1/messages.json",sb)
j.GetRequest.SetContentType("application/x-www-form-urlencoded")[/CODE]

Now only the attachment is missing! Can someone help me to implement this? At the Homepage from Pushover Pushover API i found an example in Python3
[CODE lang="python" title="Code for Pushover in Python3"]import requests
r = requests.post("https://api.pushover.net/1/messages.json", data = {
"token": "APP_TOKEN",
"user": "USER_KEY",
"message": "hello world"
},
files = {
"attachment": ("image.jpg", open("your_image.jpg", "rb"), "image/jpeg")
})
print(r.text)[/CODE]

What do i have to add to my code?

Thanks

Tom
 
Use PostMultipart
 
Upvote 0
Thank you... DonManfred!!!

I added the File-Handle for Multipart

[CODE lang="b4x" title="I add the File-Handle"]
Dim j As HttpJob
j.Initialize("",Me)
Dim values As Map = CreateMap ("token":"APP_TOKEN","user":"USER_KEY","device":"","title":"title","message":"hello world")
'Dekodiere Anfang
Dim sb As StringBuilder
Dim su As StringUtils
sb.Initialize
Dim first As Boolean = True
For Each key In values.Keys
If Not(first) Then
sb.Append("&")
Else
first = False
End If
sb.Append($"${su.EncodeUrl(key, "UTF8")}=${su.EncodeUrl(values.Get(key), "UTF8")}"$)
Next
sb.ToString
'Dekodiere Ende
Dim files As List
files.Initialize
Dim fd1 As MultipartFileData
fd1.Initialize
fd1.KeyName = "Abbey"
fd1.Dir = File.DirAssets
fd1.FileName = "abbey.jpg"
fd1.ContentType = "image/jpg"
files.Add(fd1)


Log ("Sende encoded Pushover:" & sb)
j.PostMultipart("https://api.pushover.net/1/messages.json",sb, files)
'j.PostString("https://api.pushover.net/1/messages.json",sb)
j.GetRequest.SetContentType("application/x-www-form-urlencoded")
[/CODE]

But now i get an error - but not from the File-Attachment...!?!??
[CODE title="B4A Error"]j.PostMultipart(\
javac 1.8.0_191
src\b4a\webcam\main.java:3143: error: incompatible types: StringBuilder cannot be converted to MyMap
_j._postmultipart /*String*/ ("https://api.pushover.net/1/messages.json",(anywheresoftware.b4a.objects.collections.Map) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.collections.Map(), (anywheresoftware.b4a.objects.collections.Map.MyMap)(_sb.getObject())),_files);
^
1 error[/CODE]

I there something wrong with the stringbuilder?

Thx
Tom
 
Upvote 0
sb.ToString
does not do anything if you to not use the returnvalue
For what do you need the stringbuilder at all? Send the map with postmultipart directly.
The second parameter in PostMultipart needs a MAP not a stringbuilder and even not a String.
 
Last edited:
Upvote 0
does not do anything if you to not use the returnvalue
For what do you need the stringbuilder at all? Send the map with postmultipart directly.
The second parameter in PostMultipart needs a MAP not a stringbuilder and even not a String.
OK.... Stringbuilder do nothing = removed!

Multipartupload does not work - the Messeage will be send without the picture :-(


B4X:
    Dim j As HttpJob
    j.Initialize("",Me)

    Dim images As List
    images.Initialize
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "file"
    fd.Dir = File.DirAssets
    fd.FileName = "veli.jpg"
    fd.ContentType = "image/jpg"
    images.Add(fd)

    j.PostMultipart("https://api.pushover.net/1/messages.json", CreateMap ("token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","user":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","device":"iphone","title":"Titel","message":"Message") , Array(fd))

Is there something wrong?

Thx

Tom
 
Upvote 0
Back
Top