我想使用表單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