So far I have been able to extract contacts both number and name, What I want to do is to send the data to the database via php. So far so good, instead It is sending only the last one in the array, Any suggestions as to how i can send the array and save the whole extracted contacts?
Java
String URL ="http://192.168.0.10/oap/home/SynchContacts/";
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name,email;
name = Name.getText().toString();
email = Email.getText().toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
builder.setTitle("Server Title");
builder.setMessage(response);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog bu = builder.create();
bu.show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error",Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
ContentResolver cr = getContentResolver(); //Activity/Application android.content.Context
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if(cursor.moveToFirst())
{
alContacts = new ArrayList<String>();
do
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
while (pCur.moveToNext())
{
String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// alContacts.add('{'+name+":"+contactNumber);
param = new HashMap<String, String>();
param.put("name[]", name);
param.put("phone[]", contactNumber);
break;
}
pCur.close();
}
} while (cursor.moveToNext()) ;
}
return param;
}
};
MySingleton.getInstance(MainActivity.this).addToRequestQueue(stringRequest);
}
});
PHP
function SynchContacts() {
$phone= $this->input->post('phone');
$name= $this->input->post('name');
for ($i=0; $i<count($phone);$i++){
$data = array(
'name'=>$name[$i],
'phone'=>$phone[$i]
);
$this->saveData('phonebook', $data);
}
}