download1323 2016-11-20 16:27
浏览 83

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

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

    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c