How can send arraylist data from volley request and get in php

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 12:49:11

问题


I want to send a ID and array list of string data to php from volley requset . but i'm not sure how can send correctly to server and how can get it in php . Here is android side to send request to server:

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

}

And here is my php code :

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

回答1:


If you want to send ArrayList data, I think its better to send it by Converting it into JSONArray




回答2:


Finally, I have the easiest and perfect solution:

use this dependency:

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

and use this line

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

Now you can pass this string into volley as string parameters like bellow example.

Example:

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



回答3:


First in your Object in ArrayList: create JSONObjectmethod name as getJSONObject, like this

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 Here is how I converted it,in my activity

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

Here i converted both type JSONObject and JSONArray.

After in

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

And in php side:

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


来源:https://stackoverflow.com/questions/37743550/how-can-send-arraylist-data-from-volley-request-and-get-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!