gpt4 book ai didi

android - Volley Post JsonArrayRequest 给客户端错误,同时在 postman 工作中测试相同的输入

转载 作者:行者123 更新时间:2023-11-29 02:22:45 25 4
gpt4 key购买 nike

我正在使用 Volley 向 HubSpot 发送 Post 请求,当我发送 JSON Array 对象请求时它给出了 com.android.volley.ClientError 当我记录给 JSONArrayRequest 的输入并将它们复制到 postman 时它工作.

什么都试过了,还是一样的错误。

这是我记录时 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);

关于android - Volley Post JsonArrayRequest 给客户端错误,同时在 postman 工作中测试相同的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54275174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com