2015-10-14 18 views
0

我有一個部署在Tomcat服務器上的REST服務。該服務有一個POST方法與端點的createUser,和下面的方法:具有鍵值參數的排除帖子

@Path("/myService") 
public class MyClass { 
    @POST 
    @Path("/createUser") 
    public Response createUser(@Context UriInfo info) { 
     String user = info.getQueryParameters().getFirst("name"); 
     String password = info.getQueryParameters().getFirst("password"); 

     if (user == null || password == null) { 
      return Response.serverError().entity("Name and password cannot be null").build(); 
     } 

    //do stuff... 
    return Response.ok().build() 
    } 

通過調用此方法了SoapUI一切工作順利進行。我部署我的服務器併發送一個帖子到這個(http://my_IP:8080/myApplication/myService/createUser)。

現在我正試圖從我的Android應用程序調用這個。我正在嘗試使用Volley庫。第一個測試是使用GET請求(與我的tomcat中的其他端點),並沒有問題。但是,當我嘗試調用此端點並創建一個用戶時,該方法在Tomcat中被觸發,但未檢索到任何參數(即用戶和密碼爲空)。這是我的Android代碼:

private void sendPostRequest(final String user, final String password) { 
    final Context context = getApplicationContext(); 

    RequestQueue mRequestQueue = Volley.newRequestQueue(this); 

    final String URL = "http://my_IP:8080/myApplication/myService/createUser"; 

    StringRequest strRequest = new StringRequest(Request.Method.POST, URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show(); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show(); 
       } 
      }) { 
     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("name", user); 
      params.put("password", password); 
      return params; 
     } 
    }; 
    mRequestQueue.add(strRequest); 
} 

我在做什麼錯了?我也試圖與一個JSONObjects改變Android的調用(離開REST服務器完好)用下面的代碼:

private void sendPostRequest (final String user, final String password) { 
     final Context context = getApplicationContext(); 
     final String URL = "http://my_IP:8080/myApplication/myService/createUser"; 
     RequestQueue mRequestQueue = Volley.newRequestQueue(this); 

     Map<String, String> postParam= new HashMap<String, String>(); 
     postParam.put("name", user); 
     postParam.put("password", password); 

     JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, 
      URL, new JSONObject(postParam), 
      new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         Toast.makeText(context, response.toString(), Toast.LENGTH_SHORT).show(); 
        } 
       }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show(); 
      } 
     }) { 

      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       HashMap<String, String> headers = new HashMap<String, String>(); 
       headers.put("Content-Type", "application/json"); 
       headers.put("charset", "utf-8"); 
       return headers; 
      } 

     }; 
     mRequestQueue.add(jsonObjReq); 
    } 

任何幫助非常感謝。謝謝!

更新:由於來自@ dev.bmax的提示而解決。我不得不修改我的REST服務器,並獲得全要求(不僅是URIInfo):

@Path("/myService") 
public class MyClass { 
    @Context Request request; 
    @Context UriInfo info; 

    @POST 
    @Path("/createUser") 
    public Response createUser() { 
     HttpRequestContext req = (HttpRequestContext) request; 

     String params = req.getEntity(String.class); 
     HashMap<String, String> props = Helper.unparseEntityParams(params); 

     if (props.get("username") == null || props.get("password") == null) { 
      return Response.serverError().entity("Name and password cannot be null").build(); 
     } 

     //do stuff... 
     return Response.ok().build() 
    } 
} 
+0

你檢查什麼新的JSONObject(postParam)的價值? –

+0

檢查鏈接.http://stackoverflow.com/questions/23220695/send-post-request-with-json-data-using-volley –

回答

0

後端的示例代碼提取PARAMS與getQueryParameters UriInfo()方法。這對GET方法來說很好。 但是,如果您使用相同的代碼嘗試提取POST請求的參數,那麼它將不起作用,因爲它們不包含在URL中。 取而代之的是你應該使用這樣的:

String userName = request.getParameter("name"); 
String password = request.getParameter("password");