Volley JsonObjectRequest Post request not working

后端 未结 9 2007
有刺的猬
有刺的猬 2020-11-22 06:25

I am using android Volley for making a request. So I use this code. I don\'t understand one thing. I check in my server that params is always null. I consider that getParams

9条回答
  •  面向向阳花
    2020-11-22 07:12



    build gradle(app)

    dependencies {
        implementation fileTree(dir: "libs", include: ["*.jar"])
        implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        implementation 'androidx.core:core-ktx:1.1.0'
        implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
        implementation 'com.android.volley:volley:1.1.1'
    }
    

    android manifest
    
    
    

    MainActivity
    When you use JsonObjectRequest it is mandatory to send a jsonobject and receive jsonobject otherwise you will get an error as it only accepts jsonobject.
    import com.android.volley.Request
    import com.android.volley.Response
    import com.android.volley.toolbox.JsonObjectRequest
    import com.android.volley.toolbox.Volley
    
    fun peticion(){
        val jsonObject = JSONObject()
        jsonObject.put("user", "jairo")
        jsonObject.put("password", "1234")
        val queue = Volley.newRequestQueue(this)
        val url = "http://192.168.0.3/get_user.php"
        // GET: JsonObjectRequest( url, null,
        // POST: JsonObjectRequest( url, jsonObject,
        val jsonObjectRequest = JsonObjectRequest( url, jsonObject,
            Response.Listener { response ->
                // Check if the object 'msm' does not exist
                if(response.isNull("msm")){
                    println("Name: "+response.getString("nombre1"))
                }
                else{
                    // If the object 'msm' exists we print it
                    println("msm: "+response.getString("msm"))
                }
            },
            Response.ErrorListener { error ->
                error.printStackTrace()
                println(error.toString())
            }
        )
        queue.add(jsonObjectRequest)
    }
    

    file php get_user.php
    user;
        $password=$params->password;
        $res=array();
        $verifica_usuario=mysqli_query($mysqli,"SELECT * FROM usuarios WHERE usuario='$user' and clave='$password'");
        if(mysqli_num_rows($verifica_usuario)>0){
            $query="SELECT * FROM usuarios WHERE usuario='$user'";
            $result=$mysqli->query($query);
            while($row = $result->fetch_array(MYSQLI_ASSOC)){
                $res=$row;
            }
        }
        else{
            $res=array('msm'=>"Incorrect user or password");
        }
        $jsonstring = json_encode($res);
        header('Content-Type: application/json');
        echo $jsonstring;
    ?>
    

    file php conexion
    set_charset('utf8');
            return $mysqli;
        }
    ?>
    

提交回复
热议问题