Android将完整数组发布到php

时间:2016-12-08 09:01:01

标签: android arrays arraylist

我正在从android手机中检索联系人,然后想要将数据作为数组发送到php并循环结果并发送,相反,它只找到最后一个联系人。关于如何发布整个阵列的任何建议。

这是我目前的代码

public class MainActivity extends AppCompatActivity {

    ArrayList alContacts;
    ArrayList alNumbers;
    Button button;
    EditText Name,Email;
    Map<String,String> param;
    ListView lv;
    Cursor cr;
    String URL ="http://192.168.0.10/oap/home/synchcontacts";
    AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.save);
        Name= (EditText) findViewById(R.id.name);
        Email = (EditText) findViewById(R.id.email);
        builder = new AlertDialog.Builder(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // getDetails();
                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>();
                            alNumbers = 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);
                                       alNumbers.add(contactNumber);

                                        break;
                                    }
                                    pCur.close();
                                }


                            } while (cursor.moveToNext()) ;
                            List<NameValuePair> param = new ArrayList<NameValuePair>();
                            Gson gson = new GsonBuilder().create();
                            JsonArray myContacts = gson.toJsonTree(alContacts).getAsJsonArray();
                            JsonArray myNumbers = gson.toJsonTree(alNumbers).getAsJsonArray();
                            for (int i=0; i<myContacts.size();i++){
                                param.add(new BasicNameValuePair("name[]",myContacts.get(i).toString()));
                                param.add(new BasicNameValuePair("phone[]",myNumbers.get(i).toString()));

                            }
                             Log.i("PARAM",param.toString());
                        }

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

1 个答案:

答案 0 :(得分:0)

您的问题是您正在使用HashMap传递值。而对于hashMap,您只存储最后一个值,因为您不能拥有2个具有相同ID的项目。

您可能希望在内部使用类似列表的内容,然后从中呈现请求。

例如item as(org.apache.http.NameValuePair):

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("id[]", "id1"));
    urlParameters.add(new BasicNameValuePair("id[]", "id2"));
    ....

所以,在你的情况下,这将是:

        ....
        List<NameValuePair> param = new ArrayList<NameValuePair>();
        Gson gson = new GsonBuilder().create();
        JsonArray myContacts = gson.toJsonTree(alContacts).getAsJsonArray();
        JsonArray myNumbers = gson.toJsonTree(alNumbers).getAsJsonArray();
        for (int i=0; i<myContacts.size();i++){
            param.add(new BasicNameValuePair("name[]",myContacts.get(i).toString()));
            param.add(new BasicNameValuePair("phone[]",myNumbers.get(i).toString()));
        }

        return param;

编辑:

看到整个剪辑后,似乎您受到正在使用的库的限制。我想,你会在以下链接中找到答案:

Google Volley request with duplicate parameter names

或者在这里:

https://groups.google.com/forum/#!topic/volley-users/tFRclnEbpAk