Reputation: 29
i'm new to android and probably this is silly question but please help. i am receiving output as
contact Name: RRRR
phone Number: XXXXXXXXX
contact Name: SSSS
phone Number: YYYYYYYYY
phone Number: aaaaaaaaa
phone Number: zzzzzzzzz
contact Name: TTTT
phone Number: XXXXXXXXX
phone Number: ccccccccc
.
.
.
//code
public void readContacts() {
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String phone = null;
String name = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur .getColumnIndex(ContactsContract.Contacts._ID));
name = cur .getString(cur .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer .parseInt(cur.getString(cur .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id); sb.append("\n Contact Name:" + name);
Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
while (pCur.moveToNext()) {
phone = pCur .getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone+":"+pCur.getCount()); System.out.println("phone" + phone);
}pCur.close();
}
outputText.setText(sb);
}
}
}
P.S: how can i store the ouput in container and then bundle it and send to server ?
thanks in advance,
Upvotes: 0
Views: 661
Reputation: 5097
I tried to put list of contacts in JSONArray,
Step-1 : Declare JSONArray finalJarray; for in future use to send JSONArray to server.
Step-2 : Generate JSONArray with contact name and multiple phone number. Unique key name to multiple phone numbers so that it can not override.
public void readContacts() {
try {
finalJarray = new JSONArray();
StringBuffer sb = new StringBuffer();
sb.append("......Contact Details.....");
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
String phone = null;
String name = null;
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
JSONObject contactObject = new JSONObject();
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
sb.append("\n Contact Name:" + name);
contactObject.put("contact_name", name);
Cursor pCur = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id },
null);
int tmpPhoneCount = 0;
while (pCur.moveToNext()) {
phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
sb.append("\n Phone number:" + phone + ":"
+ pCur.getCount());
System.out.println("phone" + phone);
contactObject.put("phone_number_"+tmpPhoneCount, phone);
tmpPhoneCount++;
}
pCur.close();
finalJarray.put(contactObject);
}
// outputText.setText(sb);
}
}
System.out.println("finalJarray.toString() = "
+ finalJarray.toString());
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
I tried on my device, let me know if any query.
Upvotes: 1