Volley Post JsonArrayRequest 给客户端错误,同时在邮递员工作中测试相同的输入
Volley Post JsonArrayRequest gives client error, while testing same inputs in postman works
我正在使用 Volley 向 HubSpot 发送一个 Post 请求,当我发送 JSON 数组对象来请求它时,它给出了 com.android.volley.ClientError 并且当我记录提供给JSONArrayRequest 并将它们复制到邮递员即可。
什么都试过了,还是一样的错误。
这是我记录时 jsonarrayobj 的样子。
[
{
"name": "subject",
"value": "Custom Subject"
},
{
"name": "email",
"value": "demo@demo.com"
},
{
"name": "site",
"value": "Site 3"
},
{
"name": "device",
"value": "demo-0005"
},
{
"name": "content",
"value": "This is content"
},
{
"name": "hs_pipeline",
"value": ""
},
{
"name": "hs_pipeline_stage",
"value": ""
}
]
和我的代码:
JSONArray jsonArray=new JSONArray();
Map<String,String> firstnameobj=new HashMap<String,String>();
for (int i=0;i<7;i++) {
firstnameobj.put("name", cool.get(i));
firstnameobj.put("value", data[i]);
jsonArray.put(new JSONObject(firstnameobj));
firstnameobj.clear();
}
String url="https://api.hubapi.com/crm-objects/v1/objects/tickets?hapikey=MY-API-KEY";
JsonArrayRequest jsonArrayRequest=new
JsonArrayRequest(Request.Method.POST, url,jsonArray, new
Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Toast.makeText(getContext(),"Updated
",Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),"Error Adding Ticket
"+error.toString(),Toast.LENGTH_LONG).show();
Log.e("error",error.toString());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> customHeader=new HashMap<String, String>();
customHeader.put("Content-Type","application/json");
return customHeader;
}
};
requestQueue.add(jsonArrayRequest);
如果有人遇到同样的问题,我通过以下方式解决了它:
StringRequest request=new StringRequest(Request.Method.POST, url, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getContext(),"Created !",Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),"Error Adding Ticket",Toast.LENGTH_LONG).show();
}
}){
@Override
public byte[] getBody() throws AuthFailureError {
return jsonArray.toString().getBytes();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> customHeader=new HashMap<String, String>();
customHeader.put("Content-Type","application/json");
return customHeader;
}
};
requestQueue.add(request);
我正在使用 Volley 向 HubSpot 发送一个 Post 请求,当我发送 JSON 数组对象来请求它时,它给出了 com.android.volley.ClientError 并且当我记录提供给JSONArrayRequest 并将它们复制到邮递员即可。
什么都试过了,还是一样的错误。
这是我记录时 jsonarrayobj 的样子。
[
{
"name": "subject",
"value": "Custom Subject"
},
{
"name": "email",
"value": "demo@demo.com"
},
{
"name": "site",
"value": "Site 3"
},
{
"name": "device",
"value": "demo-0005"
},
{
"name": "content",
"value": "This is content"
},
{
"name": "hs_pipeline",
"value": ""
},
{
"name": "hs_pipeline_stage",
"value": ""
}
]
和我的代码:
JSONArray jsonArray=new JSONArray();
Map<String,String> firstnameobj=new HashMap<String,String>();
for (int i=0;i<7;i++) {
firstnameobj.put("name", cool.get(i));
firstnameobj.put("value", data[i]);
jsonArray.put(new JSONObject(firstnameobj));
firstnameobj.clear();
}
String url="https://api.hubapi.com/crm-objects/v1/objects/tickets?hapikey=MY-API-KEY";
JsonArrayRequest jsonArrayRequest=new
JsonArrayRequest(Request.Method.POST, url,jsonArray, new
Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Toast.makeText(getContext(),"Updated
",Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),"Error Adding Ticket
"+error.toString(),Toast.LENGTH_LONG).show();
Log.e("error",error.toString());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> customHeader=new HashMap<String, String>();
customHeader.put("Content-Type","application/json");
return customHeader;
}
};
requestQueue.add(jsonArrayRequest);
如果有人遇到同样的问题,我通过以下方式解决了它:
StringRequest request=new StringRequest(Request.Method.POST, url, new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getContext(),"Created !",Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),"Error Adding Ticket",Toast.LENGTH_LONG).show();
}
}){
@Override
public byte[] getBody() throws AuthFailureError {
return jsonArray.toString().getBytes();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> customHeader=new HashMap<String, String>();
customHeader.put("Content-Type","application/json");
return customHeader;
}
};
requestQueue.add(request);