2017-02-27 116 views
0

我之前没有使用过Volley,所以我在这里是一个新手。我尝试使用Post参数做一个JSONArrayRequest。 一个PHP脚本将检查这些参数并回答一个将显示在列表中的JSON数组。 但不知何故Post参数不发送。所以我的PHP脚本说,后期参数丢失。JsonArrayRequest Post方法不起作用

那么我做错了,发布参数不发送?

这里是我的代码:

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

[...] 

将是巨大的,当有人发现我的错误!

+0

你可以发布你的getmembers.php文件,以便我们可以看到你有什么吗? –

+0

我已经添加了我的PHP代码的一部分。它应该是足够的@SanyamJain – Olli

+0

你在哪里使用getParams()? –

回答

1

在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>() { . . . ..