简体   繁体   English

在Android中:如何检索我所有联系人的所有电话号码和所有电子邮件(主要,工作或其他)

[英]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) : 我需要在json对象中(例如单个联系人对象)检索具有所有电话号码和电子邮件ID的所有联系人:

[{
    "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/MNuWH.png

http://i.stack.imgur.com/fs8Qk.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) 无论我在stackoverflow中得到什么代码,都给了我一个联系人的电话号码或多个联系人的多个电话号码(我可以操纵相同的电话,但这会花费很多时间,并可能导致性能问题)

My requirement is to get all phone numbers & emailids of a single contact. 我的要求是获取单个联系人的所有电话号码和电子邮件ID。

I will suggest you to use Gson in your project for easy JSON parsing 我建议您在项目中使用Gson来轻松进行JSON解析

Step 1 第1步

Open jsonschema2pojo website. 打开jsonschema2pojo网站。

Ste 2 Ste 2

Copy/Paste your JSON string in given space and select below options to use with Gson. 在给定的空间中复制/粘贴您的JSON字符串,然后选择以下与Gson一起使用的选项。

  • Enter packagename and classname as per your requirement 根据您的要求输入包名称和类名称
  • select JSON as source type 选择JSON作为源类型
  • select Gson as annotation style 选择Gson作为注释样式

在此处输入图片说明

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. 否则,您也可以选择jar来获取课程。

Ste 4 Ste 4

Download and Add Gson library to your project 下载Gson库并将其添加到您的项目

Download Gson : google-gson 下载Gson: google-gson

Step 5 第5步

Final step. 最后一步。 Write below short line of code to convert JSON string to class objects 在下面的短代码行中编写以将JSON字符串转换为类对象

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

References : 参考文献:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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