缺少参数Kairos API和Android Volley POST
我正在尝试使用安卓截击。然而,我不断得到错误1002,图像一个或多个必需的参数丢失。我尝试了两种方法将参数添加到JSON的主体中,我在代码中概述了这一点 这是我的密码-缺少参数Kairos API和Android Volley POST,android,android-volley,kairos-api,Android,Android Volley,Kairos Api,我正在尝试使用安卓截击。然而,我不断得到错误1002,图像一个或多个必需的参数丢失。我尝试了两种方法将参数添加到JSON的主体中,我在代码中概述了这一点 这是我的密码- public class MainActivity extends AppCompatActivity { RequestQueue requestQueue; @Override protected void onCreate(Bundle savedInstanceState) {
public class MainActivity extends AppCompatActivity {
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postRequestToEnrollPersonInGallery();
}
public void postRequestToEnrollPersonInGallery() {
final String appId = "3e12****";
final String appKey = "156e06fd782a3304f085f***********";
String mainUrl = "https://api.kairos.com/";
String enrollRequestUrl = "enroll";
requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, mainUrl + enrollRequestUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("Volley", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json");
params.put("app_id", appId);
params.put("app_key", appKey);
return params;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("image", "https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg");
params.put("subject_id", "12345");
params.put("gallery_name", "FirstGallery");
/*params.put("image", "\"url\":\"https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg\"");
params.put("subject_id", "\"subject_id\":\"12345\"");
params.put("gallery_name", "\"gallery_name\":\"FirstGallery\""); -- i tried this too*/
return params;
}
};
requestQueue.add(stringRequest);
}
}
public类MainActivity扩展了AppCompatActivity{
请求队列请求队列;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
postRequestToEnrollPersonInGallery();
}
public void postRequestToEnrollPersonInGallery()的请求{
最终字符串appId=“3e12****”;
最终字符串appKey=“156e06fd782a3304f085f**********”;
字符串mainUrl=”https://api.kairos.com/";
字符串enrollRequestUrl=“enroll”;
requestQueue=Volley.newRequestQueue(this);
StringRequest StringRequest=new StringRequest(Request.Method.POST,mainUrl+enrollRequestUrl,new Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Log.d(“截击”,回应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“截击”,error.toString());
}
}) {
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
参数put(“应用程序id”,应用程序id);
参数put(“应用程序键”,应用程序键);
返回参数;
}
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“图像”https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg");
参数put(“受试者id”、“12345”);
参数put(“画廊名称”、“第一画廊”);
/*参数put(“图像”,“url\:\”https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg\"");
参数put(“subject\u id”,“subject\u id\:\“12345\”);
参数put(“gallery\u name”,“gallery\u name\:“FirstGallery\”)——我也试过这个*/
返回参数;
}
};
添加(stringRequest);
}
}
您没有发布JSON
你也可以
1) 学习使用JsonObjectRequest
final JSONObject body = new JSONObject();
body.put(... , ...);
Request request = new JsonObjectRequest(url, body, ...);
2) 实际上发布一个JSON字符串
StringRequest request = new StringRequest(...) {
@Override
public byte[] getBody() throws AuthFailureError {
JSONObject params = new JSONObject();
params.put("image", "https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg");
params.put("subject_id", "12345");
params.put("gallery_name", "FirstGallery");
return params.toString().getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
你没有发布JSON 你也可以 1) 学习使用
JsonObjectRequest
final JSONObject body = new JSONObject();
body.put(... , ...);
Request request = new JsonObjectRequest(url, body, ...);
2) 实际上发布一个JSON字符串
StringRequest request = new StringRequest(...) {
@Override
public byte[] getBody() throws AuthFailureError {
JSONObject params = new JSONObject();
params.put("image", "https://s-media-cache-ak0.pinimg.com/originals/c6/c0/04/c6c004ec669d92faa36d8ff447884293.jpg");
params.put("subject_id", "12345");
params.put("gallery_name", "FirstGallery");
return params.toString().getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
};