Rails api Android Rest客户端POST和PUT不起作用

时间:2015-02-16 03:41:00

标签: android ruby-on-rails json rest

我的Android客户端应用程序正在发送一个put请求来编辑用户,它会进入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类中的内容:

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
"

1 个答案:

答案 0 :(得分:0)

[App 354212 stdout] ActionController :: ParameterMissing(param缺失或者 value为空:user):

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

您需要将参数包装在" user"密钥,即你的传入参数应该像

{"用户" => {" nivel" =>" 1"," name" =>" aaa",...," id& #34; = GT;" 2"}}

你遗失了#34;用户"键