这里这个代码正在运行,然后突然又经过另一次测试后它确实工作并说json包含了像我这样的html元素,我不能让它再次工作我需要帮助
<?php
include("config.php");
if(isset($_POST["email"]) && isset($_POST["username"]) && isset($_POST["password"])){
$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
}
$con = mysqli_connect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);
$response = array();
$statement = mysqli_prepare($con, "SELECT * FROM accounts WHERE email = ? OR username = ?");
mysqli_stmt_bind_param($statement, "ss", $email, $username);
$result = mysqli_stmt_execute($statement);
$rows = $result->num_rows;
if($rows > 0){
$response["success"] = false;
$response["message"] = "Email or Username already exists.";
}else{
mysqli_stmt_close($statement);
$statement2 = mysqli_prepare($con, "INSERT INTO accounts (email, username, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement2, "sss", $email, $username, $password);
$result2 = mysqli_stmt_execute($statement2);
$rows2 = $result2->affected_rows;
if($rows2 > 0){
$response["success"] = true;
$response["message"] = "Account created successfuly.";
}else{
$response["success"] = false;
$response["message"] = "Creation error.";
}
}
echo json_encode($response);
mysqli_close($con);
?>
当我在andoid中解析它时它告诉我它包含的东西thas是没有字符串..这是我的解码json的android代码
private void register() {
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");
String message = jsonresponse.get("message").toString();
if(success){
Intent gotoContinueRegisterActivityIntent = new Intent(RegisterActivity.this, ContinueRegisterActivity.class);
startActivity(gotoContinueRegisterActivityIntent);
}else {
error.setText(message);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error2) {
error2.printStackTrace();
}
};
RegisterRequest registerRequest = new RegisterRequest(s_email, s_username, s_password, responselistener, errorListener);
RequestQueue requestQueue = Volley.newRequestQueue(getBaseContext());
requestQueue.add(registerRequest);
}
答案 0 :(得分:1)
您最好实际发送标题,让您的Android应用知道它已接收json
,因为它可能认为它获取了内容类型:text/html
尝试将此标题放在PHP脚本的顶部:
<?php
header("Content-Type: application/json");