Reputation: 851
Hello I have a List named StudentBATCH_LIST that I populated with the contents from a csv using:
File csvFILE = new File(getFILE_PATH());
try {
CSVReader csvREAD = new CSVReader(new
FileReader(csvFILE.getAbsolutePath()));
String[] csvLINE;
int skip = 0;
while((csvLINE = csvREAD.readNext())!=null)
{
if(skip > 0)//becasue first line is column headers
{
String PARAM_USER_ID = csvLINE[0];
String PARAM_STUD_FIRSTNAME = csvLINE[1];
String PARAM_STUD_LASTNAME = csvLINE[2];
String PARAM_STUD_MIDDLENAME = csvLINE[3];
String PARAM_STUD_EMAIL = PARAM_USER_ID+EMAIL_S;
AdminAddStudentBATCH_CONFIG STUD_OBJECT = new AdminAddStudentBATCH_CONFIG(PARAM_USER_ID,
PARAM_STUD_FIRSTNAME,
PARAM_STUD_LASTNAME,
PARAM_STUD_MIDDLENAME,
PARAM_STUD_EMAIL);
StudentBATCH_LIST.add(STUD_OBJECT);
}
else
{
skip ++;
}
}
} catch (FileNotFoundException e) {
volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
} catch (IOException e) {
volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
} catch (JSONException e) {
volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
} catch (IndexOutOfBoundsException e) {
volleyErrorClass.catchInvalidResponse(e.toString(),AdminAddStudentBATCH.this);
}
I want to know how do I pass this List to PHP with volley. How do I do it? Also how do I decode this in PHP.
Anyway the tags of the values in the List are
USER_ID
,
STUD_FIRSTNAME
,
STUD_LASTNAME
,
STUD_MIDDLENAME
,
STUD_EMAIL
Upvotes: 1
Views: 1718
Reputation: 3296
For the purpose of your application, I'd convert the StudentBATCH_LIST to JSON array, and then deserialize that array on your web api.
Here are brief steps:
Step 1. Add Volley and GSON dependencies to app level build.gradle file:
dependencies {
...
compile 'com.android.volley:volley:1.1.0'
compile 'com.google.code.gson:gson:2.8.2'
}
Step 2. convert list to JSON array using GSON:
String studentBatchListString = new Gson().toJson(students);
Step 3. POST studentBatchListString to your web api ("send to 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);
Step 4. deserialize on PHP side:
$content = $_POST['students_batch_list'];
$json = json_decode($content, true);
foreach ($json as $key => $value) {
$firstname = $value["firstname"];
$lastname = $value["lastname"];
// perform other actions.
}
Upvotes: 1