简体   繁体   中英

In Android : How to retrieve all phone numbers and all email(primary , work, other) of all my contacts

I need to retrive all contacts with all phone numbers , email ids in a json object like(sample single contact object) :

[{
    "id": 123,
    "displayName": "Name",
    "company": "Company",
    "title": "Title",
    "numbers": [{
        "type": "home",
        "number": "957842"
    }, {
        "type": "work",
        "number": "54654654"
    }, {
        "type": "other",
        "number": "465454"
    }, {
        "type": "other",
        "number": "5465431"
    }, {
        "type": "other",
        "number": "54321"
    }, {
        "type": "other",
        "number": "6546545"
    }],
    "emails": [{
        "type": "home",
        "email": "asd@gmail.com"
    }, {
        "type": "work",
        "email": "asdas@gmail.com"
    }, {
        "type": "other",
        "email": "asdasd@gmail.com"
    }, {
        "type": "other",
        "email": "sdasdmail.com"
    }]
}]

Snippet from my current code looks like :

String searchQuery = "_id  > " + lastReadId;
            Cursor contactUrl = cContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,searchQuery,null,"_id ASC");
            Log.e(TAG, "getContact >> " + contactUrl.getCount());
            while (contactUrl.moveToNext())
            {
                objChild = new JSONObject();
                String name=contactUrl.getString(contactUrl.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String number=contactUrl.getString(contactUrl.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                int phoneContactID = contactUrl.getInt(contactUrl.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
                int phone_type = contactUrl.getInt(contactUrl.getColumnIndex(Phone.TYPE));
                objChild.put("name",name);
                objChild.put("number",number);
                objChild.put("phone_type", number);
                Log.e(TAG, "objChild : " + objChild.toString());

My current contact is like :

http://i.stack.imgur.com/MNuWH.png

http://i.stack.imgur.com/fs8Qk.png

Whatever code I got in stackoverflow was giving me single phone number of contacts or multiple contacts which have multiple numbers(I can manipulate same but it will take good time and may lead to performance issue)

My requirement is to get all phone numbers & emailids of a single contact.

I will suggest you to use Gson in your project for easy JSON parsing

Step 1

Open jsonschema2pojo website.

Ste 2

Copy/Paste your JSON string in given space and select below options to use with Gson.

  • Enter packagename and classname as per your requirement
  • select JSON as source type
  • select Gson as annotation style

在此处输入图片说明

Step 3

Click on Preview button to get ready made classes. Copy paste full classes in your application. Else you can also select jar to get classes.

Ste 4

Download and Add Gson library to your project

Download Gson : google-gson

Step 5

Final step. Write below short line of code to convert JSON string to class objects

Gson gson = new Gson();
List<PersonBean> details = gson.fromJson(strJsonData, PersonBean.class);

References :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM