Example usage for org.json JSONException getMessage

List of usage examples for org.json JSONException getMessage

Introduction

In this page you can find the example usage for org.json JSONException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Create a ContactName JSONObject//from w ww.ja  v a  2 s .  co m
 * @param cursor the current database row
 * @return a JSONObject representing a ContactName
 */
private JSONObject nameQuery(Cursor cursor) {
    JSONObject contactName = new JSONObject();
    try {
        String familyName = cursor
                .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
        String givenName = cursor
                .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
        String middleName = cursor
                .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME));
        String honorificPrefix = cursor
                .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX));
        String honorificSuffix = cursor
                .getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX));

        // Create the formatted name
        StringBuffer formatted = new StringBuffer("");
        if (honorificPrefix != null) {
            formatted.append(honorificPrefix + " ");
        }
        if (givenName != null) {
            formatted.append(givenName + " ");
        }
        if (middleName != null) {
            formatted.append(middleName + " ");
        }
        if (familyName != null) {
            formatted.append(familyName + " ");
        }
        if (honorificSuffix != null) {
            formatted.append(honorificSuffix + " ");
        }

        contactName.put("familyName", familyName);
        contactName.put("givenName", givenName);
        contactName.put("middleName", middleName);
        contactName.put("honorificPrefix", honorificPrefix);
        contactName.put("honorificSuffix", honorificSuffix);
        contactName.put("formatted", formatted);
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return contactName;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Create a ContactField JSONObject/*from w ww .  ja  va  2  s .c o  m*/
 * @param cursor the current database row
 * @return a JSONObject representing a ContactField
 */
private JSONObject phoneQuery(Cursor cursor) {
    JSONObject phoneNumber = new JSONObject();
    try {
        phoneNumber.put("id",
                cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)));
        phoneNumber.put("pref", false); // Android does not store pref attribute
        phoneNumber.put("value",
                cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
        phoneNumber.put("type", getPhoneType(
                cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE))));
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    } catch (Exception excp) {
        Log.e(LOG_TAG, excp.getMessage(), excp);
    }
    return phoneNumber;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Create a ContactField JSONObject//from   ww  w  .j a  va2s.c  o  m
 * @param cursor the current database row
 * @return a JSONObject representing a ContactField
 */
private JSONObject emailQuery(Cursor cursor) {
    JSONObject email = new JSONObject();
    try {
        email.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email._ID)));
        email.put("pref", false); // Android does not store pref attribute
        email.put("value",
                cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)));
        email.put("type", getContactType(
                cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE))));
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return email;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Create a ContactField JSONObject//from   ww w . ja  v  a 2  s. c  o  m
 * @param cursor the current database row
 * @return a JSONObject representing a ContactField
 */
private JSONObject imQuery(Cursor cursor) {
    JSONObject im = new JSONObject();
    try {
        im.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im._ID)));
        im.put("pref", false); // Android does not store pref attribute
        im.put("value", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)));
        im.put("type",
                getContactType(cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE))));
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return im;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Create a ContactField JSONObject/*  w  ww . j  a  v  a2 s.c o  m*/
 * @param cursor the current database row
 * @return a JSONObject representing a ContactField
 */
private JSONObject websiteQuery(Cursor cursor) {
    JSONObject website = new JSONObject();
    try {
        website.put("id",
                cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website._ID)));
        website.put("pref", false); // Android does not store pref attribute
        website.put("value",
                cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL)));
        website.put("type", getContactType(
                cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE))));
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return website;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Create a ContactField JSONObject//from  w w  w .j  a  v a 2s . c  o m
 * @param contactId 
 * @return a JSONObject representing a ContactField
 */
private JSONObject photoQuery(Cursor cursor, String contactId) {
    JSONObject photo = new JSONObject();
    try {
        photo.put("id", cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo._ID)));
        photo.put("pref", false);
        photo.put("type", "url");
        Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (new Long(contactId)));
        Uri photoUri = Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
        photo.put("value", photoUri.toString());
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }
    return photo;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Creates a new contact and stores it in the database
 * //from  w ww .ja va 2 s  .c om
 * @param id the raw contact id which is required for linking items to the contact
 * @param contact the contact to be saved
 * @param account the account to be saved under
 */
private boolean modifyContact(String id, JSONObject contact, Account account) {
    // Get the RAW_CONTACT_ID which is needed to insert new values in an already existing contact.
    // But not needed to update existing values.
    int rawId = (new Integer(getJsonString(contact, "rawId"))).intValue();

    // Create a list of attributes to add to the contact database
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    //Add contact type
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name).build());

    // Modify name
    JSONObject name;
    try {
        String displayName = getJsonString(contact, "displayName");
        name = contact.getJSONObject("name");
        if (displayName != null || name != null) {
            ContentProviderOperation.Builder builder = ContentProviderOperation
                    .newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
                                    + "=?",
                            new String[] { id,
                                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE });

            if (displayName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName);
            }

            String familyName = getJsonString(name, "familyName");
            if (familyName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, familyName);
            }
            String middleName = getJsonString(name, "middleName");
            if (middleName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, middleName);
            }
            String givenName = getJsonString(name, "givenName");
            if (givenName != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, givenName);
            }
            String honorificPrefix = getJsonString(name, "honorificPrefix");
            if (honorificPrefix != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX, honorificPrefix);
            }
            String honorificSuffix = getJsonString(name, "honorificSuffix");
            if (honorificSuffix != null) {
                builder.withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX, honorificSuffix);
            }

            ops.add(builder.build());
        }
    } catch (JSONException e1) {
        Log.d(LOG_TAG, "Could not get name");
    }

    // Modify phone numbers
    JSONArray phones = null;
    try {
        phones = contact.getJSONArray("phoneNumbers");
        if (phones != null) {
            for (int i = 0; i < phones.length(); i++) {
                JSONObject phone = (JSONObject) phones.get(i);
                String phoneId = getJsonString(phone, "id");
                // This is a new phone so do a DB insert
                if (phoneId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER,
                            getJsonString(phone, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE,
                            getPhoneType(getJsonString(phone, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing phone so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Phone._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { phoneId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                                    getJsonString(phone, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                                    getPhoneType(getJsonString(phone, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get phone numbers");
    }

    // Modify emails
    JSONArray emails = null;
    try {
        emails = contact.getJSONArray("emails");
        if (emails != null) {
            for (int i = 0; i < emails.length(); i++) {
                JSONObject email = (JSONObject) emails.get(i);
                String emailId = getJsonString(email, "id");
                // This is a new email so do a DB insert
                if (emailId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Email.DATA,
                            getJsonString(email, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Email.TYPE,
                            getContactType(getJsonString(email, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing email so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Email._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { emailId, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Email.DATA,
                                    getJsonString(email, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                                    getContactType(getJsonString(email, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Modify addresses
    JSONArray addresses = null;
    try {
        addresses = contact.getJSONArray("addresses");
        if (addresses != null) {
            for (int i = 0; i < addresses.length(); i++) {
                JSONObject address = (JSONObject) addresses.get(i);
                String addressId = getJsonString(address, "id");
                // This is a new address so do a DB insert
                if (addressId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                            getJsonString(address, "formatted"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET,
                            getJsonString(address, "streetAddress"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY,
                            getJsonString(address, "locality"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION,
                            getJsonString(address, "region"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
                            getJsonString(address, "postalCode"));
                    contentValues.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
                            getJsonString(address, "country"));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing address so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.StructuredPostal._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { addressId,
                                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                                    getJsonString(address, "formatted"))
                            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET,
                                    getJsonString(address, "streetAddress"))
                            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY,
                                    getJsonString(address, "locality"))
                            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION,
                                    getJsonString(address, "region"))
                            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
                                    getJsonString(address, "postalCode"))
                            .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
                                    getJsonString(address, "country"))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get addresses");
    }

    // Modify organizations
    JSONArray organizations = null;
    try {
        organizations = contact.getJSONArray("organizations");
        if (organizations != null) {
            for (int i = 0; i < organizations.length(); i++) {
                JSONObject org = (JSONObject) organizations.get(i);
                String orgId = getJsonString(org, "id");
                ;
                // This is a new organization so do a DB insert
                if (orgId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
                            getJsonString(org, "department"));
                    contentValues.put(ContactsContract.CommonDataKinds.Organization.COMPANY,
                            getJsonString(org, "name"));
                    contentValues.put(ContactsContract.CommonDataKinds.Organization.TITLE,
                            getJsonString(org, "title"));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing organization so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(
                                    ContactsContract.CommonDataKinds.Organization._ID + "=? AND "
                                            + ContactsContract.Data.MIMETYPE + "=?",
                                    new String[] { orgId,
                                            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT,
                                    getJsonString(org, "department"))
                            .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,
                                    getJsonString(org, "name"))
                            .withValue(ContactsContract.CommonDataKinds.Organization.TITLE,
                                    getJsonString(org, "title"))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get organizations");
    }

    // Modify IMs
    JSONArray ims = null;
    try {
        ims = contact.getJSONArray("ims");
        if (ims != null) {
            for (int i = 0; i < ims.length(); i++) {
                JSONObject im = (JSONObject) ims.get(i);
                String imId = getJsonString(im, "id");
                ;
                // This is a new IM so do a DB insert
                if (imId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Im.TYPE,
                            getContactType(getJsonString(im, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing IM so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Im._ID + "=? AND " + ContactsContract.Data.MIMETYPE
                                    + "=?",
                            new String[] { imId, ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Im.DATA, getJsonString(im, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Im.TYPE,
                                    getContactType(getJsonString(im, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Modify note
    String note = getJsonString(contact, "note");
    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
                    new String[] { id, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE })
            .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build());

    // Modify nickname
    String nickname = getJsonString(contact, "nickname");
    if (nickname != null) {
        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(
                        ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?",
                        new String[] { id, ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE })
                .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build());
    }

    // Modify urls   
    JSONArray websites = null;
    try {
        websites = contact.getJSONArray("websites");
        if (websites != null) {
            for (int i = 0; i < websites.length(); i++) {
                JSONObject website = (JSONObject) websites.get(i);
                String websiteId = getJsonString(website, "id");
                ;
                // This is a new website so do a DB insert
                if (websiteId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.CommonDataKinds.Website.DATA,
                            getJsonString(website, "value"));
                    contentValues.put(ContactsContract.CommonDataKinds.Website.TYPE,
                            getContactType(getJsonString(website, "type")));

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing website so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                            .withSelection(
                                    ContactsContract.CommonDataKinds.Website._ID + "=? AND "
                                            + ContactsContract.Data.MIMETYPE + "=?",
                                    new String[] { websiteId,
                                            ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.CommonDataKinds.Website.DATA,
                                    getJsonString(website, "value"))
                            .withValue(ContactsContract.CommonDataKinds.Website.TYPE,
                                    getContactType(getJsonString(website, "type")))
                            .build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get websites");
    }

    // Modify birthday
    String birthday = getJsonString(contact, "birthday");
    if (birthday != null) {
        ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
                .withSelection(
                        ContactsContract.Data.CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE
                                + "=? AND " + ContactsContract.CommonDataKinds.Event.TYPE + "=?",
                        new String[] { id, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
                                new String("" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) })
                .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build());
    }

    // Modify photos
    JSONArray photos = null;
    try {
        photos = contact.getJSONArray("photos");
        if (photos != null) {
            for (int i = 0; i < photos.length(); i++) {
                JSONObject photo = (JSONObject) photos.get(i);
                String photoId = getJsonString(photo, "id");
                byte[] bytes = getPhotoBytes(getJsonString(photo, "value"));
                // This is a new photo so do a DB insert
                if (photoId == null) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
                    contentValues.put(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
                    contentValues.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
                    contentValues.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);

                    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                            .withValues(contentValues).build());
                }
                // This is an existing photo so do a DB update
                else {
                    ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI).withSelection(
                            ContactsContract.CommonDataKinds.Photo._ID + "=? AND "
                                    + ContactsContract.Data.MIMETYPE + "=?",
                            new String[] { photoId, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE })
                            .withValue(ContactsContract.Data.IS_SUPER_PRIMARY, 1)
                            .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes).build());
                }
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get photos");
    }

    boolean retVal = true;

    //Modify contact
    try {
        mApp.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        Log.e(LOG_TAG, Log.getStackTraceString(e), e);
        retVal = false;
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        Log.e(LOG_TAG, Log.getStackTraceString(e), e);
        retVal = false;
    }

    return retVal;
}

From source file:com.phonegap.ContactAccessorSdk5.java

/**
 * Creates a new contact and stores it in the database
 * //w w  w .  j a  va2s .c om
 * @param contact the contact to be saved
 * @param account the account to be saved under
 */
private boolean createNewContact(JSONObject contact, Account account) {
    // Create a list of attributes to add to the contact database
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

    //Add contact type
    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name).build());

    // Add name
    try {
        JSONObject name = contact.optJSONObject("name");
        String displayName = contact.getString("displayName");
        if (displayName != null || name != null) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, displayName)
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                            getJsonString(name, "familyName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
                            getJsonString(name, "middleName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                            getJsonString(name, "givenName"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.PREFIX,
                            getJsonString(name, "honorificPrefix"))
                    .withValue(ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
                            getJsonString(name, "honorificSuffix"))
                    .build());
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get name object");
    }

    //Add phone numbers
    JSONArray phones = null;
    try {
        phones = contact.getJSONArray("phoneNumbers");
        if (phones != null) {
            for (int i = 0; i < phones.length(); i++) {
                JSONObject phone = (JSONObject) phones.get(i);
                insertPhone(ops, phone);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get phone numbers");
    }

    // Add emails
    JSONArray emails = null;
    try {
        emails = contact.getJSONArray("emails");
        if (emails != null) {
            for (int i = 0; i < emails.length(); i++) {
                JSONObject email = (JSONObject) emails.get(i);
                insertEmail(ops, email);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add addresses
    JSONArray addresses = null;
    try {
        addresses = contact.getJSONArray("addresses");
        if (addresses != null) {
            for (int i = 0; i < addresses.length(); i++) {
                JSONObject address = (JSONObject) addresses.get(i);
                insertAddress(ops, address);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get addresses");
    }

    // Add organizations
    JSONArray organizations = null;
    try {
        organizations = contact.getJSONArray("organizations");
        if (organizations != null) {
            for (int i = 0; i < organizations.length(); i++) {
                JSONObject org = (JSONObject) organizations.get(i);
                insertOrganization(ops, org);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get organizations");
    }

    // Add IMs
    JSONArray ims = null;
    try {
        ims = contact.getJSONArray("ims");
        if (ims != null) {
            for (int i = 0; i < ims.length(); i++) {
                JSONObject im = (JSONObject) ims.get(i);
                insertIm(ops, im);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get emails");
    }

    // Add note
    String note = getJsonString(contact, "note");
    if (note != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Note.NOTE, note).build());
    }

    // Add nickname
    String nickname = getJsonString(contact, "nickname");
    if (nickname != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Nickname.NAME, nickname).build());
    }

    // Add urls   
    JSONArray websites = null;
    try {
        websites = contact.getJSONArray("websites");
        if (websites != null) {
            for (int i = 0; i < websites.length(); i++) {
                JSONObject website = (JSONObject) websites.get(i);
                insertWebsite(ops, website);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get websites");
    }

    // Add birthday
    String birthday = getJsonString(contact, "birthday");
    if (birthday != null) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
                .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthday).build());
    }

    // Add photos
    JSONArray photos = null;
    try {
        photos = contact.getJSONArray("photos");
        if (photos != null) {
            for (int i = 0; i < photos.length(); i++) {
                JSONObject photo = (JSONObject) photos.get(i);
                insertPhoto(ops, photo);
            }
        }
    } catch (JSONException e) {
        Log.d(LOG_TAG, "Could not get photos");
    }

    boolean retVal = true;
    //Add contact
    try {
        mApp.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (RemoteException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        retVal = false;
    } catch (OperationApplicationException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        retVal = false;
    }

    return retVal;
}

From source file:com.iStudy.Study.Renren.Util.java

/**
 * ?JSON?RenrenError.//from  w  w w .j  a  v  a 2 s .  co m
 * 
 * @param JSON
 * @return
 */
private static RenrenError parseJson(String jsonResponse) {
    try {
        JSONObject json = new JSONObject(jsonResponse);

        int errorCode = json.getInt("error_code");
        String errorMessage = json.getString("error_msg");
        errorMessage = RenrenError.interpretErrorMessage(errorCode, errorMessage);

        return new RenrenError(errorCode, errorMessage, jsonResponse);
    } catch (JSONException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:org.lafs.hdfs.LAFS.java

private JSONArray getJSONForPath(Path path) throws IOException {

    URL url = new URL(httpURI.toString() + "/uri/" + getLAFSPath(path) + "?t=json");

    URLConnection uc = url.openConnection();
    uc.setDoInput(true);/* ww  w  .ja  v a2s . c o  m*/

    BufferedReader br;
    try {
        br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
    } catch (ConnectException e1) {
        logger.severe(url.toString());

        throw new IOException(e1);
    }

    StringBuilder sb = new StringBuilder();

    String line = br.readLine();
    while (line != null) {
        sb.append(line);
        line = br.readLine();
    }

    br.close();

    //logger.info(sb.toString());

    JSONArray ja;

    try {
        ja = new JSONArray(new JSONTokener(sb.toString()));
    } catch (JSONException e) {
        throw new IOException(e.getMessage());
    }

    return ja;
}