您好,我正在尝试从EditText获取数据并将其作为JSON对象发布,但它返回没有数据的空对象 返回的对象只包含键,而不是映射到EditText中输入的值,如屏幕所示 注意:我只需要在Toast
中检索JSON对象public class MainActivity extends AppCompatActivity {
EditText userName;
EditText password;
EditText email;
EditText phone;
Button submit;
Person person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
person = new Person();
userName = (EditText) findViewById(R.id.user_name);
password = (EditText) findViewById(R.id.password);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
submit = (Button) findViewById(R.id.submit);
person.setUserName(userName.getText().toString());
person.setPassword(password.getText().toString());
person.setEmail(email.getText().toString());
person.setPhone(phone.getText().toString());
submit.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
new JsonDataConverter().execute(person);
}
});
}
private class JsonDataConverter extends AsyncTask < Person,
Void,
String > {
@Override
protected String doInBackground(Person...params) {
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("userName", person.getUserName());
jsonObject.put("password", person.getPassword());
jsonObject.put("email", person.getEmail());
jsonObject.put("phone", person.getPhone());
return jsonObject.toString();
} catch(JSONException ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonUser = new JSONObject(s);
jsonUser.get("userName");
jsonUser.get("password");
jsonUser.get("email");
jsonUser.get("phone");
Toast.makeText(MainActivity.this, jsonUser.toString(), Toast.LENGTH_SHORT).show();
} catch(JSONException e) {
e.printStackTrace();
}
}
}
}
Person.java
public class Person implements Serializable {
String userName;
String password;
String email;
String phone;
public Person() {}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
答案 0 :(得分:0)
你刚刚在错误的地方使用了Setters。在ClickListener中使用它,如下所示..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
person = new Person();
userName = (EditText) findViewById(R.id.user_name);
password = (EditText) findViewById(R.id.password);
email = (EditText) findViewById(R.id.email);
phone = (EditText) findViewById(R.id.phone);
submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
person.setUserName(userName.getText().toString());
person.setPassword(password.getText().toString());
person.setEmail(email.getText().toString());
person.setPhone(phone.getText().toString());
new JsonDataConverter().execute(person);
}
});
}
修改强> 你使用的是更高级别的成员,而不是param [0]