2016-05-30 91 views
0

這裏此代碼後,另一個測試didtn工作突然工作,然後和說,JSON包含HTML元素,如
,我不能讓它再工作,我需要幫助我不能解析JSON數據

<?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它給我說,它包含的東西全髖關節置換是沒有一個字符串..這是我用來解析JSON

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

嘗試發送一個'json'頭 - >'標題( 「內容類型:應用程序/ JSON」);' – Darren

+0

如何u能請給我看看? –

+0

剛剛做了.....把'header(...)'放在你的php腳本的開頭。 – Darren

回答

1

你最好實際發送頭,讓Android的代碼你Android應用程序知道它正在接收json,因爲它可能認爲它獲得了內容類型:text/html

試着將這個頭在你的PHP腳本的頂部:

<?php 
header("Content-Type: application/json"); 
+0

工作:D再次感謝你 –