2017-02-27 117 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>() { . . . ..