Rails api Android Rest Client POST 和 PUT 不工作

Rails api Android Rest Client POST and PUT dont work

我的 Android 客户端应用程序正在发送一个编辑用户的放置请求,它到达 API 但它没有修改任何内容! 这是 PUT 的客户端代码:

 @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(URL_connect, ServiceHandler.PUT, params);

        Log.d("Response: ", "> " + jsonStr);
        return null;
    }

这是 PUT 的 ServiceHandler class 中的内容:

else if(method== PUT){
            // appending params to url
            HttpPut edit = new HttpPut(url);
            if (params != null) {
                edit.setEntity(new UrlEncodedFormEntity(params));
            }


            httpResponse = httpClient.execute(edit);
        }


        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

这是来自 API 的请求日志:

[App 354212 stdout] I, [2015-02-15T22:02:14.753191 #354212]  INFO -- : Started PUT "/users/2" for 127.3.16.129 at 2015-02-15 22:02:14 -0500
[ 2015-02-15 22:02:14.7659 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout] I, [2015-02-15T22:02:14.765770 #354212]  INFO -- : Processing by UsersController#update as HTML
[ 2015-02-15 22:02:14.7687 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout] I, [2015-02-15T22:02:14.766095 #354212]  INFO -- :   Paramet
ers: {"nivel"=>"1", "name"=>"aaa", "lastname"=>"Bibb", "email"=>"ccc", "pass"=>"
ddde", "id"=>"2"}
[ 2015-02-15 22:02:14.8232 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout] I, [2015-02-15T22:02:14.823029 #354212]  INFO -- : Completed
 400 Bad Request in 54ms
[ 2015-02-15 22:02:14.8268 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout] F, [2015-02-15T22:02:14.826736 #354212] FATAL -- :
[ 2015-02-15 22:02:14.8269 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout] ActionController::ParameterMissing (param is missing or the
value is empty: user):
[ 2015-02-15 22:02:14.8269 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout]   app/controllers/users_controller.rb:74:in `user_params'
[ 2015-02-15 22:02:14.8269 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout]   app/controllers/users_controller.rb:46:in `block in update
'
[ 2015-02-15 22:02:14.8269 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout]   app/controllers/users_controller.rb:45:in `update'
[ 2015-02-15 22:02:14.8269 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout]
[ 2015-02-15 22:02:14.8270 342660/7ff11c60b700 Pool2/Implementation.cpp:1274 ]:
[App 354212 stdout]
- - - [15/Feb/2015:22:02:14 -0500] "PUT /users/2 HTTP/1.1" 400 - "-" "Apache-Htt
pClient/UNAVAILABLE (java 1.4)"
10.182.141.52 - - [15/Feb/2015:22:10:44 -0500] "HEAD / HTTP/1.1" 200 - "-" "Ruby
"
10.182.141.52 - - [15/Feb/2015:22:10:44 -0500] "HEAD / HTTP/1.1" 200 - "-" "Ruby
"

[App 354212 stdout] ActionController::ParameterMissing(参数缺失或 值为空:用户):

我相信你的控制器中有 params.require(:user).permit

您需要将您的参数包装在 "user" 键下,即您的传入参数应该像

{"user" => {"nivel"=>"1", "name"=>"aaa",..., "id"=>" 2"}}

您缺少 "user" 密钥