Stack Overflow archive
1 scoreaccepted

Inconsistent behavior between Java and Android

score
1
question views
150
license
CC BY-SA 3.0

Are you sure you are using the same jar in both projects?

Android's org.json.JSONObject.toString(String) is different:

java
/**
 * Returns the value mapped by {@code name} if it exists, coercing it if
 * necessary, or throws if no such mapping exists.
 *
 * @throws JSONException if no such mapping exists.
 */
public String getString(String name) throws JSONException {
    Object object = get(name);
    String result = JSON.toString(object);
    if (result == null) {
        throw JSON.typeMismatch(name, object, "String");
    }
    return result;
}

JSON.toString(Object):

java
static String toString(Object value) {
    if (value instanceof String) {
        return (String) value;
    } else if (value != null) {
        return String.valueOf(value);
    }
    return null;
}

In your case this would not throw an exception on Android.

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.