List of usage examples for org.json JSONException getMessage
public String getMessage()
From source file:com.strato.hidrive.api.bll.free.GetCaptchaGateway.java
@Override protected Request prepareRequest() { List<BaseParam<?>> params = new ArrayList<BaseParam<?>>(); JSONObject json = new JSONObject(); try {//from w w w. j a va 2 s.c om json.put("width", "117"); json.put("height", "24"); json.put("bgcolor", "#FFFFFF"); } catch (JSONException e) { if (e != null && e.getMessage() != null) { Log.e(getClass().getSimpleName(), e.getMessage()); } } params.add(new Param("postdata", json.toString())); return new PostRequest("getcaptcha", params); }
From source file:ch.icclab.cyclops.resource.impl.RateResource.java
/** * Saves static rate into InfluxDB/*w w w . j a va2 s. c o m*/ * * Pseudo Code * 1. Populate object array with "rate" data from JSONObject * 2. Create the POJO object from object array * 3. Map POJO object to JSON data * 4. Save JSON data in InfluxDB client * * @param jsonObj Data obj containing the static rate of a resource * @param ratingPolicy String containing the rating policy * @return boolean */ private boolean saveStaticRate(JSONObject jsonObj, String ratingPolicy) { boolean result = false; TSDBData rateData = new TSDBData(); ArrayList<String> strArr = new ArrayList<String>(); ArrayList<Object> objArrNode; ArrayList<ArrayList<Object>> objArr = new ArrayList<ArrayList<Object>>(); ObjectMapper mapper = new ObjectMapper(); Load load = new Load(); HashMap staticRate = new HashMap(); String jsonData = null; JSONObject rateJsonObj; Iterator iterator; String key; if (dbClient == null) { dbClient = new InfluxDBClient(); } //TODO: simplify this code, separate method of object creation from this method // Reset the hashmap containing the static rate load.setStaticRate(staticRate); // Set the flag to "Static" Flag.setMeteringType("static"); // Create the Array to hold the names of the columns strArr.add("resource"); strArr.add("rate"); strArr.add("rate_policy"); try { rateJsonObj = (JSONObject) jsonObj.get("rate"); iterator = rateJsonObj.keys(); while (iterator.hasNext()) { key = (String) iterator.next(); objArrNode = new ArrayList<Object>(); objArrNode.add(key); objArrNode.add(rateJsonObj.get(key)); objArrNode.add(ratingPolicy); objArr.add(objArrNode); // Load the rate hashmap with the updated rate staticRate.put(key, rateJsonObj.get(key)); } } catch (JSONException e) { logger.error("Error while saving the Static Rate: " + e.getMessage()); e.printStackTrace(); } // Update the static hashmap containing the static rates load.setStaticRate(staticRate); // Set the data object to save into the DB rateData.setName("rate"); rateData.setColumns(strArr); rateData.setPoints(objArr); try { jsonData = mapper.writeValueAsString(rateData); } catch (JsonProcessingException e) { logger.error("Error while saving the Static Rate: " + e.getMessage()); e.printStackTrace(); } result = dbClient.saveData(jsonData); return result; }
From source file:de.itomig.itoplib.GetItopJSON.java
public static <T> ArrayList<T> getArrayFromJson(String json, Type T) { ArrayList<T> list = new ArrayList<T>(); String code = "100"; // json error code, "0" => everything is o.k. Log.d(TAG, "getArrayFromJson - Type=" + T.toString()); JSONObject jsonObject = null;//from w w w .ja v a 2 s .c o m try { jsonObject = new JSONObject(json); code = jsonObject.getString("code"); Log.d(TAG, "code=" + code); Log.d(TAG, "message=" + jsonObject.getString("message")); } catch (JSONException e) { Log.e(TAG, "error in getArrayFromJSON " + e.getMessage()); } if ((jsonObject != null) && (code.trim().equals("0"))) { try { JSONObject objects = jsonObject.getJSONObject("objects"); Iterator<?> keys = objects.keys(); while (keys.hasNext()) { String key = (String) keys.next(); Log.d(TAG, "key=" + key); if (objects.get(key) instanceof JSONObject) { // Log.d(TAG,"obj="+objects.get(key).toString()); JSONObject o = (JSONObject) objects.get(key); JSONObject fields = o.getJSONObject("fields"); Log.d(TAG, "fields=" + fields.toString()); Gson gson = new Gson(); String k[] = key.split(":"); int id = Integer.parseInt(k[2]); T jf = gson.fromJson(fields.toString(), T); if (jf instanceof CMDBObject) { ((CMDBObject) jf).id = id; } list.add(jf); } } Log.d(TAG, "code=" + jsonObject.getString("code")); Log.d(TAG, "message=" + jsonObject.getString("message")); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // endif (jsonObject != null) return list; }
From source file:de.itomig.itoplib.GetItopJSON.java
public static String getMessage(String json) { String message = ""; JSONObject jsonObject = null;/*w w w. j av a2s. c om*/ try { jsonObject = new JSONObject(json); int code = jsonObject.getInt("code"); Log.d(TAG, "code=" + jsonObject.getString("code")); if (code != 0) { // if code!=0 there is an error message = jsonObject.getString("message"); Log.d(TAG, "message=" + message); } } catch (JSONException e) { Log.e(TAG, "error in getArrayFromJSON " + e.getMessage()); return e.getMessage(); } return message; }
From source file:com.manish.googleprofiledemo.AbstractGetNameTask.java
@Override protected Void doInBackground(Void... params) { try {// w ww. j a va 2 s .c om fetchNameFromProfileServer(); } catch (IOException ex) { onError("Following Error occured, please try again. " + ex.getMessage(), ex); } catch (JSONException e) { onError("Bad response: " + e.getMessage(), e); } return null; }
From source file:face4j.response.TrainResponseImpl.java
public TrainResponseImpl(final String json) throws FaceClientException { super(json);/*w ww. j a v a2 s . co m*/ try { no_training_set = toUserStatusList(response.optJSONArray("no_training_set")); in_progress = toUserStatusList(response.optJSONArray("in_progress")); unchanged = toUserStatusList(response.optJSONArray("unchanged")); updated = toUserStatusList(response.optJSONArray("updated")); created = toUserStatusList(response.optJSONArray("created")); } catch (JSONException jex) { logger.error("Error getting user statuses: " + jex.getMessage(), jex); throw new FaceClientException(jex); } }
From source file:com.tcs.base64.Base64ImagePlugin.java
@Override public boolean execute(String action, JSONArray data, CallbackContext callbackContext) { boolean result = false; Log.v(TAG, "execute: action=" + action); // Context context = getContext(); if (!action.equals("saveImage")) { callbackContext.error("Invalid action : " + action); result = false;/*from ww w .j a v a 2 s . c om*/ } try { Log.v(TAG, data.getString(0)); Log.v(TAG, data.getJSONObject(1).toString()); String b64String = data.getString(0); if (b64String.startsWith("data:image")) { b64String = b64String.substring(22); } else { b64String = data.getString(0); } JSONObject params = data.getJSONObject(1); //Optional parameter String filename = params.has("filename") ? params.getString("filename") + ".png" : "b64Image_" + System.currentTimeMillis() + ".png"; String storagetype = params.has("externalStorage") ? Environment.getExternalStorageDirectory() + "" : getApplicationContext().getFilesDir().getAbsolutePath(); callbackContext.error("external ==" + Environment.getExternalStorageDirectory()); String folder = params.has("folder") ? params.getString("folder") : storagetype + "/Pictures"; Boolean overwrite = params.has("overwrite") ? params.getBoolean("overwrite") : false; result = this.saveImage(b64String, filename, folder, overwrite, callbackContext); } catch (JSONException e) { Log.v(TAG, e.getMessage()); callbackContext.error("Exception :" + e.getMessage()); result = false; } return result; }
From source file:com.suarez.cordova.mapsforge.MapsforgePlugin.java
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if ("global-status".equals(action)) { this.status(); callbackContext.success();/*from w w w. jav a 2s. c o m*/ return true; } else if (action.contains("native-")) { if ("native-set-center".equals(action)) { try { MapsforgeNative.INSTANCE.setCenter(args.getDouble(0), args.getDouble(1)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show".equals(action)) { try { MapsforgeNative.INSTANCE.show(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-hide".equals(action)) { MapsforgeNative.INSTANCE.hide(); return true; } else if ("native-marker".equals(action)) { try { Activity context = this.cordova.getActivity(); int markerId = context.getResources().getIdentifier(args.getString(0), "drawable", context.getPackageName()); if (markerId == 0) { Log.i(MapsforgePlugin.TAG, "Marker not found...using default marker: marker_green"); markerId = context.getResources().getIdentifier("marker_green", "drawable", context.getPackageName()); } int markerKey = MapsforgeNative.INSTANCE.addMarker(markerId, args.getDouble(1), args.getDouble(2)); callbackContext.success(markerKey); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-polyline".equals(action)) { try { JSONArray points = args.getJSONArray(2); if (points.length() % 2 != 0) throw new JSONException("Invalid array of coordinates. Length should be multiple of 2"); int polylineKey = MapsforgeNative.INSTANCE.addPolyline(args.getInt(0), args.getInt(1), points); callbackContext.success(polylineKey); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-delete-layer".equals(action)) { try { MapsforgeNative.INSTANCE.deleteLayer(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-initialize".equals(action)) { try { MapsforgeNative.createInstance(this.cordova.getActivity(), args.getString(0), args.getInt(1), args.getInt(2)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-max-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setMaxZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-set-min-zoom".equals(action)) { try { MapsforgeNative.INSTANCE.setMinZoom(Byte.parseByte(args.getString(0))); callbackContext.success(); } catch (NumberFormatException nfe) { callbackContext.error("Incorrect argument format. Should be: (byte zoom)"); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show-controls".equals(action)) { try { MapsforgeNative.INSTANCE.setBuiltInZoomControls(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-clickable".equals(action)) { try { MapsforgeNative.INSTANCE.setClickable(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-show-scale".equals(action)) { try { MapsforgeNative.INSTANCE.showScaleBar(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-destroy-cache".equals(action)) { try { MapsforgeNative.INSTANCE.destroyCache(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } return true; } else if ("native-map-path".equals(action)) { try { MapsforgeNative.INSTANCE.setMapFilePath(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException iae) { callbackContext.error(iae.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-cache-name".equals(action)) { try { MapsforgeNative.INSTANCE.setCacheName(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-theme-path".equals(action)) { try { MapsforgeNative.INSTANCE.setRenderThemePath(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-stop".equals(action)) { try { MapsforgeNative.INSTANCE.onStop(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-start".equals(action)) { try { MapsforgeNative.INSTANCE.onStart(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-destroy".equals(action)) { try { MapsforgeNative.INSTANCE.onDestroy(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-online".equals(action)) { try { MapsforgeNative.INSTANCE.setOnline(args.getString(0), args.getString(1), args.getString(2), args.getString(3), args.getInt(4)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("native-offline".equals(action)) { try { MapsforgeNative.INSTANCE.setOffline(args.getString(0), args.getString(1)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } } else if (action.contains("cache-")) { if ("cache-get-tile".equals(action)) { try { final long x = args.getLong(0); final long y = args.getLong(1); final byte z = Byte.parseByte(args.getString(2)); final CallbackContext callbacks = callbackContext; cordova.getThreadPool().execute(new Runnable() { public void run() { try { String path = MapsforgeCache.INSTANCE.getTilePath(x, y, z); callbacks.success(path); } catch (IOException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-initialize".equals(action)) { try { MapsforgeCache.createInstance(cordova.getActivity(), args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (IllegalArgumentException e) { callbackContext.error(e.getMessage()); } catch (IOException e) { callbackContext.error(e.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-map-path".equals(action)) { try { final String mapFile = args.getString(0); final CallbackContext callbacks = callbackContext; cordova.getThreadPool().execute(new Runnable() { @Override public void run() { try { MapsforgeCache.INSTANCE.setMapFilePath(mapFile); callbacks.success(); } catch (IllegalArgumentException e) { callbacks.error(e.getMessage()); } catch (FileNotFoundException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-max-size".equals(action)) { try { MapsforgeCache.INSTANCE.setMaxCacheSize(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-max-age".equals(action)) { try { MapsforgeCache.INSTANCE.setMaxCacheAge(args.getLong(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-cleaning-trigger".equals(action)) { try { MapsforgeCache.INSTANCE.setCleanCacheTrigger(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-enabled".equals(action)) { try { MapsforgeCache.INSTANCE.setCacheEnabled(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-external".equals(action)) { try { MapsforgeCache.INSTANCE.setExternalCache(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-name".equals(action)) { try { MapsforgeCache.INSTANCE.setCacheName(args.getString(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-tile-size".equals(action)) { try { MapsforgeCache.INSTANCE.setTileSize(args.getInt(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-clean-destroy".equals(action)) { try { MapsforgeCache.INSTANCE.setCleanOnDestroy(args.getBoolean(0)); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-theme-path".equals(action)) { try { final CallbackContext callbacks = callbackContext; final String themePath = args.getString(0); cordova.getThreadPool().execute(new Runnable() { @Override public void run() { try { MapsforgeCache.INSTANCE.setRenderTheme(themePath); callbacks.success(); } catch (IllegalArgumentException e) { callbacks.error(e.getMessage()); } catch (FileNotFoundException e) { callbacks.error(e.getMessage()); } catch (Exception e) { callbacks.error(e.getMessage()); } } }); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-screen-ratio".equals(action)) { try { MapsforgeCache.INSTANCE.setScreenRatio(Float.parseFloat(args.getString(0))); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-overdraw".equals(action)) { try { MapsforgeCache.INSTANCE.setOverdrawFactor(Float.parseFloat(args.getString(0))); callbackContext.success(); } catch (JSONException je) { callbackContext.error(je.getMessage()); } catch (NumberFormatException nfe) { callbackContext.error(nfe.getMessage()); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } else if ("cache-destroy".equals(action)) { try { MapsforgeCache.INSTANCE.onDestroy(); callbackContext.success(); } catch (Exception e) { callbackContext.error(e.getMessage()); } return true; } } return false; // Returning false results in a "MethodNotFound" error. }
From source file:com.vinnypalumbo.vinnysmovies.FetchReviewsTask.java
/** * Take the String representing the review results in JSON Format and * pull out the data we need for each review * * Fortunately parsing is easy: constructor takes the JSON string and converts it * into an Object hierarchy for us.//from w w w . j a v a 2 s. c o m */ private List<Review> getReviewDataFromJson(String reviewJsonStr) throws JSONException { Log.d("vinny-debug", "FetchReviewsTask - getReviewDataFromJson"); // These are the names of the JSON objects that need to be extracted. final String TMDB_RESULTS = "results"; final String TMDB_AUTHOR = "author"; final String TMDB_CONTENT = "content"; try { JSONObject reviewJson = new JSONObject(reviewJsonStr); JSONArray reviewArray = reviewJson.getJSONArray(TMDB_RESULTS); // Insert the review into an ArrayList List<Review> reviews = new ArrayList<Review>(); for (int i = 0; i < reviewArray.length(); i++) { String reviewAuthor; String reviewContent; // Get the JSON object representing the review JSONObject review = reviewArray.getJSONObject(i); // the review author is in a String associated to the key "author" reviewAuthor = review.getString(TMDB_AUTHOR); // the review title is in a String associated to the key "content" reviewContent = review.getString(TMDB_CONTENT); reviews.add(new Review(reviewAuthor, reviewContent)); } Log.d(LOG_TAG, "Fetch Reviews Completed"); return reviews; } catch (JSONException e) { Log.e(LOG_TAG, e.getMessage(), e); e.printStackTrace(); } return null; }
From source file:com.galactogolf.specificobjectmodel.GalactoGolfWorld.java
public void LoadLevelSet(LevelSet getLevelSet) { _levelSet = getLevelSet;/*from w ww . j av a2s .c om*/ String s; try { s = JSONSerializer.toJSON(_levelSet).toString(); Log.d("Level JSON", s); LevelSet jsonSet = JSONSerializer.fromLevelSetJSON(new JSONObject(s)); _levelSet = jsonSet; } catch (JSONException e) { Log.e("Exception", e.getMessage()); } }