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.prasanna.android.stacknetwork.utils.JSONObjectWrapper.java

public boolean getBoolean(String name) {
    if (jsonObject.has(name)) {
        try {/*ww  w. j a  va  2s  . c  om*/
            return jsonObject.getBoolean(name);
        } catch (JSONException e) {
            LogWrapper.d(TAG, e.getMessage());
        }
    }

    return false;
}

From source file:com.prasanna.android.stacknetwork.utils.JSONObjectWrapper.java

public String getString(String name) {
    if (jsonObject.has(name)) {
        try {/*from www.j av a2 s  . c o  m*/
            return jsonObject.getString(name);
        } catch (JSONException e) {
            LogWrapper.d(TAG, e.getMessage());
        }
    }
    return null;
}

From source file:com.jsonstore.api.JSONStoreQueryPart.java

/**
 * @exclude/*from w ww  .  j  a  v  a2  s.  c o  m*/
 */
private JSONStoreQueryPartItem parseItem(String key, boolean is_key_special, QueryPartOperation operation,
        Object val) throws JSONStoreFindException {
    try {
        switch (operation.getRestriction()) {
        case ARRAY_ONLY:
            if (!(val instanceof JSONArray)) {
                throw new JSONStoreFindException(
                        "Cannot parse query content part. Tuple with key " + key + " must be an array", null);
            }

            JSONArray valAsArray = (JSONArray) val;
            List<Object> convertedObjectsList = new ArrayList<Object>(valAsArray.length());
            //Convert this to a List<Object>
            for (int i = 0; i < valAsArray.length(); i++) {
                try {
                    Object o = valAsArray.get(i);

                    if (!isObjectPrimitive(o)) {
                        throw new JSONStoreFindException("Cannot parse query content part. Tuple with key "
                                + key + " at index " + i + "cannot be an JSONArray or JSONObject", null);
                    }

                    convertedObjectsList.add(o);
                } catch (JSONException e) {
                    throw new JSONStoreFindException(
                            "Cannot parse query content part. An internal error occured", e);
                }
            }
            val = convertedObjectsList; //Replace the JSONArray as a List<Object>
            break;
        case RANGE_ONLY:
            if (!(val instanceof JSONArray)) {
                throw new JSONStoreFindException(
                        "Cannot parse query content part. Tuple with key " + key + " must be an array", null);
            }
            JSONArray arr = (JSONArray) val;
            if (arr.length() != 2) {
                throw new JSONStoreFindException("Cannot parse query content part. Tuple with key " + key
                        + " must be an array of size 2", null);
            }

            try {
                Object firstNumber = arr.get(0);
                Object secondNumber = arr.get(1);

                if (!isObjectPrimitive(firstNumber) || !isObjectPrimitive(secondNumber)) {
                    throw new JSONStoreFindException("Cannot parse query content part. Tuple with key " + key
                            + " must be an array of size 2 primitives", null);
                }

                List<Object> rangeList = new ArrayList<Object>(2);
                rangeList.add(firstNumber);
                rangeList.add(secondNumber);
                val = rangeList;
            } catch (JSONException e) {
                throw new JSONStoreFindException(
                        "Cannot parse query content part. Tuple with key " + key + " caused an internal error ",
                        e);

            }
            break;

        case PRIMITIVE_ONLY:
            if (!isObjectPrimitive(val)) {
                throw new JSONStoreFindException("Cannot parse query content part. Tuple with key " + key
                        + " cannot be an array or object", null);
            }
            break;
        }
    } catch (JSONStoreFindException e) {
        JSONStoreLogger.getLogger("QueryContentPart").logTrace("Invalid syntax: " + e.getMessage()); //$NON-NLS-1$
        return null;
    }

    return new JSONStoreQueryPartItem(key, false, operation, val);

}

From source file:org.dasein.cloud.gogrid.network.ip.GoGridIPSupport.java

@Override
public boolean isSubscribed() throws CloudException, InternalException {
    GoGridMethod method = new GoGridMethod(provider);
    String regionId = getRegionId(getContext());

    JSONArray list = method.get(GoGridMethod.LOOKUP_LIST, new GoGridMethod.Param("lookup", "ip.datacenter"));

    if (list == null) {
        return false;
    }// www. jav a  2s . co  m
    for (int i = 0; i < list.length(); i++) {
        try {
            JSONObject r = list.getJSONObject(i);

            if (r.has("id") && regionId.equals(r.getString("id"))) {
                return true;
            }
        } catch (JSONException e) {
            logger.error("Unable to load data centers from GoGrid: " + e.getMessage());
            e.printStackTrace();
            throw new CloudException(e);
        }
    }
    return false;
}

From source file:org.dasein.cloud.gogrid.network.ip.GoGridIPSupport.java

@Override
public @Nonnull Iterable<IpAddress> listIpPool(@Nonnull IPVersion version, boolean unassignedOnly)
        throws InternalException, CloudException {
    if (version.equals(IPVersion.IPV4)) {
        ProviderContext ctx = getContext();
        String regionId = getRegionId(ctx);

        GoGridMethod method = new GoGridMethod(provider);

        GoGridMethod.Param[] params = new GoGridMethod.Param[unassignedOnly ? 2 : 1];

        params[0] = new GoGridMethod.Param("datacenter", regionId);
        if (unassignedOnly) {
            params[1] = new GoGridMethod.Param("ip.state", "1");
        }/*from   ww w.  j  a  v  a 2  s  .com*/
        JSONArray list = method.get(GoGridMethod.IP_LIST, params);

        if (list == null) {
            return Collections.emptyList();
        }
        ArrayList<IpAddress> addresses = new ArrayList<IpAddress>();
        JSONArray vmList = null;
        JSONArray lbList = null;

        if (!unassignedOnly) {
            vmList = method.get(GoGridMethod.SERVER_LIST);
            lbList = method.get(GoGridMethod.LB_LIST);
        }
        for (int i = 0; i < list.length(); i++) {
            try {
                IpAddress ip = toAddress(list.getJSONObject(i), vmList, lbList);

                if (ip != null) {
                    addresses.add(ip);
                }
            } catch (JSONException e) {
                logger.error("Failed to parse JSON: " + e.getMessage());
                e.printStackTrace();
                throw new CloudException(e);
            }
        }
        return addresses;
    }
    return Collections.emptyList();
}

From source file:org.dasein.cloud.gogrid.network.ip.GoGridIPSupport.java

@Override
public @Nonnull Iterable<ResourceStatus> listIpPoolStatus(@Nonnull IPVersion version)
        throws InternalException, CloudException {
    if (version.equals(IPVersion.IPV4)) {
        ProviderContext ctx = getContext();
        String regionId = getRegionId(ctx);

        GoGridMethod method = new GoGridMethod(provider);

        GoGridMethod.Param[] params = new GoGridMethod.Param[1];

        params[0] = new GoGridMethod.Param("datacenter", regionId);
        JSONArray list = method.get(GoGridMethod.IP_LIST, params);

        if (list == null) {
            return Collections.emptyList();
        }//  ww w. j ava2 s .  c  o  m
        ArrayList<ResourceStatus> addresses = new ArrayList<ResourceStatus>();
        JSONArray vmList = method.get(GoGridMethod.SERVER_LIST);
        JSONArray lbList = method.get(GoGridMethod.LB_LIST);

        for (int i = 0; i < list.length(); i++) {
            try {
                ResourceStatus ip = toStatus(list.getJSONObject(i), vmList, lbList);

                if (ip != null) {
                    addresses.add(ip);
                }
            } catch (JSONException e) {
                logger.error("Failed to parse JSON: " + e.getMessage());
                e.printStackTrace();
                throw new CloudException(e);
            }
        }
        return addresses;
    }
    return Collections.emptyList();
}

From source file:org.dasein.cloud.gogrid.network.ip.GoGridIPSupport.java

private @Nullable IpAddress toAddress(@Nullable JSONObject json, @Nullable JSONArray vmList,
        @Nullable JSONArray lbList) throws CloudException, InternalException {
    if (json == null) {
        return null;
    }/*from  w w w  . j av a 2  s  .c o m*/
    IpAddress address = new IpAddress();

    address.setForVlan(false);
    address.setRegionId(getRegionId(getContext()));
    address.setVersion(IPVersion.IPV4);
    try {
        if (json.has("id") && json.has("ip")) {
            address.setIpAddressId(json.getString("id"));
            address.setAddress(json.getString("ip"));
        } else {
            return null;
        }
        if (json.has("public") && json.getBoolean("public")) {
            address.setAddressType(AddressType.PUBLIC);
        } else {
            address.setAddressType(AddressType.PRIVATE);
        }
        if (json.has("state")) {
            JSONObject state = json.getJSONObject("state");

            if (state.has("id")) {
                int s = state.getInt("id");

                if (s != 1 && (vmList == null || lbList == null)) {
                    return null;
                } else if (s == 2) {
                    for (int i = 0; i < vmList.length(); i++) {
                        JSONObject vm = vmList.getJSONObject(i);

                        if (!vm.has("id")) {
                            continue;
                        }
                        if (vm.has("ip")) {
                            JSONObject ip = vm.getJSONObject("ip");

                            if (ip.has("id") && ip.getString("id").equals(address.getProviderIpAddressId())) {
                                address.setServerId(vm.getString("id"));
                            }
                        }
                    }
                    if (address.getServerId() == null) {
                        for (int i = 0; i < lbList.length(); i++) {
                            JSONObject lb = lbList.getJSONObject(i);

                            if (!lb.has("id")) {
                                continue;
                            }
                            if (lb.has("virtualip.ip")) {
                                JSONObject ip = lb.getJSONObject("virtualip.ip");

                                if (ip.has("id")
                                        && ip.getString("id").equals(address.getProviderIpAddressId())) {
                                    address.setProviderLoadBalancerId(lb.getString("id"));
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (JSONException e) {
        logger.error("Failed to parse JSON from the cloud: " + e.getMessage());
        e.printStackTrace();
        throw new CloudException(e);
    }
    return address;
}

From source file:org.dasein.cloud.gogrid.network.ip.GoGridIPSupport.java

private @Nullable ResourceStatus toStatus(@Nullable JSONObject json, @Nullable JSONArray vmList,
        @Nullable JSONArray lbList) throws CloudException, InternalException {
    if (json == null) {
        return null;
    }/*from  w  w w.  j a v a  2 s  . c  o  m*/
    Boolean available = null;
    String id;

    try {
        if (json.has("id") && json.has("ip")) {
            id = json.getString("id");
        } else {
            return null;
        }
        if (json.has("state")) {
            JSONObject state = json.getJSONObject("state");

            if (state.has("id")) {
                int s = state.getInt("id");

                if (s != 1 && (vmList == null || lbList == null)) {
                    return null;
                } else if (s == 2) {
                    for (int i = 0; i < vmList.length(); i++) {
                        JSONObject vm = vmList.getJSONObject(i);

                        if (!vm.has("id")) {
                            continue;
                        }
                        if (vm.has("ip")) {
                            JSONObject ip = vm.getJSONObject("ip");

                            if (ip.has("id") && ip.getString("id").equals(id)) {
                                available = false;
                            }
                        }
                    }
                    if (available == null) {
                        for (int i = 0; i < lbList.length(); i++) {
                            JSONObject lb = lbList.getJSONObject(i);

                            if (!lb.has("id")) {
                                continue;
                            }
                            if (lb.has("virtualip.ip")) {
                                JSONObject ip = lb.getJSONObject("virtualip.ip");

                                if (ip.has("id") && ip.getString("id").equals(id)) {
                                    available = false;
                                }
                            }
                        }
                    }
                }
            }
        }
        return new ResourceStatus(id, available == null || available);
    } catch (JSONException e) {
        logger.error("Failed to parse JSON from the cloud: " + e.getMessage());
        e.printStackTrace();
        throw new CloudException(e);
    }
}

From source file:com.hotstar.player.adplayer.feeds.ReferencePlayerFeedItemAdapter.java

/**
 * Gets field value from JSON object given field name
 * // ww w. j  a v  a 2  s . com
 * @param jsonObject
 *            - JSON object to search in
 * @param fieldName
 *            - field name to be searched for
 * @return String - field value or null if field name does not exist or
 *         field value is empty
 */
protected String getString(JSONObject jsonObject, String fieldName) {
    /*
    try {
       if (jsonObject.has(fieldName) && !jsonObject.isNull(fieldName)) {
    return jsonObject.getString(fieldName);
       }
    } catch (JSONException e) {
       AdVideoApplication.logger.e(LOG_TAG + "::getString",
       "Error getting field from JSON: " + e.getMessage());
    }
    */

    try {
        Iterator<String> iter = jsonObject.keys();
        while (iter.hasNext()) {
            String key1 = iter.next();
            if (key1.equalsIgnoreCase(fieldName)) {
                if (!jsonObject.isNull(key1))
                    return jsonObject.getString(key1);
                else
                    return null;
            }
        }
    } catch (JSONException e) {
        AdVideoApplication.logger.e(LOG_TAG + "::getString",
                "Error getting field from JSON: " + e.getMessage());
    }
    return null;
}

From source file:com.hotstar.player.adplayer.feeds.ReferencePlayerFeedItemAdapter.java

/**
 * Sets the small and large thumb URLs/*www .  ja v  a2s  .c  o m*/
 */
protected void parseThumbnailUrls() {
    if (getString(entryObject, NAME_THUMBNAILS) == null) {
        return;
    }

    JSONObject smallThumbnailObject = null;
    JSONObject largeThumbnailObject = null;

    try {
        JSONArray content = entryObject.getJSONArray(NAME_THUMBNAILS);
        for (int i = 0; i < content.length(); i++) {
            JSONObject contentObject = content.getJSONObject(i);
            String url = getString(contentObject, NAME_THUMBNAILS_URL);
            if (url != null && !url.isEmpty()) {
                if (smallThumbnailObject == null) {
                    smallThumbnailObject = largeThumbnailObject = contentObject;
                } else {
                    if (contentObject.getInt(NAME_THUMBNAILS_WIDTH) < smallThumbnailObject
                            .getInt(NAME_THUMBNAILS_WIDTH)) {
                        smallThumbnailObject = contentObject;
                    } else if (contentObject.getInt(NAME_THUMBNAILS_WIDTH) > largeThumbnailObject
                            .getInt(NAME_THUMBNAILS_WIDTH)) {
                        largeThumbnailObject = contentObject;
                    }
                }
            }
        }

        if (smallThumbnailObject != null) {
            this.smallThumbnailURL = smallThumbnailObject.getString(NAME_THUMBNAILS_URL);
        }

        if (largeThumbnailObject != null) {
            this.largeThumbnailURL = largeThumbnailObject.getString(NAME_THUMBNAILS_URL);
        }
    } catch (JSONException e) {
        AdVideoApplication.logger.e(LOG_TAG + "::parseThumbnailUrls",
                "Error parsing thumbnail list: " + e.getMessage());
    }
}