简体   繁体   English

使用 Volley 库从 Android 在 PHP 服务器中接收 Json 数据 POST 方法

[英]Receive Json data POST method in PHP server from Android using Volley Library

Hello I'm send an JSON object from android using volley library.您好,我正在使用 volley 库从 android 发送一个 JSON 对象。 I can not receive this JSON object in PHP.我无法在 PHP 中接收此 JSON 对象。 I checked by echo ING my JSON data I can see the object in my 'OnResponse Method'.我通过 echo ING 检查了我的 JSON 数据,我可以在“OnResponse 方法”中看到该对象。 It would be my pleaser if anyone can help me to solve it.如果有人能帮我解决它,那将是我的荣幸。 I'll owe you a great debt.我欠你一大笔债。 Here is my code ->这是我的代码->

Android Volley Code -> Android Volley 代码 ->

private void registerUser() {
    JSONObject postObject = new JSONObject();
    RequestQueue queue =  Volley.newRequestQueue(this);
    JSONObject historyObject = new JSONObject();
    
    String url ="http://helpinghandbd.org/app/index.php";
    try {
        //historyObject.put("id","1");
        historyObject.put("email","1234");
        historyObject.put("password","1234");
        postObject.put("user",historyObject);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
    Log.e("LoginActivityJsonObject",""+postObject);
    JsonObjectRequest objRequest = new JsonObjectRequest(Request.Method.POST, url,postObject,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("LoginActivity","OnResponse: "+response);
                    Toast.makeText(LoginActivity.this, String.valueOf(response), Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("OnError", String.valueOf(error.getMessage()));
        }
    });

    queue.add(objRequest);

}

JSON Format is -> JSON 格式为 ->

{ 'user':{
           'email':'1234',
           'password':'1234'
        }
 }

And Finally PHP Code is ->最后 PHP 代码是 ->

<?php

    $data = file_get_contents("php://input"); 
    //echo $data; -> //{ 'user':{'email':'1234','password':'1234'}};
    $decode = json_decode($data,true);
    $email = $decode->user['email'];
    $password = $decode->user['passowrd'];

    $servername = "localhost";
    $username = "helpinghandbd_app";
    $password = "Demopass000";
    $dbname = "helpinghandbd_app";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    //$data = file_get_contents("php://input"); 
    //{ 'user':{'email':'1234','password':'1234'}};
    
    
    
    $sql = "INSERT INTO users (id,email,password)
    VALUES (null, '$email', '$password')";
    
    if ($conn->query($sql) === TRUE) {
        echo $data;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    
    
    $conn->close();
?> 

I can not receive JSON Object in PHP.我无法在 PHP 中接收 JSON 对象。 Thanks in advance.提前致谢。

In your php code, change在您的php代码中,更改

$decode = json_decode($data,true);
$email = $decode->user['email'];
$password = $decode->user['passowrd'];

to

$decode = json_decode($data,true);
$email = $decode['user']['email'];
$password = $decode['user']['passowrd'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM