HttpUtils2Service.bas and application/json

Ohanian

Active Member
Licensed User
Longtime User
I'm trying to post some json data to a web service, but it seems something is wrong and the web service doesn't accept the data.

My code :

B4X:
Dim job1 As HttpJob 

job1.Initialize("Job1", Me)
   
Dim objMap As Map: objMap.Initialize
   
objMap.Put("id",123)
objMap.Put("params","XXX,YYY")
objMap.Put("method","apiLogin")

Dim objJSon As JSONGenerator: objJSon.Initialize(objMap)
 
 job1.PostString("https://SITEADDRESS/api/jsonrpc/1_0", objJSon.ToPrettyString(2))


And the error from server :

B4X:
Catchable fatal error: Argument 1 passed to Zend_Json_Server_Request::setOptions() must be an array, null given, called in ...

The server uses https so I've changed this line:

B4X:
hc.InitializeAcceptAll("hc")

But how to set ContentType to application/json ?
 

Ohanian

Active Member
Licensed User
Longtime User
you can modify this sub:
B4X:
Public Sub GetRequest As HttpRequest
   Return req
End Sub
And add:
B4X:
req.SetContentType(...)

tanx Erel,
got the same error, i think i have to set ContentType on POST method.
i have to post some json data, and get the json result.
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
Maybe this error is not related to the content type. Adding this code will set the content type of all requests.

Here's the working jquery code :

B4X:
var params = { "method": "apiLogin", "params": ["XXX", "YYY"], "id": 1891274114 };

$.ajax({ url: 'https://SITEADDRESS/api/jsonrpc/1_0', type: 'POST', contentType: "application/json", datatype: "json", traditional: true, data: JSON.stringify(params), success: function(data) { window.alert(data); }, error: function(event, request, settings) { window.alert('Error' + ' : ' + settings); }, timeout: 20000 });

But I couldn't make the equivalent code in B4A, any help plz?
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
You should use:
B4X:
objMap.Put("params", Array As String("XXX", "YYY"))

Log the generated JSON string and compare it to the working one.

tanx again,
here's the result !

objMap.Put("params",Array As String("ohanian", "ceOn2qeHXBjKdWM8E1Ks"))

HttpUtils.PostString("POST JOB1", "https://SITEADDRESS/api/jsonrpc/1_0", objJSon.ToPrettyString(4), ContentType)

{
"id": 1891274114,
"method": "apiLogin",
"params": "[I@410c2718"
}


Does anybody else have/had this problem?
It shouldn't be so hard, but i don't know where the problem is.
 
Upvote 0

Ohanian

Active Member
Licensed User
Longtime User
My mistake. It should be:
B4X:
Dim l As List
l.Initialize
l.AddAll(Array As String("ohanian", "ceOn2qeHXBjKdWM8E1Ks"))
objMap.Put("params",l)

tanx, it's ok now.
 
Upvote 0

macnlkc

Member
Licensed User
Longtime User
you can modify this sub:
B4X:
Public Sub GetRequest As HttpRequest
   Return req
End Sub
And add:
B4X:
req.SetContentType(...)

I could never get the code above to work properly. However, if I place the code below it works every time...

'Send a POST request
job2.Initialize("Job2", Me)

Dim objMap As Map: objMap.Initialize

objMap.Put("username","me")
objMap.Put("phone","5551212")
objMap.Put("emailAddress","[email protected]")
objMap.Put("zip", 78701)

Dim objJSon As JSONGenerator: objJSon.Initialize(objMap)

job2.PutString("http://10.0.1.54:8080/security/register", objJSon.ToPrettyString(2))
job2.GetRequest.SetContentType("application/json")
Notice that the job2.GetRequest.SetContentType is always at the end. If I pull it from here and put it in the GetRequest Sub it throws an error every time. Any thoughts?

Also, how do I capture the returned JSON object?

I appreciate all the help. Starting to like B4A!
 
Upvote 0
Top