将Json响应(以字符串格式)转换为Json对象

时间:2016-06-17 12:00:00

标签: php android json android-volley

我试图在cpanel中托管的mysql中插入一些数据,我在php文件中编写了查询并在cpanel文件管理器中上传。我在这里复制了php文件的代码,它应该以json格式给我回复。

PHP代码:

<?php
    $con = mysqli_connect("sql308.byethost24.com", "username", "password", "b24_18335415_alokdb");

    $name = $_POST["name"];
    $age = $_POST["age"];
    $username = $_POST["username"];
    $password = $_POST["password"];

    $statement = mysqli_prepare($con, "INSERT INTO user (name, username, age, password) VALUES (?, ?, ?, ?)");
    mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
    mysqli_stmt_execute($statement);

    $response = array();
    $response["success"] = true;  

    echo json_encode($response);
?>

Java代码:

Response.Listener<String> responseListener = new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        try{
           JSONObject jsonResponse = new JSONObject(response);//Here my code is failing
            System.out.println("After JSONRESPONSE");
            boolean success = jsonResponse.getBoolean("success");
           // boolean success = response.getBoolean("success");
            if(success){
                Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                RegisterActivity.this.startActivity(intent);
            }
            else{
                AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
                builder.setMessage("Registration Failed")
                        .setNegativeButton("Retry", null)
                        .create()
                        .show();
            }
        }
        catch(JSONException e){e.printStackTrace();}
    }
};
    RegisterRequest registerRequest = new RegisterRequest(name,username,age,password,responseListener);
    RequestQueue requestQueue = Volley.newRequestQueue(RegisterActivity.this);
    requestQueue.add(registerRequest);
}

0 个答案:

没有答案