Java Code Examples for org.json.JSONException#getMessage()

The following examples show how to use org.json.JSONException#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WeatherUtils.java    From Travel-Mate with MIT License 6 votes vote down vote up
/**
 * parses the icons.json file which contains the weather condition codes and descriptions
 * required to fetch the right weather icon to display
 *
 *
 * @param context context to access application resources
 * @param code weather condition code
 * @return weather condition description
 */
private static String getSuffix(Context context, int code) throws JSONException, IOException {
    String json;
    String cond = "";
    try {
        InputStream is = context.getAssets().open("icons.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");

        JSONObject jsonObject = new JSONObject(json);
        if (jsonObject.has(String.valueOf(code))) {
            JSONObject object = jsonObject.getJSONObject(String.valueOf(code));
            cond = object.getString("icon");
        }

    } catch (JSONException ex) {
        throw new JSONException(ex.getMessage());
    } catch (IOException e) {
        throw new IOException(e.getMessage());
    }
    return cond;
}
 
Example 2
Source File: Apk.java    From TvAppRepo with Apache License 2.0 6 votes vote down vote up
public Builder(String serial) {
    Log.d(TAG, serial);
    try {
        JSONObject jsonObject = new JSONObject(serial);
        Log.d(TAG, jsonObject.toString());
        mApk = new Apk();
        mApk.name = jsonObject.getString(KEY_NAME);
        mApk.banner = jsonObject.getString(KEY_BANNER);
        mApk.icon = jsonObject.getString(KEY_ICON);
        String downloadUrl = jsonObject.getString(KEY_DOWNLOAD_URL);
        Log.d(TAG, downloadUrl);
        mApk.downloadUrl = new FirebaseMap(downloadUrl).getMap();
        mApk.packageName = jsonObject.getString(KEY_PACKAGE_NAME);
        mApk.submitted = jsonObject.getLong(KEY_SUBMISSION_DATE);
        mApk.versionCode = jsonObject.getInt(KEY_VERSION_CODE);
        mApk.versionName = jsonObject.getString(KEY_VERSION_NAME);
        mApk.key = jsonObject.getString(KEY);
        mApk.downloads = jsonObject.getLong(KEY_DOWNLOADS);
        mApk.views = jsonObject.getLong(KEY_VIEWS);
    } catch (JSONException e) {
        throw new RuntimeException(e.getMessage());
    }
}
 
Example 3
Source File: SmsEventData.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
@NonNull
@Override
public String serialize(@NonNull PluginDataFormat format) {
    String res;
    switch (format) {
        default:
            try {
                JSONObject jsonObject = new JSONObject();
                if (!Utils.isBlank(innerData.sender))
                    jsonObject.put(K_SENDER, innerData.sender);
                if (!Utils.isBlank(innerData.content))
                    jsonObject.put(K_CONTENT, innerData.content);
                res = jsonObject.toString();
            } catch (JSONException e) {
                Logger.e(e, "Error serializing %s", getClass().getSimpleName());
                throw new IllegalStateException(e.getMessage());
            }
    }
    return res;
}
 
Example 4
Source File: GfJsonArray.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param index
 * @param value
 * @return this GfJsonArray
 * @throws GfJsonException
 *           If the index is negative or if the the value is an invalid
 *           number.
 */
public GfJsonArray put(int index, Map<?, ?> value)
    throws GfJsonException {
  try {
    this.jsonArray.put(index, value);
  } catch (JSONException e) {
    throw new GfJsonException(e.getMessage());
  }
  return this;
}
 
Example 5
Source File: GfJsonArray.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param array
 * @throws GfJsonException If not an array.
 */
public GfJsonArray(Object array) throws GfJsonException {
  try {
    if (array instanceof JSONArray) {
      this.jsonArray = (JSONArray) array;
    } else {
      this.jsonArray = new JSONArray(array);
    }
  } catch (JSONException e) {
    throw new GfJsonException(e.getMessage());
  }
}
 
Example 6
Source File: GfJsonArray.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param indentFactor
 * @return this GfJsonArray
 * @throws GfJsonException
 *           If the object contains an invalid number.
 */
public String toIndentedString(int indentFactor) throws GfJsonException {
  try {
    return jsonArray.toString(indentFactor);
  } catch (JSONException e) {
    throw new GfJsonException(e.getMessage());
  }
}
 
Example 7
Source File: User.java    From fanfouapp-opensource with Apache License 2.0 5 votes vote down vote up
public static User parse(final JSONObject o) throws ApiException {
    if (null == o) {
        return null;
    }
    try {
        final User user = new User();
        user.id = o.getString(BasicColumns.ID);
        user.screenName = o.getString(UserInfo.SCREEN_NAME);
        user.location = o.getString(UserInfo.LOCATION);
        user.gender = o.getString(UserInfo.GENDER);
        user.birthday = o.getString(UserInfo.BIRTHDAY);
        user.description = o.getString(UserInfo.DESCRIPTION);
        user.profileImageUrl = o.getString(UserInfo.PROFILE_IMAGE_URL);
        user.url = o.getString(UserInfo.URL);
        user.protect = o.getBoolean(UserInfo.PROTECTED);
        user.followersCount = o.getInt(UserInfo.FOLLOWERS_COUNT);
        user.friendsCount = o.getInt(UserInfo.FRIENDS_COUNT);
        user.favouritesCount = o.getInt(UserInfo.FAVORITES_COUNT);
        user.statusesCount = o.getInt(UserInfo.STATUSES_COUNT);
        user.following = o.getBoolean(UserInfo.FOLLOWING);
        user.createdAt = ApiParser.date(o
                .getString(BasicColumns.CREATED_AT));

        user.type = Constants.TYPE_NONE;
        user.ownerId = AppContext.getUserId();
        return user;
    } catch (final JSONException e) {
        throw new ApiException(ResponseCode.ERROR_JSON_EXCEPTION,
                e.getMessage(), e);
    }
}
 
Example 8
Source File: JsonableTranslator.java    From mdw with Apache License 2.0 5 votes vote down vote up
@Override
public String toString(Object obj, String variableType) throws TranslationException {
    try {
        return toJson(obj).toString(2);
    }
    catch (JSONException ex) {
        throw new TranslationException(ex.getMessage(), ex);
    }
}
 
Example 9
Source File: Index.java    From algoliasearch-client-java with MIT License 5 votes vote down vote up
/**
 * Partially Override the content of several objects
 *
 * @param objects        the array of objects to update (each object must contains an objectID attribute)
 * @param requestOptions Options to pass to this request
 */
public JSONObject partialUpdateObjects(List<JSONObject> objects, RequestOptions requestOptions) throws AlgoliaException {
  try {
    JSONArray array = new JSONArray();
    for (JSONObject obj : objects) {
      array.put(partialUpdateObject(obj));
    }
    return batch(array, requestOptions);
  } catch (JSONException e) {
    throw new AlgoliaException(e.getMessage());
  }
}
 
Example 10
Source File: Index.java    From algoliasearch-client-java with MIT License 5 votes vote down vote up
/**
 * Partially Override the content of several objects
 *
 * @param objects        the array of objects to update (each object must contains an objectID attribute)
 * @param requestOptions Options to pass to this request
 */
public JSONObject partialUpdateObjects(JSONArray objects, RequestOptions requestOptions) throws AlgoliaException {
  try {
    JSONArray array = new JSONArray();
    for (int n = 0; n < objects.length(); n++) {
      array.put(partialUpdateObject(objects.getJSONObject(n)));
    }
    return batch(array, requestOptions);
  } catch (JSONException e) {
    throw new AlgoliaException(e.getMessage());
  }
}
 
Example 11
Source File: GfJsonArray.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Get the object value associated with an index.
 * 
 * @param index
 * @return An object value.
 * @throws GfJsonException If there is no value for the index.
 */
public Object get(int index) throws GfJsonException {
  try {
    return this.jsonArray.get(index);
  } catch (JSONException e) {
    throw new GfJsonException(e.getMessage());
  }
}
 
Example 12
Source File: GfJsonArray.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param index
 * @param value
 * @return this GfJsonArray
 * @throws GfJsonException
 *           If the index is negative or if the the value is an invalid
 *           number.
 */
public GfJsonArray put(int index, Map<?, ?> value)
    throws GfJsonException {
  try {
    this.jsonArray.put(index, value);
  } catch (JSONException e) {
    throw new GfJsonException(e.getMessage());
  }
  return this;
}
 
Example 13
Source File: HttpEndpoint.java    From spring-integration-aws with MIT License 5 votes vote down vote up
private void handleSubscriptionConfirmation(HttpServletRequest request,
		HttpServletResponse response) throws IOException {
	try {
		String source = readBody(request);
		log.debug("Subscription confirmation:\n" + source);
		JSONObject confirmation = new JSONObject(source);

		if (validSignature(source, confirmation)) {
			URL subscribeUrl = new URL(
					confirmation.getString("SubscribeURL"));
			HttpURLConnection http = (HttpURLConnection) subscribeUrl
					.openConnection();
			http.setDoOutput(false);
			http.setDoInput(true);
			StringBuilder buffer = new StringBuilder();
			byte[] buf = new byte[4096];
			while ((http.getInputStream().read(buf)) >= 0) {
				buffer.append(new String(buf));
			}
			log.debug("SubscribeURL response:\n" + buffer.toString());
		}
		response.setStatus(HttpServletResponse.SC_OK);

	} catch (JSONException e) {
		throw new IOException(e.getMessage(), e);
	}
}
 
Example 14
Source File: GfJsonObject.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param indentFactor
 * @return this GfJsonObject
 * @throws GfJsonException
 *           If the object contains an invalid number.
 */
public String toIndentedString(int indentFactor) throws GfJsonException {
  try {
    return jsonObject.toString(indentFactor);
  } catch (JSONException e) {
    throw new GfJsonException(e.getMessage());
  }
}
 
Example 15
Source File: ConditionSerializer.java    From Easer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public String serialize(ConditionStructure data) throws UnableToSerializeException {
    try {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(C.NAME, data.getName());
        jsonObject.put(C.VERSION, C.VERSION_CURRENT);
        jsonObject.put(C.CONDITION, serialize_condition(data.getData()));
        return jsonObject.toString();
    } catch (JSONException e) {
        throw new UnableToSerializeException(e.getMessage());
    }
}
 
Example 16
Source File: Dereg.java    From UAF with Apache License 2.0 5 votes vote down vote up
public String clientSendDeregResponse (String uafMessage) {
	StringBuffer res = new StringBuffer();
	String decoded = null;
	try {
		JSONObject json = new JSONObject(uafMessage);
		decoded = json.getString("uafProtocolMessage").replace("\\", "");
		post(decoded);
		return decoded;
	} catch (JSONException e) {
		e.printStackTrace();
		return e.getMessage();
	}
}
 
Example 17
Source File: APIClient.java    From algoliasearch-client-java with MIT License 5 votes vote down vote up
private JSONObject postBatch(Object actions, RequestOptions requestOptions) throws AlgoliaException {
  try {
    JSONObject content = new JSONObject();
    content.put("requests", actions);
    return postRequest("/1/indexes/*/batch", content.toString(), true, false, requestOptions);
  } catch (JSONException e) {
    throw new AlgoliaException(e.getMessage());
  }
}
 
Example 18
Source File: LongListTranslator.java    From mdw with Apache License 2.0 5 votes vote down vote up
@Override
public Object toObject(String str, String type) throws TranslationException {
    try {
        List<Long> longList = new ArrayList<>();
        JSONArray jsonArray = new JSONArray(str);
        for (int i = 0; i < jsonArray.length(); i++)
            longList.add(jsonArray.opt(i) == null ? null : jsonArray.getLong(i));
        return longList;
    }
    catch (JSONException ex) {
        throw new TranslationException(ex.getMessage(), ex);
    }
}
 
Example 19
Source File: JSONObjectWithLegibleException.java    From vespa with Apache License 2.0 4 votes vote down vote up
private String getErrorMessage(String key, Object value, JSONException e) {
    return "Trying to add invalid JSON object with key '" + key +
            "' and value '" + value +
            "' - " + e.getMessage();
}
 
Example 20
Source File: InternalProviderData.java    From androidtv-sample-inputs with Apache License 2.0 3 votes vote down vote up
/**
 * Creates a new object and attempts to populate by obtaining the String representation of the
 * provided byte array
 *
 * @param bytes Byte array corresponding to a correctly formatted String representation of
 *     InternalProviderData
 * @throws ParseException If data is not formatted correctly
 */
public InternalProviderData(@NonNull byte[] bytes) throws ParseException {
    try {
        mJsonObject = new JSONObject(new String(bytes));
    } catch (JSONException e) {
        throw new ParseException(e.getMessage());
    }
}