繁体   English   中英

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

[英]Rails api Android Rest Client POST and PUT dont work

我的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
"

[App 354212 stdout] ActionController :: ParameterMissing(param丢失或值为空:user):

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

你需要在“用户”键下包装你的参数,即你的传入参数应该是

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

你缺少“用户”键

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM