Java 如何使用volley将json对象发布到Watson Studio API
我想使用Volley将这种类型的数据从android应用程序发送到IBM Watson Studio中部署的机器学习模型APIJava 如何使用volley将json对象发布到Watson Studio API,java,android,json,android-volley,watson-studio,Java,Android,Json,Android Volley,Watson Studio,我想使用Volley将这种类型的数据从android应用程序发送到IBM Watson Studio中部署的机器学习模型API { "fields":[ "Gender", "Age", "EstimatedSalary" ], "values":[ Gender, Age, EstimatedSalary ] } 我一直在为这个需求创建JSON对象并将其发送到API 我无法继续创建JSON对象 p
{
"fields":[
"Gender",
"Age",
"EstimatedSalary"
],
"values":[
Gender,
Age,
EstimatedSalary
]
}
我一直在为这个需求创建JSON对象并将其发送到API
我无法继续创建JSON对象
protectedmap getParams()抛出AuthFailureError{
Map params=新的HashMap();
参数put(“字段”、“性别”);
参数put(“价值观”,性别);
返回super.getParams();
}
我知道那不行。但是我不知道如何进行。在构建JSONArray之后,我得到了答案。如果有人有同样的问题,这里是答案
JSONObject JSONObject=new JSONObject();
试一试{
JSONArray jsonArrayFields=新的JSONArray();
jsonArrayFields.put(“年龄”);
jsonArrayFields.put(“性别”);
jsonArrayFields.put(“EstimatedSalary”);
jsonObject.put(“结果”,jsonArrayFields);
JSONArray jsonArrayValues=新的JSONArray();
jsonArrayValues.put(年龄);
jsonArrayValues.put(性别);
jsonArrayValues.put(估计量);
JSONArray jsonArrayValues2D=新的JSONArray();
jsonArrayValues2D.put(jsonArrayValues);//在另一个数组中围绕jsonArrayValues
put(“值”,jsonArrayValues2D);
}捕获(JSONException e){
e、 printStackTrace();
}
//最后发送到API
JsonObjectRequest\u json=新的JsonObjectRequest(request.Method.POST、url、jsonObject、,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//论成功应对
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
e(“Error:,Error.getMessage());
}
});
//将请求对象添加到要执行的队列中
RequestQueue=Volley.newRequestQueue(this);
添加(请求_json);
您的字段
和值
是json数组
,值是否会保持不变?@Swati谢谢,是的,我正在尝试构建json数组。不要担心这些值,它们只是变量的名称