我对android比较新,我试图以
的形式将数组发送到rails服务器"user"=>{"name"=>"jackson", "email"=>"jack@yahoo.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}
我不知道我是否正确地做了,但这就是我所拥有的。我有一个用于保存数据的User类
public class User {
private String name;
private String email;
private String password;
private String password_confirmation;
public User(String name, String email, String password, String password_confirmation) {
this.name = name;
this.email = email;
this.password = password;
this.password_confirmation = password_confirmation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPasswordConfirmation() {
return password_confirmation;
}
public void setPasswordConfirmation(String password_confirmation) {
this.password_confirmation = password_confirmation;
}
}
然后我使用
获取输入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 User user = new User(null, null, null, null);
user.setName(String.valueOf(inputUsername.getText()));
user.setEmail(String.valueOf(inputEmail.getText()));
user.setPassword(String.valueOf(inputPassword.getText()));
user.setPasswordConfirmation(String.valueOf(inputPasswordConfirmation.getText()));
现在我被困的地方是发帖请求。我尝试使用
发送电子邮件protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("email",email);
return params;
}
但是,这会将数据发送为{"email"=>"example@gmail.com"}
,而不是"user"= {"name"=>"example@gmail.com"}
有人可以告诉我我哪里出错了,谢谢。
答案 0 :(得分:0)
尝试这个:
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
Map<String, String> user = new HashMap<String, String>();
user.put("email",email);
params.put("user", user);
return params;
}
答案 1 :(得分:0)
将所有数据放入JSONObject
并将其用作请求的正文。有点像这样:
JSONObject jsonObject = new JSONObject();
// populate JSON with the user data
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// your implementation
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// your implementation
}
});
但在你尝试之前,我必须给你一些稍微偏离主题的建议:
RequestQueue
的多个实例。预期的模式是单个RequestQueue
来处理您的所有网络需求。这就是为什么许多人选择单身人士模式。null
变量的构造函数是不好的样式。如果您是初学者,我建议您阅读Java。启动IMO的一个好地方是&#34; Effective Java&#34;。我希望我没有劝阻你,我只是想给你一些见解,因为你相对较新。
祝你好运。