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:io.github.grahambell.taco.TacoTransport.java

/**
 * Write a message to the output stream.
 *
 * @param message the message to be written
 * @throws TacoException on error converting the message to JSON or
 *     writing it to the output stream/*from w w w  . ja  va 2s.co  m*/
 */
public void write(Map<String, Object> message) throws TacoException {
    try {
        JSONObject json = mapToJson(message);
        json.write(out);
        out.write("\n// END\n");
        out.flush();
    } catch (JSONException e) {
        throw new TacoException("json write error: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new TacoException("i/o write error: " + e.getMessage(), e);
    }
}

From source file:com.ijiaban.uitls.AbstractGetNameTask.java

@Override
protected Void doInBackground(Void... params) {
    try {/*from   w ww . j a  v a 2  s  . c o  m*/
        fetchNameFromProfileServer();
        fetchImageFromProfileServer();
        fetchSubsChannelsfrmpprofileServer();
    } catch (IOException ex) {
        onError("Following Error occured, please try again. " + ex.getMessage(), ex);
    } catch (JSONException e) {
        onError("Bad response: " + e.getMessage(), e);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:sh.calaba.driver.server.DriverRegistrationHandler.java

private JSONObject getNodeConfig() {
    JSONObject res = new JSONObject();
    try {// w w  w. ja v  a  2 s.com
        res.put("class", "org.openqa.grid.common.RegistrationRequest");
        res.put("configuration", getConfiguration());
        JSONArray caps = new JSONArray();
        for (CalabashCapabilities c : config.getCapabilities()) {
            caps.put(c.getRawCapabilities());
        }
        res.put("capabilities", caps);
    } catch (JSONException e) {
        throw new CalabashException("Error encoding to JSON " + e.getMessage(), e);
    }

    return res;
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

@Override
/** /* ww  w .  j  a v a  2  s  .  c  o  m*/
 * This method takes the fields required and search options in order to produce an 
 * array of contacts that matches the criteria provided.
 * @param fields an array of items to be used as search criteria
 * @param options that can be applied to contact searching
 * @return an array of contacts 
 */
public JSONArray search(JSONArray fields, JSONObject options) {
    String searchTerm = "";
    int limit = Integer.MAX_VALUE;
    boolean multiple = true;

    if (options != null) {
        searchTerm = options.optString("filter");
        if (searchTerm.length() == 0) {
            searchTerm = "%";
        } else {
            searchTerm = "%" + searchTerm + "%";
        }
        try {
            multiple = options.getBoolean("multiple");
            if (!multiple) {
                limit = 1;
            }
        } catch (JSONException e) {
            // Multiple was not specified so we assume the default is true.
        }
    } else {
        searchTerm = "%";
    }

    ContentResolver cr = mApp.getContentResolver();

    Set<String> contactIds = buildSetOfContactIds(fields, searchTerm);
    HashMap<String, Boolean> populate = buildPopulationSet(fields);

    Iterator<String> it = contactIds.iterator();

    JSONArray contacts = new JSONArray();
    JSONObject contact;
    String contactId;
    int pos = 0;
    while (it.hasNext() && (pos < limit)) {
        contact = new JSONObject();
        try {
            contactId = it.next();
            contact.put("id", contactId);

            // Do query for name and note
            Cursor cur = cr.query(People.CONTENT_URI, new String[] { People.DISPLAY_NAME, People.NOTES },
                    PEOPLE_ID_EQUALS, new String[] { contactId }, null);
            cur.moveToFirst();

            if (isRequired("displayName", populate)) {
                contact.put("displayName", cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
            }
            if (isRequired("phoneNumbers", populate)) {
                contact.put("phoneNumbers", phoneQuery(cr, contactId));
            }
            if (isRequired("emails", populate)) {
                contact.put("emails", emailQuery(cr, contactId));
            }
            if (isRequired("addresses", populate)) {
                contact.put("addresses", addressQuery(cr, contactId));
            }
            if (isRequired("organizations", populate)) {
                contact.put("organizations", organizationQuery(cr, contactId));
            }
            if (isRequired("ims", populate)) {
                contact.put("ims", imQuery(cr, contactId));
            }
            if (isRequired("note", populate)) {
                contact.put("note", cur.getString(cur.getColumnIndex(People.NOTES)));
            }
            // nickname
            // urls
            // relationship
            // birthdays
            // anniversary

            pos++;
            cur.close();
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
        contacts.put(contact);
    }
    return contacts;
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

/**
 * Query the database using the search term to build up a list of contact ID's 
 * matching the search term//from ww w. j  a v  a  2s. c  om
 * @param fields
 * @param searchTerm
 * @return a set of contact ID's
 */
private Set<String> buildSetOfContactIds(JSONArray fields, String searchTerm) {
    Set<String> contactIds = new HashSet<String>();

    String key;
    try {
        for (int i = 0; i < fields.length(); i++) {
            key = fields.getString(i);
            if (key.startsWith("displayName")) {
                doQuery(searchTerm, contactIds, People.CONTENT_URI, People._ID, dbMap.get(key) + " LIKE ?",
                        new String[] { searchTerm });
            }
            //            else if (key.startsWith("name")) {
            //               Log.d(LOG_TAG, "Doing " + key + " query");
            //               doQuery(searchTerm, contactIds,
            //                     ContactsContract.Data.CONTENT_URI,
            //                     ContactsContract.Data.CONTACT_ID,
            //                     dbMap.get(key) + " LIKE ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
            //                     new String[] {searchTerm, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE});
            //            }
            else if (key.startsWith("phoneNumbers")) {
                doQuery(searchTerm, contactIds, Phones.CONTENT_URI, Phones.PERSON_ID,
                        dbMap.get(key) + " LIKE ?", new String[] { searchTerm });
            } else if (key.startsWith("emails")) {
                doQuery(searchTerm, contactIds, ContactMethods.CONTENT_EMAIL_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
                        new String[] { searchTerm, ContactMethods.CONTENT_EMAIL_ITEM_TYPE });
            } else if (key.startsWith("addresses")) {
                doQuery(searchTerm, contactIds, ContactMethods.CONTENT_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
                        new String[] { searchTerm, ContactMethods.CONTENT_POSTAL_ITEM_TYPE });
            } else if (key.startsWith("ims")) {
                doQuery(searchTerm, contactIds, ContactMethods.CONTENT_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ? AND " + ContactMethods.KIND + " = ?",
                        new String[] { searchTerm, ContactMethods.CONTENT_IM_ITEM_TYPE });
            } else if (key.startsWith("organizations")) {
                doQuery(searchTerm, contactIds, Organizations.CONTENT_URI, ContactMethods.PERSON_ID,
                        dbMap.get(key) + " LIKE ?", new String[] { searchTerm });
            } else if (key.startsWith("note")) {
                doQuery(searchTerm, contactIds, People.CONTENT_URI, People._ID, dbMap.get(key) + " LIKE ?",
                        new String[] { searchTerm });
            }
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
    }

    return contactIds;
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

/**
 * Create a ContactField JSONArray/*from  w w  w.j  ava2  s .c o  m*/
 * @param cr database access object
 * @param contactId the ID to search the database for
 * @return a JSONArray representing a set of ContactFields
 */
private JSONArray imQuery(ContentResolver cr, String contactId) {
    String imWhere = ContactMethods.PERSON_ID + " = ? AND " + ContactMethods.KIND + " = ?";
    String[] imWhereParams = new String[] { contactId, ContactMethods.CONTENT_IM_ITEM_TYPE };
    Cursor cursor = cr.query(ContactMethods.CONTENT_URI, null, imWhere, imWhereParams, null);
    JSONArray ims = new JSONArray();
    JSONObject im;
    while (cursor.moveToNext()) {
        im = new JSONObject();
        try {
            im.put("id", cursor.getString(cursor.getColumnIndex(ContactMethods._ID)));
            im.put("perf", false);
            im.put("value", cursor.getString(cursor.getColumnIndex(ContactMethodsColumns.DATA)));
            im.put("type", getContactType(cursor.getInt(cursor.getColumnIndex(ContactMethodsColumns.TYPE))));
            ims.put(im);
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
    }
    cursor.close();
    return null;
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

/**
 * Create a ContactOrganization JSONArray
 * @param cr database access object//from   w w  w. ja va2s.  co  m
 * @param contactId the ID to search the database for
 * @return a JSONArray representing a set of ContactOrganization
 */
private JSONArray organizationQuery(ContentResolver cr, String contactId) {
    String orgWhere = ContactMethods.PERSON_ID + " = ?";
    String[] orgWhereParams = new String[] { contactId };
    Cursor cursor = cr.query(Organizations.CONTENT_URI, null, orgWhere, orgWhereParams, null);
    JSONArray organizations = new JSONArray();
    JSONObject organization;
    while (cursor.moveToNext()) {
        organization = new JSONObject();
        try {
            organization.put("id", cursor.getString(cursor.getColumnIndex(Organizations._ID)));
            organization.put("name", cursor.getString(cursor.getColumnIndex(Organizations.COMPANY)));
            organization.put("title", cursor.getString(cursor.getColumnIndex(Organizations.TITLE)));
            // organization.put("department", cursor.getString(cursor.getColumnIndex(Organizations)));
            organizations.put(organization);
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
    }
    return organizations;
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

/**
 * Create a ContactAddress JSONArray/*from  ww w.  ja v  a2s  .c  om*/
 * @param cr database access object
 * @param contactId the ID to search the database for
 * @return a JSONArray representing a set of ContactAddress
 */
private JSONArray addressQuery(ContentResolver cr, String contactId) {
    String addrWhere = ContactMethods.PERSON_ID + " = ? AND " + ContactMethods.KIND + " = ?";
    String[] addrWhereParams = new String[] { contactId, ContactMethods.CONTENT_POSTAL_ITEM_TYPE };
    Cursor cursor = cr.query(ContactMethods.CONTENT_URI, null, addrWhere, addrWhereParams, null);
    JSONArray addresses = new JSONArray();
    JSONObject address;
    while (cursor.moveToNext()) {
        address = new JSONObject();
        try {
            address.put("id", cursor.getString(cursor.getColumnIndex(ContactMethods._ID)));
            address.put("formatted", cursor.getString(cursor.getColumnIndex(ContactMethodsColumns.DATA)));
            addresses.put(address);
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
    }
    return addresses;
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

/**
 * Create a ContactField JSONArray/*from w ww.j  a  v a2 s  . c o  m*/
 * @param cr database access object
 * @param contactId the ID to search the database for
 * @return a JSONArray representing a set of ContactFields
 */
private JSONArray phoneQuery(ContentResolver cr, String contactId) {
    Cursor cursor = cr.query(Phones.CONTENT_URI, null, Phones.PERSON_ID + " = ?", new String[] { contactId },
            null);
    JSONArray phones = new JSONArray();
    JSONObject phone;
    while (cursor.moveToNext()) {
        phone = new JSONObject();
        try {
            phone.put("id", cursor.getString(cursor.getColumnIndex(Phones._ID)));
            phone.put("perf", false);
            phone.put("value", cursor.getString(cursor.getColumnIndex(Phones.NUMBER)));
            phone.put("type", getPhoneType(cursor.getInt(cursor.getColumnIndex(Phones.TYPE))));
            phones.put(phone);
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
    }
    return phones;
}

From source file:com.phonegap.ContactAccessorSdk3_4.java

/**
 * Create a ContactField JSONArray//from  ww  w .j  av a  2 s .  c o  m
 * @param cr database access object
 * @param contactId the ID to search the database for
 * @return a JSONArray representing a set of ContactFields
 */
private JSONArray emailQuery(ContentResolver cr, String contactId) {
    Cursor cursor = cr.query(ContactMethods.CONTENT_EMAIL_URI, null, ContactMethods.PERSON_ID + " = ?",
            new String[] { contactId }, null);
    JSONArray emails = new JSONArray();
    JSONObject email;
    while (cursor.moveToNext()) {
        email = new JSONObject();
        try {
            email.put("id", cursor.getString(cursor.getColumnIndex(ContactMethods._ID)));
            email.put("perf", false);
            email.put("value", cursor.getString(cursor.getColumnIndex(ContactMethods.DATA)));
            // TODO Find out why adding an email type throws and exception
            //email.put("type", cursor.getString(cursor.getColumnIndex(ContactMethods.TYPE)));
            emails.put(email);
        } catch (JSONException e) {
            Log.e(LOG_TAG, e.getMessage(), e);
        }
    }
    return emails;
}