List of usage examples for org.json JSONException printStackTrace
public void printStackTrace()
From source file:com.hichinaschool.flashcards.anki.servicelayer.NoteService.java
/** * Creates an empty Note from given Model * //w w w. j ava 2 s . c o m * @param model the model in JSOBObject format * @return a new note instance */ public static MultimediaEditableNote createEmptyNote(JSONObject model) { try { JSONArray fieldsArray = model.getJSONArray("flds"); int numOfFields = fieldsArray.length(); if (numOfFields > 0) { MultimediaEditableNote note = new MultimediaEditableNote(); note.setNumFields(numOfFields); for (int i = 0; i < numOfFields; i++) { JSONObject fieldObject = fieldsArray.getJSONObject(i); TextField uiTextField = new TextField(); uiTextField.setName(fieldObject.getString("name")); uiTextField.setText(fieldObject.getString("name")); note.setField(i, uiTextField); } note.setModelId(model.getLong("id")); return note; } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:com.tassadar.multirommgr.installfragment.InstallCard.java
private int getDefaultKernel() { int res = 0;/*from www . j a v a 2 s . c o m*/ Iterator<Map.Entry<String, Manifest.InstallationFile>> itr = m_manifest.getKernels().entrySet().iterator(); for (int i = 0; itr.hasNext(); ++i) { JSONObject extra = itr.next().getValue().extra; if (extra == null) continue; try { if (extra.has("display") && Build.DISPLAY.indexOf(extra.getString("display")) == -1) continue; if (extra.has("releases")) { JSONArray r = extra.getJSONArray("releases"); boolean found = false; for (int x = 0; x < r.length(); ++x) { if (r.getString(x).equals(Build.VERSION.RELEASE)) { found = true; break; } } if (!found) continue; } res = i; } catch (JSONException e) { e.printStackTrace(); } } return res; }
From source file:edu.cwru.apo.API.java
public boolean callMethod(Methods method, AsyncRestRequestListener<Methods, JSONObject> callback, String... params) {/* w w w . jav a2 s . co m*/ boolean result = false; switch (method) { case checkAppVersion: ApiCall checkVersionCall = new ApiCall(context, callback, method, "Checking for updates", "Please Wait"); RestClient checkVersionClient = new RestClient(secureUrl, httpClient, RequestMethod.POST); checkVersionClient.AddParam("method", "appVersion"); //execute the call checkVersionCall.execute(checkVersionClient); result = true; break; case login: if (params.length != 2) break; if (params[0] == null || params[1] == null) break; // set up a login request NOTE: Must be HTTPS!! ApiCall loginCall = new ApiCall(context, callback, method, "Logging In", "Please Wait"); RestClient loginClient = new RestClient(secureUrl, httpClient, RequestMethod.POST); loginClient.AddParam("method", "login"); loginClient.AddParam("user", params[0]); loginClient.AddParam("pass", Auth.md5(params[1]).toString()); loginClient.AddParam("installID", URLEncoder.encode(Installation.id(context.getApplicationContext()))); loginClient.AddParam("secretKey", Auth.Hmac.getSecretKey().toString()); //execute the call loginCall.execute(loginClient); result = true; break; case checkCredentials: // set up a checkCredentials request ApiCall checkCredentialsCall = new ApiCall(context, callback, method); RestClient checkCredentialsClient = new RestClient(secureUrl, httpClient, RequestMethod.POST); // if both exist add parameters to call and execute String installID = Installation.id(context.getApplicationContext()); String timestamp = Long.toString(Auth.getTimestamp()); String data = "checkCredentials" + timestamp + installID; checkCredentialsClient.AddParam("method", "checkCredentials"); checkCredentialsClient.AddParam("installID", installID); checkCredentialsClient.AddParam("timestamp", timestamp); try { checkCredentialsClient.AddParam("HMAC", Auth.Hmac.generate(data).toString()); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } //execute the call checkCredentialsCall.execute(checkCredentialsClient); result = true; break; case logout: // set up a logout request break; case resetPassword: // set up a resetPassword request break; case getContract: // set up a getContract request ApiCall getContractCall = new ApiCall(context, callback, method, "Loading", "Please Wait"); RestClient getContractClient = new RestClient(secureUrl, httpClient, RequestMethod.POST); // if both exist add parameters to call and execute String contractinstallID = Installation.id(context.getApplicationContext()); String contracttimestamp = Long.toString(Auth.getTimestamp()); String contractdata = "getContract" + contracttimestamp + contractinstallID; getContractClient.AddParam("method", "getContract"); getContractClient.AddParam("installID", contractinstallID); getContractClient.AddParam("timestamp", contracttimestamp); try { getContractClient.AddParam("HMAC", Auth.Hmac.generate(contractdata).toString()); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } //execute the call getContractCall.execute(getContractClient); result = true; break; case phone: // set up a phone request ApiCall phoneCall = new ApiCall(context, callback, method, "Updating User Directory", "Please Wait"); RestClient phoneClient = new RestClient(secureUrl, httpClient, RequestMethod.POST); // if both exist add parameters to call and execute String phoneInstallID = Installation.id(context.getApplicationContext()); String phoneTimestamp = Long.toString(Auth.getTimestamp()); JSONObject phoneUserData = new JSONObject(); try { if (params != null) phoneUserData.put("updateTime", Long.parseLong(params[0])); else phoneUserData.put("updateTime", getUpdateTime(context.getSharedPreferences(APO.PREF_FILE_NAME, MODE_PRIVATE))); String phoneUD = URLEncoder.encode(phoneUserData.toString()); String phoneData = "phone" + phoneTimestamp + phoneInstallID + phoneUserData.toString(); phoneClient.AddParam("method", "phone"); phoneClient.AddParam("installID", phoneInstallID); phoneClient.AddParam("timestamp", phoneTimestamp); phoneClient.AddParam("userData", phoneUD); phoneClient.AddParam("HMAC", Auth.Hmac.generate(phoneData).toString()); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } //execute the call phoneCall.execute(phoneClient); result = true; break; } return result; }
From source file:edu.cwru.apo.API.java
public boolean callMethod(Methods method, AsyncRestRequestListener<Methods, JSONObject> callback, JSONObject json, String... params) { boolean result = false; switch (method) { case serviceReport: // set up a phone request ApiCall reportCall = new ApiCall(context, callback, method, "Submitting Service Report", "Please Wait"); RestClient reportClient = new RestClient(secureUrl, httpClient, RequestMethod.POST); // if both exist add parameters to call and execute String reportInstallID = Installation.id(context.getApplicationContext()); String reportTimestamp = Long.toString(Auth.getTimestamp()); JSONObject reportUserData = new JSONObject(); try {//from ww w . j a v a 2 s .c om reportUserData.put("date", params[0]); reportUserData.put("projectName", params[1]); reportUserData.put("projectLocation", params[2]); reportUserData.put("inOut", params[3]); reportUserData.put("offCampus", params[4]); reportUserData.put("serviceType", params[5]); reportUserData.put("travelTime", params[6]); reportUserData.put("comments", params[7]); reportUserData.put("numBros", params[8]); reportUserData.put("brothers", json); } catch (JSONException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String reportUD = URLEncoder.encode(reportUserData.toString()); String reportData = "serviceReport" + reportTimestamp + reportInstallID + reportUserData.toString(); reportClient.AddParam("method", "serviceReport"); reportClient.AddParam("installID", reportInstallID); reportClient.AddParam("timestamp", reportTimestamp); reportClient.AddParam("userData", reportUD); try { reportClient.AddParam("HMAC", Auth.Hmac.generate(reportData).toString()); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } //execute the call reportCall.execute(reportClient); result = true; break; } return result; }
From source file:com.dongli.model.MyJSONObject.java
public String getUID() { try {// w ww .j a v a2s .c o m return (String) jsonObject.get("uid"); } catch (JSONException e) { e.printStackTrace(); return null; } }
From source file:com.dongli.model.MyJSONObject.java
public void setUID(String uid) { try {/* w ww . j a v a 2 s.co m*/ jsonObject.put("uid", uid); } catch (JSONException e) { e.printStackTrace(); } }
From source file:org.protorabbit.json.DefaultSerializer.java
public Object serialize(Object o) { // null is null if (o == null) { return JSONObject.NULL; }/* w w w . j av a 2s .co m*/ // collections if (Collection.class.isAssignableFrom(o.getClass())) { Iterator<?> it = ((Collection<?>) o).iterator(); JSONArray ja = new JSONArray(); while (it.hasNext()) { Object i = serialize(it.next()); ja.put(i); } return ja; } // maps if (Map.class.isAssignableFrom(o.getClass())) { JSONObject jo = new JSONObject(); Map<?, ?> m = ((Map<?, ?>) o); Iterator<?> ki = m.keySet().iterator(); while (ki.hasNext()) { Object key = ki.next(); Object value = serialize(m.get(key)); try { jo.put(key.toString(), value); } catch (JSONException e) { e.printStackTrace(); } } return jo; } // primitives if (o instanceof Double || o instanceof Number || o instanceof Integer || o instanceof String || o instanceof Enum<?> || o instanceof Boolean) { return o; } if (o instanceof Date) { return ((Date) o).getTime(); } // convert arrays to collections boolean b = o.getClass().isArray(); if (b) { try { Object[] objs = (Object[]) o; List<Object> l = Arrays.asList(objs); return serialize(l); } catch (ClassCastException e) { return JSONObject.NULL; } } // serialize using bean like methods return serializePOJO(o, true); }
From source file:org.protorabbit.json.DefaultSerializer.java
@SuppressWarnings("unchecked") void invokeMethod(Method[] methods, String key, String name, JSONObject jo, Object targetObject) { Object param = null;//from w ww . ja v a2s . co m Throwable ex = null; for (int i = 0; i < methods.length; i++) { Method m = methods[i]; if (m.getName().equals(name)) { Class<?>[] paramTypes = m.getParameterTypes(); if (paramTypes.length == 1 && jo.has(key)) { Class<?> tparam = paramTypes[0]; boolean allowNull = false; try { if (jo.isNull(key)) { // do nothing because param is already null : lets us not null on other types } else if (Long.class.isAssignableFrom(tparam) || tparam == long.class) { param = new Long(jo.getLong(key)); } else if (Double.class.isAssignableFrom(tparam) || tparam == double.class) { param = new Double(jo.getDouble(key)); } else if (Integer.class.isAssignableFrom(tparam) || tparam == int.class) { param = new Integer(jo.getInt(key)); } else if (String.class.isAssignableFrom(tparam)) { param = jo.getString(key); } else if (Enum.class.isAssignableFrom(tparam)) { param = Enum.valueOf((Class<? extends Enum>) tparam, jo.getString(key)); } else if (Boolean.class.isAssignableFrom(tparam)) { param = new Boolean(jo.getBoolean(key)); } else if (jo.isNull(key)) { param = null; allowNull = true; } else if (Collection.class.isAssignableFrom(tparam)) { if (m.getGenericParameterTypes().length > 0) { Type t = m.getGenericParameterTypes()[0]; if (t instanceof ParameterizedType) { ParameterizedType tv = (ParameterizedType) t; if (tv.getActualTypeArguments().length > 0 && tv.getActualTypeArguments()[0] == String.class) { List<String> ls = new ArrayList<String>(); JSONArray ja = jo.optJSONArray(key); if (ja != null) { for (int j = 0; j < ja.length(); j++) { ls.add(ja.getString(j)); } } param = ls; } else if (tv.getActualTypeArguments().length == 1) { ParameterizedType type = (ParameterizedType) tv.getActualTypeArguments()[0]; Class itemClass = (Class) type.getRawType(); if (itemClass == Map.class && type.getActualTypeArguments().length == 2 && type.getActualTypeArguments()[0] == String.class && type.getActualTypeArguments()[1] == Object.class) { List<Map<String, Object>> ls = new ArrayList<Map<String, Object>>(); JSONArray ja = jo.optJSONArray(key); if (ja != null) { for (int j = 0; j < ja.length(); j++) { Map<String, Object> map = new HashMap<String, Object>(); JSONObject mo = ja.getJSONObject(j); Iterator<String> keys = mo.keys(); while (keys.hasNext()) { String okey = keys.next(); Object ovalue = null; // make sure we don't get JSONObject$Null if (!mo.isNull(okey)) { ovalue = mo.get(okey); } map.put(okey, ovalue); } ls.add(map); } } param = ls; } else { getLogger().warning( "Don't know how to handle Collection of type : " + itemClass); } } else { getLogger().warning("Don't know how to handle Collection of type : " + tv.getActualTypeArguments()[0]); } } } } else { getLogger().warning( "Unable to serialize " + key + " : Don't know how to handle " + tparam); } } catch (JSONException e) { e.printStackTrace(); } if (param != null || allowNull) { try { if (m != null) { Object[] args = { param }; m.invoke(targetObject, args); ex = null; break; } } catch (SecurityException e) { ex = e; } catch (IllegalArgumentException e) { ex = e; } catch (IllegalAccessException e) { ex = e; } catch (InvocationTargetException e) { ex = e; } } } } } if (ex != null) { if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } else { throw new RuntimeException(ex); } } }
From source file:org.protorabbit.json.DefaultSerializer.java
public void deSerialize(String jsonText, Object targetObject) { try {/* w ww. java 2 s .c o m*/ JSONObject jo = new JSONObject(jsonText); deSerialize(jo, targetObject); } catch (JSONException e) { e.printStackTrace(); } }
From source file:gr.cti.android.experimentation.controller.ui.RestRankingController.java
@ResponseBody @Deprecated/*from ww w.j ava2s . c o m*/ @RequestMapping(value = "/statistics", method = RequestMethod.GET) public String getRankings(@RequestParam(required = false, defaultValue = "0") final int deviceId) { final long resultsTotal = resultRepository.countByDeviceId(deviceId); final long resultsToday = resultRepository.countByDeviceIdAndTimestampAfter(deviceId, new DateTime().withMillisOfDay(0).getMillis()); final Set<Result> experimentsTotal = new HashSet<>(); final Set<Integer> experimentIdsTotal = new HashSet<>(); experimentsTotal.addAll(resultRepository.findDistinctExperimentIdByDeviceId(deviceId)); final Set<Result> experimentsToday = new HashSet<>(); final Set<Integer> experimentsIdsToday = new HashSet<>(); experimentsToday.addAll(resultRepository.findDistinctExperimentIdByDeviceIdAndTimestampAfter(deviceId, new DateTime().withMillisOfDay(0).getMillis())); experimentIdsTotal .addAll(experimentsTotal.stream().map(Result::getExperimentId).collect(Collectors.toList())); experimentsIdsToday .addAll(experimentsToday.stream().map(Result::getExperimentId).collect(Collectors.toList())); final JSONObject obj = new JSONObject(); try { obj.put("resultsTotal", resultsTotal); obj.put("resultsToday", resultsToday); obj.put("experimentsTotal", experimentIdsTotal.size()); obj.put("experimentsToday", experimentsIdsToday.size()); } catch (JSONException e) { e.printStackTrace(); } return obj.toString(); }