dreamact3026
2016-05-10 20:04
浏览 107

值类型java.lang.String的类型无法转换为JSONObject - android php

I am developing an android registration activity I'm using : Android Studio php postgreSQL

this is the piece of code which is supposed to insert in the database

public void insert_in_Database(final String first_name,final String last_name, final String username, final String password, final String email, final String phone) {

    StringRequest strReq = new StringRequest(Request.Method.POST, Server.REGISTERATION_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    //Showing toast message of the response

                    try {
                        JSONObject object = new JSONObject(response);
                        String status= object.getString("status");

                        if (status.equalsIgnoreCase("success"))
                        {
                            Toast.makeText(SignUpActivity.this, status, Toast.LENGTH_LONG).show();
                        }
                        else {

                            Toast.makeText(SignUpActivity.this, status, Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    //Showing toast

                    Toast.makeText(SignUpActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new Hashtable<>();
            //Adding parameters
            params.put(Server.KEY_USERNAME, username);
            params.put(Server.KEY_PASSWORD, password);
            params.put(Server.KEY_EMAIL, email);

            //returning parameters
            return params;
        }
    };
    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    //Adding request to the queue
    requestQueue.add(strReq);
}

and this is registration.php

<?php
    require_once(__DIR__.'/../dbConnect.php');  

    $success = array('status' => "success");
    $failed_username = array('status' => "This username is already taken");
    $failed_email = array('status' => "This email is already registered");
    $failed = array('status' => "failed");

    if($_SERVER['REQUEST_METHOD']=='POST'){ 

        $first_name = $_POST['first_name'];
        $last_name = $_POST['last_name'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];   

        $query_username = "SELECT * FROM users WHERE username='$username'";
        $result_username = pg_query($con, $query_username);
        $rows_username = pg_num_rows($result_username);     
        if ($rows_username == 0) 
        {
                $query_email = "SELECT * FROM users WHERE email='$email'";
                $result_email = pg_query($con, $query_email);
                $rows_email = pg_num_rows($result_email);   
                if($rows_email == 0){

                        $sql = "INSERT INTO users (id,first_name,last_name, username, password, email, phone) 
                        VALUES(DEFAULT , '$first_name', '$last_name', '$username', '$password', '$email', '$phone')";
                        if(pg_query($con,$sql))
                        {
                        echo json_encode($success);
                        }
                        else{ 
                        echo json_encode($failed);
                        }

                }else{
                    echo json_encode($failed_email);
                }           
        }else 
        {
            echo json_encode($failed_username);
        }       
        pg_close($con);
    }else{
        echo json_encode($failed);
    }
?>

I get this error

org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

referring to the first and the last lines in the following block

  StringRequest strReq = new StringRequest(Request.Method.POST, Server.REGISTERATION_URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //Showing toast message of the response

                        try {
                            JSONObject object = new JSONObject(response);

Can anyone help?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhui9936 2016-05-11 08:22
    已采纳

    I've just solved the error and it's working now

    The problem was from the database

    When a user inserts his info , there r some fields which r inserted with NULL .. not important information at registration phase according to the app requirements but he can insert them later when editing his profile

    So I had to get that html error , I printed it using Toast to test and see what happens

    The person responsible for the database created the columns to be NOT NULL

    So the solution was : Removing the NOT NULL constraints of some columns

    点赞 打赏 评论

相关推荐 更多相似问题