Android Question CURL trouble

Philip Prins

Active Member
Licensed User
Longtime User
Hello,

This is the info i get from the manufacturar:

I tried this:
B4X:
Dim S1 As String = ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S2 As String = ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S3 As String =  ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S4 As String =  ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress

Dim NV As Map
    NV.Initialize
    NV.Put("single", S1)
    NV.Put("double", S2)
    NV.Put("long", S3)
    NV.Put("touch", S4)
    
    Dim req As OkHttpRequest
    req = MultipartPost.CreatePostRequest("http://192.168.254.1/api/v1/device/"&ButtonMac, NV, Null)
    req.SetHeader("Content-Type", "application/json")
    req.SetContentType("application/json")
    req.SetContentEncoding("text/plain")
    
    hc.Execute(req, 1)

I also tried to make a JSON string and send it with POST string.
B4X:
    Dim S1 As String = "single="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S2 As String =  "double="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S3 As String =  "long="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress
    Dim S4 As String =  "touch="&ServerIP&":61042/MyStrom?id="&ButtonID&"&fi="&FleetID&"&tp=1&msg="&MessageTxt&"&ads="&MessageAdress


Dim X As Map
    X.Initialize
    X.Put("single",S1)
    X.Put("double",S2)
    X.Put("long",S3)
    X.Put("touch",S4)
    
    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(X)
    Dim R As String = JSONGenerator.ToPrettyString(2)
    
    Dim J As HttpJob
    J.Initialize("AST1",Me)
    j.PostString("http://192.168.254.1/api/v1/device/"&ButtonMac,R)
    j.GetRequest.SetContentType("application/x-www-form-urlencoded")


I always get as result SUCCES.

When i read the device i see this:
Strange characters instead of //

B4X:
response: anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpResponse@78dc88f
BSSID 62:01:94:27:ec:bd
{
    "single": "http:\/\/85.214.204.131:61042\/MyStrom?id",
    "double": "",
    "long": "",
    "touch": "",
    "generic": ""
}

What am i doing wrong??
 

DonManfred

Expert
Licensed User
Longtime User
OkHttpRequest
sounds like an mistake to use it at all.

I suggest to switch to okhttputils2 and not using the client directly!

Strange characters instead of //
/ must be escaped. It´s not strange

B4X:
Dim parser As JSONParser
parser.Initialize($"{
    "single": "http:\/\/85.214.204.131:61042\/MyStrom?id",
    "double": "",
    "long": "",
    "touch": "",
    "generic": ""
}"$)
Dim root As Map = parser.NextObject
Dim single As String = root.Get("single")
Dim double As String = root.Get("double")
Dim touch As String = root.Get("touch")
Dim long As String = root.Get("long")
Dim generic As String = root.Get("generic")

You should follow the route using httpjob.
 
Last edited:
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Hello Don Manfred,

Can you point me in the right direction, been trying different things for the last 5 hours bu can not get the parameters in the device completely.
Tried String Utils to encode URL, tried so many things that i am completely lost now...
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Smart String Literals .. https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/

notice @DonManfred Parser.Initialize string in Post #2

for example ..
B4X:
Dim S1 As String = $"single=${ServerIP}:61042/MyStrom?id=${ButtonID}&fi=${FleetID}&tp=1&msg=${MessageTxt}&ads=${MessageAdress}"$

B4X:
j.PostString($"http://192.168.254.1/api/v1/device/${ButtonMac}"$,R)

Log the output to ensure it is as expected.
 
Last edited:
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…