本文介绍了如何从 volley 请求发送 arraylist 数据并进入 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从 volley requset 向 php 发送字符串数据的 ID 和数组列表.但我不确定如何正确发送到服务器以及如何在 php 中获取它.这是向服务器发送请求的android端:
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);
}
这是我的 php 代码:
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);
}
?>
推荐答案
如果要发送 ArrayList 数据,我认为最好通过 Converting it into JSONArray 发送
If you want to send ArrayList data, I think its better to send it by Converting it into JSONArray
这篇关于如何从 volley 请求发送 arraylist 数据并进入 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!