dreamact3026 2016-05-10 20:04
浏览 112
已采纳

值类型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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试