我想使用表单urlencoded参数创建POST JSONObjectRequest。我怎样才能做到这一点?我试过下面的代码,但无济于事。在发布请求中发送表单urlencoded参数android volley
final String api = "http://api.url";
final JSONObject jobj = new JSONObject();
jobj.put("Username", "usr");
jobj.put("Password", "passwd");
jobj.put("grant_type", "password");
final JsonObjectRequest jor = new JsonObjectRequest(Request.Method.POST, api, jobj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_LONG).show();
//do other things with the received JSONObject
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error!", Toast.LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> pars = new HashMap<String, String>();
pars.put("Content-Type", "application/x-www-form-urlencoded");
return pars;
}
};
//add to the request queue
requestqueue.AddToRequestQueue(jor);
我得到一个400坏请求与API调用!我该如何解决它?
对不起,没有运气!仍然是400错误! – user4071017 2014-09-23 16:29:38
这意味着您所做的请求不正确。检查API文档和您发送的内容,并确保它是正确的。你可以发送你的请求与一些其他的东西,如邮递员或RESTClient,并告诉我结果? – mmlooloo 2014-09-23 16:40:13
一切工作与邮递员:( – user4071017 2014-09-23 16:45:38