【问题标题】:Rails api Android Rest Client POST and PUT dont workRails api Android Rest Client POST 和 PUT 不起作用
【发布时间】:2015-02-16 03:41:00
【问题描述】:

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

【问题讨论】:

    标签: android ruby-on-rails json rest


    【解决方案1】:

    [App 354212 标准输出] ActionController::ParameterMissing(参数丢失或 值为空:用户):

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

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

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

    您缺少“用户”键

    【讨论】:

    • 是的!我确实有参数要求! :D 但我如何包装参数?
    • 没关系,我刚刚删除了“require(:user)”并解决了它:D 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    相关资源
    最近更新 更多