如何从排球请求中发送arraylist数据并获取php

时间:2016-06-10 08:32:06

标签: php android arraylist android-volley

我想从volley requset向php发送一个ID和数组字符串数据列表。但我不确定如何正确发送到服务器,如何在PHP中获取它。 这是android端向服务器发送请求:

private void sendMessage() {

 StringRequest stringRequest = new StringRequest(Request.Method.POST, Config.NOTIF_URL,
  new Response.Listener < String > () {
   @Override
   public void onResponse(String response) {

    Log.d("Response --->", response);
    jsonNotif = new ParseJSON(response);
    jsonNotif.parseJSON();

   }
  },
  new Response.ErrorListener() {
   @Override
   public void onErrorResponse(VolleyError error) {
    Toast.makeText(context, error.getMessage(), Toast.LENGTH_LONG).show();
   }
  }) {
  @Override
  protected Map < String, String > getParams() throws AuthFailureError {
   Map < String, String > params = new HashMap < > ();
   //Adding parameters to request

   ArrayList < String > courseList = new ArrayList < String > (checkedSet);

   String ID = prefProfID.getString(Config.PROFID_SHARED_PREF, "0");
   Log.d("ID prof list >>", ID);
   params.put(Config.PROFID_SHARED_PREF, ID);


   for (int i = 0; i < courseList.size(); i++) {
    params.put("courselist", courseList.get(i));
   }
   //returning parameter
   return params;
  }
 };
 RequestQueue requestQueue = Volley.newRequestQueue(context);
 requestQueue.add(stringRequest);

}

这是我的PHP代码:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //Getting values 
    $courseList = $_POST['courseList'];
    $professor_ID = $_POST['Prof_ID'];
    require_once('dbConnect.php');
    $newcourseList = implode(", ", $courseList);
    $sql           = "select Stud_ID,student.f_Name,student.l_Name from student,course     where course.S_ID = student.Stud_ID and course.P_ID in ($newcourseList)";
    $res    = mysqli_query($con, $sql);
    $result = array();
    while ($row = mysqli_fetch_array($res)) {
        array_push($result, array(
            'id' => $row[0],
            'fname' => $row[1],
            'lname' => $row[2],
            'tag' => 'studlist'
        ));
    }
    echo json_encode(array(
        "result" => $result
    ));
    mysqli_close($con);
}
?>

3 个答案:

答案 0 :(得分:3)

最后,我有最简单,最完美的解决方案:

使用此依赖项:

implementation 'com.google.code.gson:gson:2.8.2'

并使用此行

String data = new Gson().toJson(myArrayList);

现在您可以将此字符串作为字符串参数传递给volley,例如下面的示例。

示例:

protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                String data = new Gson().toJson(myArrayList);
                params.put("terms", data);
                return params;
            }

答案 1 :(得分:2)

如果你想发送ArrayList数据,我认为最好通过将它转换为JSONArray来发送它

答案 2 :(得分:1)

首先在ArrayList中的Object中:创建JSONObjectmethod名称作为getJSONObject,就像这样

public class EstimateObject {
String id, name, qty, price, total;
public EstimateObject(String id, String name, String qty, String price, String total, int position)
{
    this.id = id;
    this.name = name;
    this.qty = qty;
    this.price = price;
    this.total =total;
    this.position = position;
}
 public JSONObject getJSONObject() {
    JSONObject obj = new JSONObject();
    try {
        obj.put("Id", id);
        obj.put("Name", name);
        obj.put("Qty",qty);
        obj.put("Price", price);
        obj.put("Total", total);
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    return obj;
}

Aftrer以下是我在活动中如何转换它

    JSONObject JSONestimate = new JSONObject();
    JSONArray myarray = new JSONArray();

    for (int i = 0; i < items.size(); i++) {

        try {
            JSONestimate.put("data:" + String.valueOf(i + 1), items.get(i).getJSONObject());
            myarray.put(items.get(i).getJSONObject());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    Log.d("JSONobject: ", JSONestimate.toString());
    Log.d("JSONArray : ", myarray.toString());

这里我转换了JSONObject和JSONArray类型。

之后

map.put("jsonarray",myarray.toString());

在php方面:

$json = $_POST['jsonarray'];
$jsonarray = json_decode($json,true);