PHP와 Mysql, 안드로이드 스튜디오를 연동중입니다.
그런데 JSONObject jsonObject에서 에러가 나네요.
org.json.JSONException: Value MySql of type java.lang.String cannot be converted to JSONObject
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
boolean success = jsonObject.getBoolean("success");
if(success) {
Toast.makeText(getApplicationContext(), "회원 등록에 성공하였습니다.",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), loginScreen.class);
startActivity(intent);
}
else {
Toast.makeText(getApplicationContext(), "회원 등록에 실패하였습니다.",
Toast.LENGTH_SHORT).show();
return;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
php파일입니다.
<?php
$host = "localhost";
$dbUser = "";
$db_pw = "";
$db_name = "";
//mysqli_connect("localhost", "사용자 아이디", "패스워드", "사용할 DB")
$connection = mysqli_connect("$host", "$dbUser", "$db_pw", "$db_name");
// connection check
if (mysqli_connect_errno()) {
printf("Connection failed: %s \n", mysqli_connect_error());
exit();
}
else {
echo "MySql connection is completed";
}
mysqli_query($connection, 'SET NAMES utf8');
$userId = $_POST["userId"];
$userPw = $_POST["userPw"];
$userName = $_POST["userName"];
$sex = $_POST["sex"];
$statement = mysqli_prepare($connection, "INSERT INTO User VALUE (?,?,?,?)");
mysqli_stmt_bind_param($statement, "ssss", $userId, $userPw, $userName, $sex);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
mysqli_close($connection);
?>
에러코드입니다.
W/System.err: org.json.JSONException: Value MySql of type java.lang.String cannot be converted to JSONObject
at org.json.JSON.typeMismatch(JSON.java:112)
at org.json.JSONObject.<init>(JSONObject.java:169)
at org.json.JSONObject.<init>(JSONObject.java:182)
at com.example.myhealthdiary.signUpScreen$1$1.onResponse(signUpScreen.java:52)
at com.example.myhealthdiary.signUpScreen$1$1.onResponse(signUpScreen.java:48)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
at android.os.Handler.handleCallback(Handler.java:938)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)