Java 轨道/设计-不可加工实体

Java 轨道/设计-不可加工实体,java,android,ruby-on-rails,Java,Android,Ruby On Rails,我正在尝试使用post请求(JSON)来创建用户。使用curl的示例效果很好。下面是curl命令: curl -X POST -H "Content-Type: application/json" 'http://localhost:3000/users.json' -d '{ "user": {"email": "e@f.com", "password": "foobar", "password_confirmation": "foobar"}}' 卷曲输出: Started POST "/

我正在尝试使用post请求(JSON)来创建用户。使用curl的示例效果很好。下面是curl命令:

curl -X POST -H "Content-Type: application/json" 'http://localhost:3000/users.json' -d '{ "user": {"email": "e@f.com", "password": "foobar", "password_confirmation": "foobar"}}'
卷曲输出:

Started POST "/users.json" for 127.0.0.1 at 2012-01-28 16:19:10 -0800
  Processing by Devise::RegistrationsController#create as JSON
  Parameters: {"user"=>{"email"=>"e@f.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
但是,当我使用java中的http库尝试请求时,我得到一个错误:

Started POST "/users.json" for 192.168.1.88 at 2012-01-28 16:47:56 -0800
  Processing by Devise::RegistrationsController#create as JSON
  Parameters: {"user"=>"{\"password_confirmation\":\"secret\",\"password\":\"secret\",\"email\":\"a@foo.com\"}"}
Completed 422 Unprocessable Entity in 73ms (Views: 3.0ms | ActiveRecord: 0.0ms)
用于创建请求的代码:

            RestClient client = new RestClient(SIGNUP_URL);
            JSONObject jObj = new JSONObject();
            JSONObject jsonUserObj = new JSONObject();

            try {
                jObj.put("email", txtUserName.getText().toString());
                jObj.put("password", txtPassword.getText().toString());
                jObj.put("password_confirmation", txtPassword.getText().toString());

                jsonUserObj.put("user", jObj.toString());
            } catch (JSONException e1) {
                e1.printStackTrace();
            }

            client.setJSONParams(jsonUserObj);
            try {
                client.Execute(RequestMethod.JSON_POST);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                    e.printStackTrace();
            } catch (Exception e) {
                    e.printStackTrace();
            }   
            if (client.getResponseCode() == HttpStatus.SC_OK) {
               // Good response
               try {
                   jObj = new JSONObject(client.getResponse());
                   System.out.println("Signup Successful");
               } catch (JSONException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
               }
            }
        }
以下是为执行调用的函数:

    HttpPost request = new HttpPost(url);
            request.addHeader("Content-Type", "application/json");
            request.addHeader("Accept", "application/json");
            StringEntity s = new StringEntity(jsonParams.toString(), HTTP.UTF_8);
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            request.setEntity(s);
            executeRequest(request, url);
            break;

如果查看Java库请求的日志,您会发现用户的内容只是一个大的转义字符串,而不是嵌套的哈希

当您查看创建请求的代码时,您有:

jsonUserObj.put("user", jObj.toString());
我猜如果你把它改成:

jsonUserObj.put("user", jObj);

这将是可行的,因为这样它将把作为jObj的散列与“user”关联起来,而不是将该对象的字符串与“user”关联起来。对我来说,相同的问题来自于注册控制器中的某个错误,因此我将与您共享我的问题,您可以进行必要的更改以满足您的需要

class RegistrationsController < Devise::RegistrationsController
  skip_before_filter :verify_authenticity_token,
                     :if => Proc.new { |c| c.request.format == 'application/json' }

  respond_to :json

  def create
    build_resource
    resource = User.new(params[:user])
    if resource.save
      sign_in resource
      render :status => 200,
           :json => { :success => true,
                      :info => "Registered",
                      :data => { :user => resource,
                                 :auth_token => current_user.authentication_token } }
    else
      logger.info("current user passed from json: #{resource.to_yaml}")
      render :status => :unprocessable_entity,
             :json => { :success => false,
                        :info => resource.errors,
                        :data => {} }
    end
  end
end
类注册控制器Proc.new{| c | c.request.format=='application/json'}
回复:json
def创建
建设资源
resource=User.new(参数[:User])
如果resource.save
登录资源
渲染:状态=>200,
:json=>{:success=>true,
:info=>“已注册”,
:data=>{:user=>resource,
:auth\u token=>current\u user.authentication\u token}
其他的
info(“当前用户从json:#{resource.to_yaml}传递”)
呈现:状态=>:无法处理的_实体,
:json=>{:success=>false,
:info=>resource.errors,
:data=>{}
结束
结束
结束

我很确定问题在于字符串被转义了。