Android Question [SOLVED]Poststring problem

elitevenkat

Active Member
Licensed User
Longtime User
hi I am trying to post json data using poststring method. It is returning always as false and the poststring method is not hitting the url at all.(ResponseError. Reason: , Response: )

However when tested from postman it works fine. Kindly help me in solving the issue.

B4X:
  B4A
        Main.cLocalIp="https://dapiserver.herokuapp.com"
        j2.PostString(Main.cLocalIp & "/neworder", "") 
        j2.GetRequest.SetHeader("order", ord)    '  ord is a  map
        j2.GetRequest.SetHeader("items", s)    ' s is a map
        j2.GetRequest.SetContentType("application/json")
      
        Wait For (j2) JobDone(j2 As HttpJob)
 
       If j2.Success Then
            Log(j2.GetString)
            'log("accepted " & j.GetString)            
        Else          
            Log(j2.ErrorMessage)
        End If
        j2.Release
the getmethod https://dapiserver.herokuapp.com/12345/orders?token=1000 is working fine
B4X:
   j2.Download(Main.cLocalIp & "/513002/orders?token=2c76584995a8450eaa2d68c7496d0d30")
    j2.GetRequest.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36")
    j2.GetRequest.SetHeader("Content-Type", "application/json")
        
    Wait For (j2) JobDone(j2 As HttpJob)   ' ---- works fine

B4X:
postman   code 
https://dapiserver.herokuapp.com/neworder
{
    "order": {
        "aggregator": 1,
        "aggname": "Test",
        "orderno": "764674764",
        "restaurantid": "12345",
        "token": "1000",
        "discount": "0",
        "packingcharges": "0"
    },
    "items": [
        {
            "aggitemcode": "677",
            "positemcode": null,
            "item": "Upma",
            "variant": "null",
            "qty": 1,
            "amount": "73"
        },
        {
            "aggitemcode": "644",
            "positemcode": null,
            "item": "Tea Without Sugar",
            "variant": "null",
            "qty": 1,
            "amount": "37"
        }
    ]
}
 

aeric

Expert
Licensed User
Longtime User
I think you should add the maps inside the poststring parameter instead of sending them as header.
B4X:
j2.PostString(Main.cLocalIp & "/neworder", CreateMap("order": ord, "items": s))

However, s looks like a list instead of a map.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
possibly related. (in addition to issues raised by aeric)
 

Attachments

  • working.png
    8.9 KB · Views: 150
Upvote 0

elitevenkat

Active Member
Licensed User
Longtime User
The poststring accepts both the 2 params as string only. Can't pass map in 2nd parameter
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
    Private map1 As Map = CreateMap("aggregator": 1, _
    "aggname": "Test", _
    "orderno": "764674764", _
    "restaurantid": "12345", _
    "token": "1000", _
    "discount": "0", _
    "packingcharges": "0")
    
    Private m1 As Map = CreateMap("aggitemcode": "677", _
    "positemcode": Null, _
    "item": "Upma", _
    "variant": "null", _
    "qty": 1, _
    "amount": "73")
    
    Private m2 As Map = CreateMap("aggitemcode": "644", _
    "positemcode": Null, _
    "item": "Tea Without Sugar", _
    "variant": "null", _
    "qty": 1, _
    "amount": "37")
    
    Private list1 As List
    list1.Initialize
    list1.Add(m1)
    list1.Add(m2)
    
    SendOrder(map1, list1)

B4X:
Sub SendOrder (ord As Map, s As List)
    Private cLocalIp As String = "https://dapiserver.herokuapp.com"
    Private j2 As HttpJob
    j2.Initialize("", Me)
    'j2.PostString(cLocalIp & "/neworder", "")
    Private data As Map = CreateMap("order": ord, "items": s)
    Log(data.As(JSON).ToString)
    j2.PostString(cLocalIp & "/neworder", data.As(JSON).ToString)
    'j2.GetRequest.SetHeader("order", ord)    '  ord is a  map
    'j2.GetRequest.SetHeader("items", s)    ' s is a map
    j2.GetRequest.SetContentType("application/json")
      
    Wait For (j2) JobDone(j2 As HttpJob)
    If j2.Success Then
        Log("Result:" & j2.GetString)
        'log("accepted " & j.GetString)
    Else
        Log("Error:" & j2.ErrorMessage)
    End If
    j2.Release
End Sub
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
?
B4X:
Public Sub TestJson
    Private orden As Map = CreateMap("aggregator": 1, _
                                    "aggname": "Test", _
                                    "orderno": "764674764", _
                                    "restaurantid": "12345", _
                                    "token": "1000", _
                                    "discount": "0", _
                                    "packingcharges": "0")
    
    
    Private items As List = Array (CreateMap("aggitemcode": "677", _
                                             "positemcode": Null, _
                                             "item": "Upma", _
                                             "variant": "null", _
                                             "qty": 1, _
                                             "amount": "73"), _
                                   CreateMap("aggitemcode": "644", _
                                             "positemcode": Null, _
                                             "item": "Tea Without Sugar", _
                                             "variant": "null", _
                                             "qty": 1, _
                                             "amount": "37"))
                                            
                                            
    Dim Data As Map = CreateMap("order": orden, "items": items)
    
    Dim URL As String = "https://dapiserver.herokuapp.com/neworder"
    Dim JsonText As String = Data.As(JSON).ToString
    
    Wait For (PostURL(URL, JsonText)) Complete (DataResult As String)
    Log(DataResult)
    
    
End Sub

Public Sub PostURL (URL As String, Data As String) As ResumableSub
    Dim Result As String
    Dim j As HttpJob
    Try
        j.Initialize("", Me)
        j.PostString(URL, Data)
        j.GetRequest.SetContentType("application/json")
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Result = j.GetString
        Else
            Result = j.Response.ErrorResponse
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
    Return Result
End Sub

tips:
Use
 
Upvote 0

elitevenkat

Active Member
Licensed User
Longtime User
Thank you aeric. Thanks a little.. it worked
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…