vendredi 24 juin 2016

Converting Json response(in String Format) to Json Object

I am trying to insert some data in mysql which is hosted in cpanel, I have written queries in PHP files and uploaded in cpanel file manager. I have copied code of PHP file here which should give me response in JSON format.

PHP code Here==>

<?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 Code Here===>

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

        }

Aucun commentaire:

Enregistrer un commentaire