Android Question JSON Post, quotes in wrong place

ChipC

Member
Licensed User
Hi everyone;

I'm having a rough time posting a particular JSON object. The JSONParser works perfectly, but when the resulting map is posted, the quotes for the array type and JSON type end up in the wrong place, breaking the object.

B4X:
Sub btnSend_Click
    '****************************************************************************
    'Send a POST request

    Dim Map1 As Map
    Dim JSON As JSONParser
    Dim Jout As JSONGenerator
    Dim jsonString As String
    
    JSON.Initialize($"{"address":["123412341234123"],
  "senderAddress":"wombat",
  "outboundSMSTextMessage":{"message":"OFF"},
  "ClientCorrelator":"123456",
      "senderName":"AFTestClient"}
    "$)

    
    Map1.Initialize   
    Map1 = JSON.NextObject
    
    Jout.Initialize(Map1)
    jsonString=Jout.ToString
    Log(jsonString)
    
    job1.PostMultipart($"HTTP://dweet.io/dweet/for/WombatStatus"$, Map1, Null)   
    job1.GetRequest.SetHeader("Content-Type", "application/json")

The log result is here:

{"address":["123412341234123"],"senderAddress":"wombat","outboundSMSTextMessage":{"message":"OFF"},"ClientCorrelator":"123456","senderName":"AFTestClient"}

JobName = Job1, Success = true

{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"WombatStatus","created":"2019-12-21T13:11:04.980Z","content":

{"address":"[123412341234123]","senderAddress":"wombat","outboundSMSTextMessage":"{message=OFF}","ClientCorrelator":123456,"senderName":"AFTestClient"},"transaction":"53168ea6-5309-4bda-80e8-e02a71f1bbb5"}}

The array object has the quotes outside of the bracket, and the embedded JSON message object no longer surrounds the values with quotes.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Several mistakes here.

1:
B4X:
 Map1.Initialize   
    Map1 = JSON.NextObject
You are initializing a new map and then assigning a different map to the same variable.
Correct code:
B4X:
Dim Map1 As Map = JSON.NextObject

2. I don't understand what you are doing with the json string. Why do you parse it and then generate it again?

3. Job.PostMultipart should be used to sent multipart requests. This is not the case here.

Correct code:
B4X:
'it is usually a mistake to build the string like this
Dim s As String = $"{"address":["123412341234123"],
  "senderAddress":"wombat",
  "outboundSMSTextMessage":{"message":"OFF"},
  "ClientCorrelator":"123456",
      "senderName":"AFTestClient"}
    "$
Job.PostString(..., s)
 
Upvote 0

ChipC

Member
Licensed User
Thank you Erel;

  1. I changed the initialization, but the result was the same.
  2. The purpose of generating and parsing the json string was simply diagnostic, I wanted to see if the string was being built properly.
I originally built and sent as a string:
B4X:
    jsonString=$"{"address":["123412341234123"],
  "senderAddress":"wombat",
  "outboundSMSTextMessage":{"message":"OFF"},
  "ClientCorrelator":"123456",
      "senderName":"AFTestClient"}
    "$

job1.PostString($"HTTP://dweet.io/dweet/for/WombatStatus"$, jsonString)
job1.GetRequest.SetHeader("Content-Type", "application/json")

But this contained escaped quotes as shown here after the "content" tag:

{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"WombatStatus","created":"2019-12-22T11:44:10.297Z","content":{"{\"address\":":{"\"123412341234123\"],\n \"senderAddress\":\"wombat\",\n \"outboundSMSTextMessage\":{\"message\":\"OFF\"},\n \"ClientCorrelator\":\"123456\",\n\t \"senderName\":\"AFTestClient\"}\n":""}},"transaction":"e657cded-0d76-40a5-a826-b254f00f35e9"}}

Posting a map with multipart produces better results, but still has errors with nested json objects shown at the "outboundSMSTextMessage" tag and "address" tag:

{"this":"succeeded","by":"dweeting","the":"dweet","with":{"thing":"WombatStatus","created":"2019-12-22T12:40:52.893Z","content":{"address":"[123412341234123]","senderAddress":"wombat","outboundSMSTextMessage":"{message=OFF}","ClientCorrelator":123456,"senderName":"AFTestClient"},"transaction":"7b02d298-0d56-4d4e-904a-b16f05da2b6e"}}
 
Upvote 0

ChipC

Member
Licensed User
Ah ha, found it! I need to set

B4X:
    job1.GetRequest.SetContentType("application/json")

rather than

B4X:
    job1.GetRequest.SetHeader("Content-Type", "application/json")

Then posting the string works properly.
 
Upvote 0
Top