从android提取联系人并保存到数据库

时间:2016-11-20 16:27:08

标签: java php android arrays

到目前为止,我已经能够提取数字和名称的联系人,我想要做的是通过php将数据发送到数据库。到目前为止这么好,相反它只发送数组中的最后一个,有关如何发送数组并保存整个提取的联系人的任何建议吗?

爪哇

 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);
       }

    }

0 个答案:

没有答案