2012-11-12 18 views
0

我有代碼發送數據手機從聯繫人在Android中。正如我們所知道的,手機中的許多數據都在android中聯繫。我有代碼從手機聯繫人數據,並顯示爲列表視圖這樣發送數組數據到服務器在android

Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 

       int row = cursor.getCount(); 
       friend_item = new MenuItem [row]; 
       int i=0; 
       while(cursor.moveToNext()){ 
        nama = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

        friend_item[i] = new MenuItem(nama,phone); 
        i++; 
       } 

List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("phone", mPhoneNumber)); 
       params.add(new BasicNameValuePair("friend", phone)); 

       // getting JSON string from URL 
       JSONObject json = jParser.makeHttpRequest(Constants.url_phone_contact, "POST", params); 
       // Check your log cat for JSON reponse 
       Log.d("All Friend: ", json.toString()); 

       try {      
        friend = json.getJSONArray("friend"); 
        friend_item = new MenuItem[friend.length()]; 
        // looping through All Products 
        for (int a = 0; a < friend.length(); a++) { 
        JSONObject c = friend.getJSONObject(i); 

        //Storing each json item in variable 
        String phone_friend= c.getString(TAG_PHONE); 

        friend_item[i] = new MenuItem(nama, phone_friend); 

        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put("nama", nama); 
        map.put("phone", phone_friend); 

        // adding HashList to ArrayList 
         friendList.add(map); 
        } 


       } catch (JSONException e) { 

       e.printStackTrace(); 

       }    

     return null; 

     } 
params.add(new BasicNameValuePair("friend", phone));

朋友

是數據陣列,從[PHONE_CONTACT]數組。那麼我怎麼能發送數據數組和字符串「mPhoneNumber」到服務器在android?謝謝

回答

0

您可以像這樣發送數據到服務器。

for(int i=0;i<unique_id.length;i++) 
{ 
    params.add(new BasicNameValuePair("unique_id[]",unique_id[i])); 
} 

您需要獲取for循環獲取數組值並將數據分配給namevaluepairs。

+0

Iam新的android,如何將這樣的數據分配給namevaluepairs? –