我已经尝试了每种方法都可用于stackoverflow但结果保持不变。使用Volley发送POST参数和标题时的代码响应代码400
I want to send params and also api key in the Authorization field of header using volley.
这是代码片段。
holder.comment_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String POSTCOMMENT_URL = Utility.getIPADDRESS()+"comments";
volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, POSTCOMMENT_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("Response: " , response.toString());
Toast.makeText(myApplication.getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(myApplication.getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
}
}){
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Authorization", getApiKey());
return params;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("pid", "39");
params.put("comment", "mobile testing");
return params;
}
};
requestQueue.add(jsonObjectRequest);
}
});
我得到这个错误:
[5333] BasicNetwork.performRequest: Unexpected response code 400 for http://www.XXXXXXXXXXX.com/comments
我已经测试使用先进的REST客户端的端点,它的工作原理,并与先前添加的注释返回的JSONObject。
下面是结果:
{
"error": false
"message": "comment added successfully added!"
"comment_id": "12"
"user_id": "12"
"pid": "39"
"comment": "testing for stackoverflow"
"date of comment": "2016-07-30 13:49:08"
}
400是什么意思? – greenapps
由于格式错误,服务器无法理解请求。总之_Bad网络request_。 –
'String POSTCOMMENT_URL = Utility.getIPADDRESS()+“comments”;'。那应该是'String POSTCOMMENT_URL = Utility.getIPADDRESS()+“/ comments”;'?如果不是,那么函数名称会令人困惑。 – greenapps