Microsoft认知服务面临Java创建人员组的API错误
此代码用于使用Java创建人员组Microsoft认知服务面临Java创建人员组的API错误,java,microsoft-cognitive,Java,Microsoft Cognitive,此代码用于使用Java创建人员组 import java.net.URI; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URIBuilder; imp
import java.net.URI;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class JavaSample
{
public static void main(String[] args)
{
HttpClient httpclient = HttpClients.createDefault();
try
{
URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/face/v1.0/persongroups/personGroupId");
URI uri = builder.build();
HttpPut request = new HttpPut(uri);
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", "mysubscriptionkey");
// Request body
StringEntity reqEntity = new StringEntity("{body}");
request.setEntity(reqEntity);
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null)
{
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
当我运行此代码时,我会考虑此错误:
{“error”:{“code”:“BadArgument”,“message”:“请求正文无效。”}
我能做些什么来修理它。我没有在internet上找到使用java创建个人组的示例,因此无法修复它。正如错误所示,HTTP请求正文的格式错误为
{body}
请查看页面,在标有请求正文的部分下。您需要提供名称,并且可以选择添加用户数据
StringEntity reqEntity = new StringEntity("{" +
"\"name\":\"group1\"," +
"\"userData\":\"user-provided data attached to the person group\""+
"}")
您可以在GitHub页面上找到一些其他Java示例