JsonArrayRequest Post Method 不起作用



我以前没有使用凌空,所以我在这里是新手。我尝试使用Post参数进行JSONARRAYREQUEST。PHP脚本将检查这些参数,并使用列表中显示的JSON数组回答。但是以某种方式不会发送帖子参数。因此,我的php脚本说帖子参数缺少。

那么,帖子参数不发送我做错了什么?

这是我的代码:

private void getPersonsData(final String PhoneNr, final String Password) {
    String url = "http://127.0.0.1:80/android_login_api/getmembers.php";
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Method.POST, url, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            try {
                //Adding a person in the list
                if (response.length() > 0) {
                    personList.clear();
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject jsonObject = response.getJSONObject(i);
                        Person person = new Person();
                        if (!jsonObject.isNull("fullname")) {
                            person.name = jsonObject.getString("fullname");
                        }
                        if (!jsonObject.isNull("location")) {
                            person.location = jsonObject.getString("location");
                        }
                        personList.add(i, person);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e("Error: ", error.getMessage());
        }
    })  {
        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("phonenr", PhoneNr);
            params.put("password", Password);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonArrayRequest);
}

这是我的PHP代码的某些部分:

getmembers.php
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array("error" => FALSE);
if (isset($_POST['phonenr']) && isset($_POST['password'])) {  //this goes on false
// receiving the post params
$phonenr = $_POST['phonenr'];
$password = $_POST['password'];
$user = $db->getUserByPhonenrAndPassword($phonenr, $password);
[...]
当有人发现我的错误时,

会很棒!

PHP中的普通$_POST方法在凌空中不起作用。您需要在PHP文件中进行此操作。

$post = json_decode(file_get_contents("php://input"), true);
$my_value = $post['param1'];

param1是您放在凌空中的价值。

使用此:

final Hashmap<String,String> param1 = new Hashmap<string,string>();
param1.put("param1",your value);

它对我有用。您可以尝试

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Method.POST, url, **new JSonObject(param1)**, new Response.Listener<JSONArray>() { . . . .. 

相关内容

最新更新