hamid reza gholami hamid reza gholami - 2 months ago 15
MySQL Question

How to insert an ArrayList to mysql using Volley

I want to insert an

ArrayList
that contain some String and some integer into
MySQL
using Volley in android. This is how I want to send my array in android.
I dont know if this is right or not.

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", user.getApikey());
return params;
}

@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
for(int i=0; i<wordlist.size();i++){
params.put("word_id["+i+"]",String.valueOf(wordlist.get(i).getWord_id()));
params.put("word["+i+"]",wordlist.get(i).getWord());
params.put("translation["+i+"]",wordlist.get(i).getTranslation());
params.put("english["+i+"]",wordlist.get(i).getEnglish());

}
return params;
}


The problem is I have no idea how to get an
ArrayList
in
PHP
with POST data and how to use it to insert my data to
MySQL
.
I would be really grateful if some body tell me how to handle
PHP
part with example and even if I am doing right in android part or not.

Answer
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Authorization", user.getApikey());
return params;
}

@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("list",wordlist);
return params;
}

Then in your PHP side

 $list = $_POST["list"];
 for($i=0;$i<count($list);$i++){
//do something
}