刚尝试运行,我收到此错误:
org.json.JSONException:类型java.lang.String成功时的值不能转换为布尔值
在org.json.JSON.typeMismatch(JSON.java:100)
在org.json.JSONObject.getBoolean(JSONObject.java:378)
我是Java的初学者,我似乎无法弄清楚为什么字符串不能转换为布尔值。
错误代码专门出现在“ if”行和“ else if”行。
代码:
public class RegisterActivity extends AppCompatActivity {
private ArrayAdapter adapter;
private Spinner spinner;
private String userID;
private String userPassword;
private String userGender;
private String userMajor;
private String userEmail;
private AlertDialog dialog;
private boolean validate = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
spinner = (Spinner) findViewById(R.id.majorSpinner);
adapter = ArrayAdapter.createFromResource(this, R.array.major, android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final EditText idText = (EditText) findViewById(R.id.idText);
final EditText passwordText = (EditText) findViewById(R.id.passwordText);
final EditText emailText = (EditText) findViewById(R.id.emailText);
RadioGroup genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
int genderGroupID = genderGroup.getCheckedRadioButtonId();
userGender = ((RadioButton) findViewById(genderGroupID)).getText().toString();
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
RadioButton genderButton = (RadioButton) findViewById(i);
userGender = genderButton.getText().toString();
}
});
final Button validateButton = (Button) findViewById(R.id.validateButton);
validateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userID = idText.getText().toString();
if(validate)
{
return;
}
if(userID.equals(""))
{
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("아이디는 빈 칸일 수 없습니다.")
.setPositiveButton("확인", null)
.create();
dialog.show();
return;
}
Response.Listener<String> responseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response){
try
{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success)
{
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("사용할 수 있는 아이디 입니다.")
.setPositiveButton("확인", null)
.create();
dialog.show();
idText.setEnabled(false);
validate = true;
idText.setBackgroundColor(getResources(). getColor(R.color.colorGray));
validateButton.setBackgroundColor(getResources(). getColor(R.color.colorGray));
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("사용할 수 없는 아이디 입니다.")
.setNegativeButton("확인", null)
.create();
dialog.show();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
ValidateRequest validateRequest = new ValidateRequest(userID, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(validateRequest);
}
});
Button registerButton = (Button) findViewById(R.id.registerButton);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userID = idText.getText().toString();
String userPassword = passwordText.getText().toString();
String userMajor = spinner.getSelectedItem().toString();
String userEmail = emailText.getText().toString();
if(!validate){
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("먼저 중복 체크를 해주세요.")
.setNegativeButton("확인", null)
.create();
dialog.show();
return;
}
if(userID.equals("") || userPassword.equals("") || userMajor.equals("") || userEmail.equals("") || userGender.equals("")){
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("빈 칸 없이 입력해주세요.")
.setNegativeButton("확인", null)
.create();
dialog.show();
return;
}
Response.Listener<String> responseListener = new Response.Listener<String>(){ #########Error here
@Override
public void onResponse(String response){ #########Error here
try
{
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success)
{
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("회원 등록이 성공했습니다.")
.setPositiveButton("확인", null)
.create();
dialog.show();
finish();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
dialog = builder.setMessage("회원 등록에 실패했습니다.")
.setNegativeButton("확인", null)
.create();
dialog.show();
}
}
catch (JSONException e)
{
e.printStackTrace();
// Toast.makeText(getApplicationContext(), "error",
// Toast.LENGTH_SHORT).show();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(userID, userPassword, userGender, userMajor, userEmail, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
@Override
protected void onStop(){
super.onStop();
if(dialog !=null)
{
dialog.dismiss();
dialog = null;
}
}
}
答案 0 :(得分:1)
错误消息说明了一切。
org.json.JSONException:类型java.lang.String
键(成功)拥有一个值为 ture 的值,该值不是布尔值,而是类似 true 的值。因此json在将其转换为布尔值时遇到了问题。
此问题是在网络服务方面,您在网络服务中误返回了ture(不是真的),因此请在那进行更改。
答案 1 :(得分:0)
与此:
boolean success = jsonResponse.getBoolean("success");
您正在尝试获取boolean
值,但是返回的值无法转换为布尔值,它是一个字符串。
从您的错误日志中,我看到它可能是一个像"ture"
的值!!!!
因此将其更改为:
String success = jsonResponse.getString("success");
并像这样检查它:
if (success.equals("ture")) {
//.........
}