当我尝试使用android volley发布请求时,它返回错误“意外响应代码400”,当我在git bash中运行curl命令时,它是成功的。 我的curl命令是
curl -X POST \
https://sam.vuoto.us/sapi/employee/activity \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-H 'token: eyJhbGciOiJSUzI1NiIsImtpZCI6IjYwZTQxMjczMzMwYTg2ZmRjMjhlMjgzMDVhNDRkYzlhODgzZTI2YTciLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vd2F0ZXItc29hbC1zb2NpZXR5IiwiYXVkIjoid2F0ZXItc29hbC1zb2NpZXR5IiwiYXV0aF90aW1lIjoxNTYxNTgyMTEzLCJ1c2VyX2lkIjoiU1U5aWVnRGhyM2I5cjNnQ1VaelhsOWVRakVKMiIsInN1YiI6IlNVOWllZ0RocjNiOXIzZ0NVWnpYbDllUWpFSjIiLCJpYXQiOjE1NjMzODQxNjIsImV4cCI6MTU2MzM4Nzc2MiwiZW1haWwiOiJhbHZpbkB2dW90b2xhYnMuY29tIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJmaXJlYmFzZSI6eyJpZGVudGl0aWVzIjp7ImVtYWlsIjpbImFsdmluQHZ1b3RvbGFicy5jb20iXX0sInNpZ25faW5fcHJvdmlkZXIiOiJwYXNzd29yZCJ9fQ.kJi_tVNrc-2Mf4uoVOYuURUpBKs72H3Sr7TyFkO-b5vSzBbv1iBu4PIYkHd7bNZEVxhNfyAg2zNFcwQuQPG9TdSjZ0fPGF1mzdrkOMsKjoBPQIZK2coG24oLoKkkuXtxaWMXe1BlNpLhPcWM9BBoZ0Os2PmXf-PIgGfzrPzQ1nBOt6zAD5rv2H4WEntIA3tRLSHgSrFba0taVjD8Do1drIm7ocfnbDPHKMIeJRj60uzaEj5WjbbaB_MXXpjClVgUR1XF25_QDMA4F1Ok2odTrT1nMofgM_jMVn0s_K2ljU2zCoIKQwe477tx1I4zLfgIyBXI8FWhg03-e08QxvD-Zw' \
-d '{
"title":"Test Activity 4",
"description":"Test Description",
"activityStreamId": 1401
}'
此命令正常工作。
但是当我在android中尝试此操作时,它会返回错误
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, serverurl,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d(TAG, "other class: "+response);
acitvityprog.setVisibility(View.GONE);
JSONObject employee=response.getJSONObject("success");
}
catch (JSONException e) {
e.printStackTrace();
acitvityprog.setVisibility(View.GONE);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
acitvityprog.setVisibility(View.GONE);
}
}
){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<String,String>();
params.put("title",titles);
params.put("description",desc);
params.put("activityStreamId","1401");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers=new HashMap<String,String>();
headers.put("Content-Type","application/json");
headers.put("cache-control","no-cache");
headers.put("token",token);
return headers;
}
};
singletonclasshttp.getInstance(getActivity()).addtorequestque(jsonObjectRequest);