我正在尝试将用户注册详细信息传递给rails用户控制器。虽然rails Rails使用@user对象创建表单标记,但我不知道如何为android做同样的事情。我使用以下代码使用Volley发送帖子请求。
public class Register extends Activity {
Button btnLogin;
EditText inputUsername;
EditText inputEmail;
EditText inputPassword;
EditText inputPasswordConfirmation;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
inputUsername = (EditText) findViewById(R.id.fld_username);
inputEmail = (EditText) findViewById(R.id.fld_email);
inputPassword = (EditText) findViewById(R.id.fld_pwd);
inputPasswordConfirmation = (EditText) findViewById(R.id.fld_pwd_confirm);
btnLogin = (Button) findViewById(R.id.btn_login);
final RequestQueue queue = Volley.newRequestQueue(this);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String username = String.valueOf(inputUsername.getText());
final String email = String.valueOf(inputEmail.getText());
final String password = String.valueOf(inputPassword.getText());
final String passwordconfirmation = String.valueOf(inputPasswordConfirmation.getText());
StringRequest postRequest = new StringRequest(Request.Method.POST,
"http://community.ngrok.com/users", new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.d("Response", s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("Error",volleyError.toString());
}
}){
@Override
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("name", username);
params.put("email",email);
params.put("password", password);
params.put("password_confirmation", passwordconfirmation);
return params;
}
};
queue.add(postRequest);
Intent home = new Intent(getApplicationContext(), MainActivity.class);
startActivity(home);
}
});
}
}
用于注册代码的我的rails new.html.erb是
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(@user) do |f| %>
<%= render 'fields', f: f %>
<%= f.submit "Create my account", class: "btn btn-primary" %>
<% end %>
</div>
</div>
发送帖子请求时收到的错误是
ActionController::ParameterMissing (param is missing or the value is empty: user):
app/controllers/users_controller.rb:73:in `user_params'
app/controllers/users_controller.rb:21:in `create'
所以我的问题是如何使用Volley将user_params包装到数组中以获得@user对象
答案 0 :(得分:0)
这是在Android中你做不到/不容易做的事情。 Serializeable类可以在Android(java)中退出,但您的实现方式有所不同。您需要从HTTP等提取数据以便使用它。