2016-07-01 202 views
0

我使用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 
} 

我想:

  1. 改變JsonObjectRequest到StringRequest,

  2. 在URL_string.api_url的地方原來的URL,

但反應仍然是一樣的。這是因爲我沒有爲Volley圖書館製作任何課程嗎?我從Androidhive.info也這樣做,但失敗了!

現在我不知道如何從這一步開始,而登錄getParams會記錄用戶輸入的名稱,電子郵件和pw的正確值,但POST操作仍然不起作用。請幫忙! 在此先感謝!

+0

你應該返還paramters不超價值 – silverFoxA

+0

是的,它worked.Thank你這麼多@silverFoxA – Swr7der

回答

2

你需要返回你創建的params對象,而不是返回父對象的方法(這是空的)。

@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 params; //not super.getParams(); 
} 

正如pablobu指出,這也適用於您的getHeaders():

@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 headers; //not super.getHeaders(); 
} 
+1

getHeaders()所需的相同更改返回 – pablobu

+0

保存我的一天......非常感謝:) DaveChen和@pablobu – Swr7der