下面列出了org.json.JSONException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
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());
}
}
/**
* 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;
}
/**
*
* @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;
}
@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);
}
}
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());
}
}
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();
}
}
@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());
}
}
/**
*
* @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());
}
}
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);
}
}
/**
*
* @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;
}
/**
* 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());
}
}
/**
* 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());
}
}
/**
* 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());
}
}
@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);
}
}
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);
}
}
/**
*
* @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());
}
}
/**
*
* @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());
}
}
private String getErrorMessage(String key, Object value, JSONException e) {
return "Trying to add invalid JSON object with key '" + key +
"' and value '" + value +
"' - " + e.getMessage();
}
/**
* 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());
}
}