为了应用程序的目的,我将StudentBATCH\u列表转换为JSON数组,然后在web api上反序列化该数组。
以下是简要步骤:
第1步。将Volley和GSON依赖项添加到应用程序级构建中。gradle文件:
dependencies {
...
compile 'com.android.volley:volley:1.1.0'
compile 'com.google.code.gson:gson:2.8.2'
}
第2步。使用GSON将列表转换为JSON数组:
String studentBatchListString = new Gson().toJson(students);
第3步。将studentBatchListString发布到web api(“发送到PHP”)
String url = "http://yourdomain.com/post.php";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("students_batch_list", studentBatchListString);
return params;
}
};
queue.add(postRequest);
第4步。在PHP端反序列化:
$content = $_POST['students_batch_list'];
$json = json_decode($content, true);
foreach ($json as $key => $value) {
$firstname = $value["firstname"];
$lastname = $value["lastname"];
// perform other actions.
}