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

时间:2018-06-26 09:37:08

标签: php json

您好,我正在使用凌空库从android发送一个json对象。我无法在php中收到此json对象。我通过回显ING我的Json数据进行检查,可以在“ OnResponse方法”中看到该对象。如果有人可以帮助我解决这个问题,那将是我的荣幸。我欠你很多债。这是我的代码->

Andoird Volley代码->

Requirement already satisfied: scrapy in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (1.5.0)
Requirement already satisfied: cssselect>=0.9 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (1.0.3)
Requirement already satisfied: parsel>=1.1 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (1.4.0)
Requirement already satisfied: w3lib>=1.17.0 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (1.19.0)
Requirement already satisfied: six>=1.5.2 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (1.11.0)
Requirement already satisfied: PyDispatcher>=2.0.5 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (2.0.5)
Requirement already satisfied: pyOpenSSL in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (18.0.0)
Requirement already satisfied: lxml in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (4.2.2)
Requirement already satisfied: queuelib in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (1.5.0)
Requirement already satisfied: service-identity in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (17.0.0)
Requirement already satisfied: Twisted>=13.1.0 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from scrapy) (18.4.0)
Requirement already satisfied: cryptography>=2.2.1 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from pyOpenSSL->scrapy) (2.2.2)
Requirement already satisfied: pyasn1 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from service-identity->scrapy) (0.4.3)
Requirement already satisfied: pyasn1-modules in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from service-identity->scrapy) (0.2.1)
Requirement already satisfied: attrs in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from service-identity->scrapy) (18.1.0)
Requirement already satisfied: zope.interface>=4.4.2 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from Twisted>=13.1.0->scrapy) (4.5.0)
Requirement already satisfied: constantly>=15.1 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from Twisted>=13.1.0->scrapy) (15.1.0)
Requirement already satisfied: incremental>=16.10.1 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from Twisted>=13.1.0->scrapy) (17.5.0)
Requirement already satisfied: Automat>=0.3.0 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from Twisted>=13.1.0->scrapy) (0.7.0)
Requirement already satisfied: hyperlink>=17.1.1 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from Twisted>=13.1.0->scrapy) (18.0.0)
Requirement already satisfied: idna>=2.1 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from cryptography>=2.2.1->pyOpenSSL->scrapy) (2.7)
Requirement already satisfied: cffi>=1.7; platform_python_implementation != "PyPy" in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from cryptography>=2.2.1->pyOpenSSL->scrapy) (1.11.5)
Requirement already satisfied: asn1crypto>=0.21.0 in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from cryptography>=2.2.1->pyOpenSSL->scrapy) (0.24.0)
Requirement already satisfied: setuptools in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from zope.interface>=4.4.2->Twisted>=13.1.0->scrapy) (39.2.0)
Requirement already satisfied: pycparser in c:\users\sameer\appdata\local\programs\python\python35\lib\site-packages (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography>=2.2.1->pyOpenSSL->scrapy) (2.18)

Json格式为->

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);

}

最后,PHP代码是->

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

?>

我无法在PHP中接收Json对象。预先感谢。

1 个答案:

答案 0 :(得分:0)

在您的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'];