我使用Volley將POST用戶名,密碼和電子郵件用於註冊目的。我參考了This tutorial。並按照本教程中的定義工作。Volley Post請求不適用於API
這裏是我的代碼:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
URL_string.api_url,null
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("Result", "Success");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Result", "UnSuccess");
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
Log.i("Result_Params",emailValue + passwordValue + nameValue);
params.put("email", emailValue);
params.put("password", passwordValue);
params.put("name", nameValue);
return super.getParams();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return super.getHeaders();
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(jsonObjectRequest);
但我的代碼顯示了這個錯誤:
[368] BasicNetwork.performRequest: Unexpected response code 422 for http://myapi.com/api/register
錯誤422的API定義爲:
{
"message": "422 Unprocessable Entity",
"errors": {
"email": [
"The email field is required."
],
"password": [
"The password field is required."
],
"name": [
"The password field is required."
]
},
"status_code": 422
}
我想:
改變JsonObjectRequest到StringRequest,
在URL_string.api_url的地方原來的URL,
但反應仍然是一樣的。這是因爲我沒有爲Volley圖書館製作任何課程嗎?我從Androidhive.info也這樣做,但失敗了!
現在我不知道如何從這一步開始,而登錄getParams會記錄用戶輸入的名稱,電子郵件和pw的正確值,但POST操作仍然不起作用。請幫忙! 在此先感謝!
你應該返還paramters不超價值 – silverFoxA
是的,它worked.Thank你這麼多@silverFoxA – Swr7der