Android Question Posting Json data to Project Oxford

EdmundHerbert

Member
Licensed User
Longtime User
H can anyone help me I am trying to do the following:
----------------------------------------------------------------------------------------------------
PUT face/v0/persongroups/1
Content-Type: application/json
Host: api.projectoxford.ai
Content-Length: 129
Ocp-Apim-Subscription-Key: ••••••••••••••••••••••••••••••••

{
"name":"sample_group",
"userData":"user-provided data attached to the person group"
}
----------------------------------------------------------------------------------------------------
This is what they require:

Face API
PersonGroup - Create a PersonGroup
Creates a new person group with a user-specified ID.

A person group is one of the most important parameters for the Identification API. The Identification searches person faces in a specified person group.

Http Method
PUT
Request parameters
personGroupId
Request headers
Content-Type


Ocp-Apim-Subscription-Key
Request body
JSON fields in request body:

name
String
Person group display name. The maximum length is 128.
userData

String

User-provided data attached to the person group. The size limit is 16KB.

---------------------------------------------------------------------------------------------------



My code looks like this:

Sub CreateGroup()

Try


Dim GroupMap As Map
GroupMap.Initialize
GroupMap.Put("name","sample_group")
GroupMap.Put("userData","sample group created")

Dim JSONGenerator As JSONGenerator
JSONGenerator.Initialize(GroupMap)


' JSONGenerator puts GroupMap.Put("userData","sample group created") first

' then GroupMap.Put("name","sample_group") last this is wrong sequence

Dim JSONstring As String
JSONstring = JSONGenerator.ToString

JSONstring = "{ ""name"":""sample_group"",""userData"":""sample group created"" }"

I tried hard coding json string


Dim job2 As HttpJob

job2.Initialize("job2",Me) ' Create Person Group
job2.PostString("https://api.projectoxford.ai/face/v0/persongroups/1",JSONstring)
job2.GetRequest.SetHeader("Content-Type","application/json")
job2.GetRequest.SetHeader("Ocp-Apim-Subscription-Key","xxxxxxxxxxxxxxxxxxxxxxxxxxx")

Catch
ToastMessageShow("Comms with Oxford failed",False)
End Try
End Sub

It does not work am I doing something stupid

Regards

Edmund
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [code]code here...[/code] tags when posting code.

The order of fields in a json string is not important.

You are sending a POST request instead of PUT request.

You need to modify HttpJob and add these two methods:
B4X:
'Sends a PUT request with the given data as the post data.
Public Sub PutString(Link As String, Text As String)
   PutBytes(Link, Text.GetBytes("UTF8"))
End Sub

'Sends a PUT request with the given string as the post data
Public Sub PutBytes(Link As String, Data() As Byte)
   req.InitializePut2(Link, Data)
   CallSubDelayed2(HttpUtils2Service, "SubmitJob", Me)
End Sub
You should then call PutString.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…