duangou6446 2016-05-30 00:30
浏览 56
已采纳

我无法解析json数据

here this code was working and then suddenly after another test it didtn work and said that the json contains html elements like
and i cant make it work again i need help

<?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);

?>

when i parse it in andoid it says to me that it contains a something thas is no a string .. and this is my android code for parsing the 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);
    }
  • 写回答

1条回答 默认 最新

  • dtcyv3985 2016-05-30 00:44
    关注

    You're best to actually send headers to let your android app know that it's receiving json as it probably thinks it's getting the Content Type: text/html.

    Try placing this header at the top of your PHP script:

    <?php
    header("Content-Type: application/json");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab appdesigner私有函数嵌套整合
  • ¥15 给我一个openharmony跑通webrtc实现视频会议的简单demo项目,sdk为12
  • ¥15 vb6.0使用jmail接收smtp邮件并另存附件到D盘
  • ¥30 vb net 使用 sendMessage 如何输入鼠标坐标
  • ¥15 关于freesurfer使用freeview可视化的问题
  • ¥100 谁能在荣耀自带系统MagicOS版本下,隐藏手机桌面图标?
  • ¥15 求SC-LIWC词典!
  • ¥20 有关esp8266连接阿里云
  • ¥15 C# 调用Bartender打印机打印
  • ¥15 我这个代码哪里有问题 acm 平台上显示错误 90%,我自己运行好像没什么问题