簡體   English   中英

使用 Volley 庫從 Android 在 PHP 服務器中接收 Json 數據 POST 方法

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

您好,我正在使用 volley 庫從 android 發送一個 JSON 對象。 我無法在 PHP 中接收此 JSON 對象。 我通過 echo ING 檢查了我的 JSON 數據,我可以在“OnResponse 方法”中看到該對象。 如果有人能幫我解決它,那將是我的榮幸。 我欠你一大筆債。 這是我的代碼->

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 格式為 ->

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

最后 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();
?> 

我無法在 PHP 中接收 JSON 對象。 提前致謝。

在您的php代碼中,更改

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

$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