I am relatively new to android and I am trying to send an array to a rails server in the form
"user"=>{"name"=>"jackson", "email"=>"jack@yahoo.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}
I don't know if i am doing it correctly but here is what I have. I have a User class to save the data
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;
}
}
I then get the input using
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()));
Now where I am stuck is making the post request. I have tried sending the email using
protected Map<String,String> getParams(){
Map<String, String> params = new HashMap<String, String>();
params.put("email",email);
return params;
}
but that sends data as {"email"=>"example@gmail.com"}
Instead of "user"= {"name"=>"example@gmail.com"}
Can someone please show me where I am going wrong, thanks.
Try this maybe:
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;
}
Put all of your data into a JSONObject
and use it as the body of the request. Something along the lines of this:
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
}
});
But before you try it I must give you some slightly off-topic advice:
RequestQueue
. The intended pattern is a single RequestQueue
to handle all of your network needs. That's why many choose the singleton pattern. null
variables is bad style. I recommend reading up on Java if you are a beginner. A good place to start IMO is "Effective Java". I hope I didn't discourage you, I just want to give you some insight since you are relatively new.
Good luck.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.