List of usage examples for org.json JSONException getMessage
public String getMessage()
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available organizations. If the * organization has an id that is not equal to null the organization will be updated in the database. * If the id is null then we treat it as a new organization. * /* w w w . j a v a 2 s.c o m*/ * @param contact the contact to extract the organizations from * @param uri the base URI for this contact. */ private void saveOrganizations(JSONObject contact, Uri newPersonUri) { ContentValues values = new ContentValues(); Uri orgUri = Uri.withAppendedPath(newPersonUri, Contacts.Organizations.CONTENT_DIRECTORY); String id = null; try { JSONArray orgs = contact.getJSONArray("organizations"); if (orgs != null && orgs.length() > 0) { JSONObject org; for (int i = 0; i < orgs.length(); i++) { org = orgs.getJSONObject(i); id = getJsonString(org, "id"); values.put(Contacts.Organizations.COMPANY, getJsonString(org, "name")); values.put(Contacts.Organizations.TITLE, getJsonString(org, "title")); if (id == null) { Uri contactUpdate = mApp.getContentResolver().insert(orgUri, values); } else { Uri tempUri = Uri.withAppendedPath(orgUri, id); mApp.getContentResolver().update(tempUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save organizations = " + e.getMessage()); } }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available addresses. If the * address has an id that is not equal to null the address will be updated in the database. * If the id is null then we treat it as a new address. * //from w w w. j a va 2s .co m * @param contact the contact to extract the addresses from * @param uri the base URI for this contact. */ private void saveAddresses(JSONObject contact, Uri uri) { ContentValues values = new ContentValues(); Uri newUri = Uri.withAppendedPath(uri, Contacts.People.ContactMethods.CONTENT_DIRECTORY); String id = null; try { JSONArray entries = contact.getJSONArray("addresses"); if (entries != null && entries.length() > 0) { JSONObject entry; values.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL); for (int i = 0; i < entries.length(); i++) { entry = entries.getJSONObject(i); id = getJsonString(entry, "id"); String address = getJsonString(entry, "formatted"); if (address != null) { values.put(Contacts.ContactMethods.DATA, address); } else { values.put(Contacts.ContactMethods.DATA, createAddressString(entry)); } if (id == null) { Uri contactUpdate = mApp.getContentResolver().insert(newUri, values); } else { Uri tempUri = Uri.withAppendedPath(newUri, id); mApp.getContentResolver().update(tempUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save address = " + e.getMessage()); } }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available entries (Emails/IM's). If the * entry has an id that is not equal to null the entry will be updated in the database. * If the id is null then we treat it as a new entry. * /*from ww w . ja v a2 s. co m*/ * @param contact the contact to extract the entries from * @param uri the base URI for this contact. */ private void saveEntries(JSONObject contact, Uri uri, String dataType, int contactKind) { ContentValues values = new ContentValues(); Uri newUri = Uri.withAppendedPath(uri, Contacts.People.ContactMethods.CONTENT_DIRECTORY); String id = null; try { JSONArray entries = contact.getJSONArray(dataType); if (entries != null && entries.length() > 0) { JSONObject entry; values.put(Contacts.ContactMethods.KIND, contactKind); for (int i = 0; i < entries.length(); i++) { entry = entries.getJSONObject(i); id = getJsonString(entry, "id"); values.put(Contacts.ContactMethods.DATA, getJsonString(entry, "value")); values.put(Contacts.ContactMethods.TYPE, getContactType(getJsonString(entry, "type"))); if (id == null) { Uri contactUpdate = mApp.getContentResolver().insert(newUri, values); } else { Uri tempUri = Uri.withAppendedPath(newUri, id); mApp.getContentResolver().update(tempUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save " + dataType + " = " + e.getMessage()); } }
From source file:com.phonegap.ContactAccessorSdk3_4.java
/** * Takes a JSON contact object and loops through the available phone numbers. If the phone * number has an id that is not equal to null the phone number will be updated in the database. * If the id is null then we treat it as a new phone number. * //from w w w . j a v a 2s.com * @param contact the contact to extract the phone numbers from * @param uri the base URI for this contact. */ private void savePhoneNumbers(JSONObject contact, Uri uri) { ContentValues values = new ContentValues(); Uri phonesUri = Uri.withAppendedPath(uri, Contacts.People.Phones.CONTENT_DIRECTORY); String id = null; try { JSONArray phones = contact.getJSONArray("phoneNumbers"); if (phones != null && phones.length() > 0) { JSONObject phone; for (int i = 0; i < phones.length(); i++) { phone = phones.getJSONObject(i); id = getJsonString(phone, "id"); values.put(Contacts.Phones.NUMBER, getJsonString(phone, "value")); values.put(Contacts.Phones.TYPE, getPhoneType(getJsonString(phone, "type"))); if (id == null) { Uri phoneUpdate = mApp.getContentResolver().insert(phonesUri, values); } else { Uri newUri = Uri.withAppendedPath(phonesUri, id); mApp.getContentResolver().update(newUri, values, null, null); } } } } catch (JSONException e) { Log.d(LOG_TAG, "Could not save phones = " + e.getMessage()); } }
From source file:de.eorganization.crawler.server.servlets.JSONImportServlet.java
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try {/* www . ja va 2 s .c o m*/ resp.setContentType("text/plain"); InputStream stream = req.getInputStream(); log.info("Got Upload " + req.getContentType() + " (" + req.getContentLength() + " bytes) from " + req.getRemoteAddr()); String amiId = req.getHeader("AMI-ID"); log.info("Got AMI ID:" + amiId); String repository = req.getHeader("REPO"); log.info("Got REPO:" + repository); if (amiId == null || repository == null || "".equals(amiId) || "".equals(repository)) { log.severe("AMI ID and REPO HTTP Header required."); return; } // Gson gson = new Gson(); // log.info("Parsed Gson " + gson.fromJson(new // InputStreamReader(stream), JsonObject.class)); String jsonString = IOUtils.toString(stream); log.info("Got JSON String " + jsonString.substring(0, jsonString.length() > 128 ? 128 : jsonString.length() - 1)); JSONObject json = new JSONObject(); try { json = new JSONObject(jsonString); log.fine("Parsed Json " + json); log.finer("Json has keys: " + json.names()); } catch (JSONException e) { log.severe("No JSON sent or wrong format."); return; } JSONObject software = json.optJSONObject("software"); if (software != null) { log.finer("Software JSON " + software); JSONArray softwareNames = software.names(); log.finer("Key Array JSON " + softwareNames); for (int i = 0; i < softwareNames.length(); i++) { log.fine("software " + softwareNames.getString(i) + " with version " + software.getJSONObject(softwareNames.getString(i)).getString("version")); Map<String, String> softAttributes = new HashMap<String, String>(); JSONArray softwareAttributes = software.getJSONObject(softwareNames.getString(i)).names(); for (int j = 0; j < softwareAttributes.length(); j++) softAttributes.put(softwareAttributes.getString(j), software.getJSONObject(softwareNames.getString(i)) .getString(softwareAttributes.getString(j))); Software soft = AmiManager.saveSoftware(amiId, repository, softwareNames.getString(i), software.getJSONObject(softwareNames.getString(i)).getString("version"), softAttributes); if (soft != null) { log.fine("Saved/restored software " + soft); // soft.getAttributes().putAll(softAttributes); // AmiManager.updateSoftware(soft); // log.fine("Saved object " + soft); } else log.severe("Not able to save software information for given Ami Id " + amiId + "!"); } List<String> names = new ArrayList<String>(); for (int i = 0; i < softwareNames.length(); i++) names.add(softwareNames.getString(i)); AmiManager.updateSoftwareNames(names); log.info("Saved " + softwareNames.length() + " software objects"); } JSONObject languages = json.optJSONObject("languages"); if (languages != null) { log.finer("Languages JSON " + languages); JSONArray languagesNames = languages.names(); log.finer("Key Array JSON " + languagesNames); for (int i = 0; i < languagesNames.length(); i++) { log.fine("languages " + languagesNames.getString(i) + " with version " + languages.getJSONObject(languagesNames.getString(i)).getString("version")); Map<String, String> langAttributes = new HashMap<String, String>(); JSONArray languageAttributes = languages.getJSONObject(languagesNames.getString(i)).names(); for (int j = 0; j < languageAttributes.length(); j++) langAttributes.put(languageAttributes.getString(j), languages.getJSONObject(languagesNames.getString(i)) .getString(languageAttributes.getString(j))); Language lang = AmiManager.saveLanguage(amiId, repository, languagesNames.getString(i), languages.getJSONObject(languagesNames.getString(i)).getString("version"), langAttributes); if (lang != null) { log.fine("Saved/restored programming language " + lang); lang.getAttributes().putAll(langAttributes); AmiManager.updateLanguage(lang); log.fine("Saved object " + lang); } else log.severe("Not able to save programming language information for given Ami Id " + amiId + "!"); } log.info("Saved " + languagesNames.length() + " programming language objects"); } resp.getWriter().println( "Saving software packages and programming languages for " + amiId + " (" + repository + ")."); } catch (JSONException e) { log.severe("Error while parsing JSON upload" + e.getMessage() + ", trace: " + e.getStackTrace()); log.throwing(JSONImportServlet.class.getName(), "doPost", e); e.printStackTrace(); } catch (Exception ex) { log.severe("Unexpected error" + ex.getMessage() + ", trace: " + ex.getStackTrace()); log.throwing(JSONImportServlet.class.getName(), "doPost", ex); ex.printStackTrace(); } // log.info("returning to referer " + req.getHeader("referer")); // resp.sendRedirect(req.getHeader("referer") != null && // !"".equals(req.getHeader("referer")) ? req.getHeader("referer") : // "localhost:8088"); }
From source file:org.upv.satrd.fic2.fe.connectors.citySDK.Fusion.java
public static FusionResult fusion(JSONObject poi1, JSONObject poi2, FusionRule fr, Logger log) { FusionResult fusionresult = null;//from www. j a v a 2 s .c o m String name_source = ""; if (log == null) log = Fusion.log; try { int namePos1 = CommonUtils.getPropertyPos(poi1, "name", log); String source1 = poi1.getJSONArray("label").getJSONObject(namePos1).getString("base"); int namePos2 = CommonUtils.getPropertyPos(poi2, "name", log); String source2 = poi2.getJSONArray("label").getJSONObject(namePos2).getString("base"); //Fusion label tag JSONArray object1 = poi1.getJSONArray("label"); JSONArray object2 = poi2.getJSONArray("label"); JSONArray object3 = new JSONArray(); int i = 0; //TODO we are not combining the location.address element of both POIs. Now we just take the first one (the one that conditions position) ////////////////////// //Add name. We take the one that first appears in the name ArrayList ////////////////////// for (int j = 0; j < fr.getName().size(); j++) { if (fr.getName().get(j).equals(source1)) { int namePos = CommonUtils.getPropertyPos(poi1, "name", log); object3.put(i, poi1.getJSONArray("label").getJSONObject(namePos)); i++; log.info("Fusion.fusion(). Fusioning. Inserting name from source: " + source1); name_source = source1; break; } if (fr.getName().get(j).equals(source2)) { int namePos = CommonUtils.getPropertyPos(poi2, "name", log); object3.put(i, poi2.getJSONArray("label").getJSONObject(namePos)); log.info("Fusion.fusion(). Fusioning. Inserting name from source: " + source2); name_source = source2; i++; break; } } //////////////////// //Add other labels //////////////////// ArrayList<String> allObjects = new ArrayList<String>(); for (int j = 0; j < object1.length(); j++) { //If is not name if (!poi1.getJSONArray("label").getJSONObject(j).get("term").equals("name")) { String value = (String) poi1.getJSONArray("label").getJSONObject(j).get("value"); //If is not repeated if (!allObjects.contains(value)) { object3.put(i, poi1.getJSONArray("label").getJSONObject(j)); allObjects.add(value); i++; } } } for (int j = 0; j < object2.length(); j++) { //If is not name if (!poi2.getJSONArray("label").getJSONObject(j).get("term").equals("name")) { String value = (String) poi2.getJSONArray("label").getJSONObject(j).get("value"); //If is not repeated if (!allObjects.contains(value)) { object3.put(i, poi2.getJSONArray("label").getJSONObject(j)); allObjects.add(value); i++; } } } //We'll store the final POI in poi1. We preserve the structure and override step by step. Up to now only label poi1.put("label", object3); /////////////////////////////////// //Fusion description tag. It is possible that this tag does not appear in some POIs, so we must be careful ////////////////////////////////// try { object1 = poi1.getJSONArray("description"); } catch (JSONException e) { object1 = null; log.warn("Fusion.fusion(). Fusioning. POI object from source " + source1 + " does not have description. We'll take the description from the other POI independently of the FusionRule"); } try { object2 = poi2.getJSONArray("description"); } catch (JSONException e) { object2 = null; log.warn("Fusion.fusion(). Fusioning. POI object from source " + source2 + " does not have description. We'll take the description from the other POI independently of the FusionRule"); } object3 = new JSONArray(); i = 0; if (object1 == null) { object3.put(i, poi2.getJSONArray("description").getJSONObject(i)); } else { if (object2 == null) { object3.put(i, poi1.getJSONArray("description").getJSONObject(i)); } else { for (int j = 0; j < fr.getDescription().size(); j++) { if (fr.getDescription().get(j).equals(source1)) { if (poi1.getJSONArray("description").length() > 0) { object3.put(i, poi1.getJSONArray("description").getJSONObject(i)); log.info("Fusion.fusion(). Fusioning. Inserting description from source: " + source1); break; } } if (fr.getDescription().get(j).equals(source2)) { if (poi2.getJSONArray("description").length() > 0) { object3.put(i, poi2.getJSONArray("description").getJSONObject(i)); log.info( "Fusion.fusion().Fusioning. Inserting description from source: " + source2); break; } } } } } //Override description field poi1.put("description", object3); ////////////////////////// //Fusion category tag ///////////////////////// try { object1 = poi1.getJSONArray("category"); } catch (JSONException e) { object1 = null; log.warn("Fusion.fusion(). Fusioning. POI object from source " + source1 + " does not have category."); } try { object2 = poi2.getJSONArray("category"); } catch (JSONException e) { object1 = null; log.warn("Fusion.fusion(). Fusioning. POI object from source " + source2 + " does not have category."); } allObjects = new ArrayList<String>(); //We don't need it as we will add all categories, we'll not check if they are repeated object3 = new JSONArray(); i = 0; if (object1 == null) { object3.put(i, poi2.getJSONArray("category").getJSONObject(i)); } else { if (object2 == null) { object3.put(i, poi1.getJSONArray("category").getJSONObject(i)); } else { for (int j = 0; j < object1.length(); j++) { String value = (String) object1.getJSONObject(j).get("value"); object3.put(i, object1.getJSONObject(j)); allObjects.add(value); i++; } for (int j = 0; j < object2.length(); j++) { String value = (String) object2.getJSONObject(j).get("value"); object3.put(i, object2.getJSONObject(j)); allObjects.add(value); i++; } } } //Override category poi1.put("category", object3); /////////////////////////// //Fusion link tag /////////////////////////// try { object1 = poi1.getJSONArray("link"); } catch (JSONException e) { object1 = null; log.warn("Fusion.fusion().Fusioning. POI object from source " + source1 + " does not have link."); } try { object2 = poi2.getJSONArray("link"); } catch (JSONException e) { object2 = null; log.warn("Fusion.fusion().Fusioning. POI object from source " + source2 + " does not have link."); } allObjects = new ArrayList<String>(); object3 = new JSONArray(); i = 0; if (object1 != null) { for (int j = 0; j < object1.length(); j++) { String value = (String) object1.getJSONObject(j).get("value"); //If is not repeated if (!allObjects.contains(value)) { object3.put(i, object1.getJSONObject(j)); allObjects.add(value); i++; } } } if (object2 != null) { for (int j = 0; j < object2.length(); j++) { String value = (String) object2.getJSONObject(j).get("value"); //If is not repeated if (!allObjects.contains(value)) { object3.put(i, object2.getJSONObject(j)); allObjects.add(value); i++; } } } //Override link poi1.put("link", object3); //Finally we should override the publisher part to say 'Fusion Engine'. Anyway we can set it up during the storage of the POI in the database //Create the FusionResult return object fusionresult = new FusionResult(poi1, name_source); } catch (Exception e) { System.out.println("Error.Fusion.fusion(): " + e.getMessage()); log.error("Error.Fusion.fusion(): " + e.getMessage()); } return fusionresult; }
From source file:org.upv.satrd.fic2.fe.connectors.citySDK.Fusion.java
public static int save(Connection con, JSONObject poi, String category, String name_sourceName, String position_sourceName, ArrayList<POISource> poisourceArray, Logger log) { Integer poiid = -1;//from w ww . jav a 2 s .c o m if (log == null) log = Fusion.log; try { //Generate the POI object Double lat = new Double(CommonUtils.getLatitude(poi, log)); Double lon = new Double(CommonUtils.getLongitude(poi, log)); String name = CommonUtils.getName(poi, log); Date date = CommonUtils.getFormatedDateAsDate(); POI poi_object = new POI(name, lat, lon, date); poiid = POI.savePOI(con, poi_object, log); if (poiid != null) { //This is just visual output of pois being inserted. You may comment/remove these two lines System.out.println("poiid: " + poiid); System.out.println(poi.toString()); JSONArray object; JSONObject aux; String type, value, source, language, license; Date updated = null; Integer licenseId = null; Integer typeId = null; Integer sourceId = null; //Insert category in POICategory table. We need to get previously the category_id Category cat = Category.getCategoryClassByName(con, category, log); POICategory poicategory = new POICategory(poiid, cat.getId()); POICategory.savePOICategory(con, poicategory, log); //We need to fill the POISource table for (int p = 0; p < poisourceArray.size(); p++) { org.upv.satrd.fic2.fe.db.POISource poisource = poisourceArray.get(p); poisource.setPOiId(poiid); org.upv.satrd.fic2.fe.db.POISource.savePOISource(con, poisource, log); } //Insert the address value String address = CommonUtils.getAddress(poi, log); if (address != null) { type = "address"; LabelType labeltype = LabelType.getLabelTypeClassByName(con, type, log); if (labeltype == null) { //the ' address' LabelType has not yet been inserted (probably it should during initialization). Create it labeltype = new LabelType(type); typeId = LabelType.saveLabelType(con, labeltype, log); } else { typeId = labeltype.getId(); } //value value = address; //'source' is the one we have used for the position sourceId = Source.getSourceClassByName(con, position_sourceName, log).getId(); language = ""; license = ""; updated = CommonUtils.getFormatedDateAsDate(); POILabel poilabel = new POILabel(poiid, typeId, value, sourceId, language, licenseId, updated); POILabel.savePOILabel(con, poilabel, log); } //Insert description in the POILabel table try { object = poi.getJSONArray("description"); } catch (JSONException e) { object = null; log.debug( "Fusion.save(). Saving POI. The POI does not have any description. No label to insert in the POILabel table"); } if (object != null) { aux = object.getJSONObject(0); //'term' should always be there. It should say 'description' type = (String) aux.get("term"); LabelType labeltype = LabelType.getLabelTypeClassByName(con, type, log); if (labeltype == null) { //the ' description' LabelType has not yet been inserted (probably it should during initialization). Create it labeltype = new LabelType(type); typeId = LabelType.saveLabelType(con, labeltype, log); } else { typeId = labeltype.getId(); } //'value' should always be there. However some POIS do not have a value field in the description object, // so we need to catch this potential Exception try { value = (String) aux.get("value"); } catch (JSONException e) { value = " "; log.debug( "Fusion.save().Saving POI. The POI has description, but no value field. It will be empty"); } //'source' should always be there source = (String) aux.get("base"); sourceId = Source.getSourceClassByName(con, source, log).getId(); try { language = (String) aux.get("lang"); } catch (JSONException e) { language = ""; //log.debug("Saving POI. The 'description' field does not have a 'lang' subfield" ); } try { license = (String) aux.get("license"); licenseId = License.getLicenseClassByName(con, license, log).getId(); } catch (JSONException e) { license = ""; //log.debug("Saving POI. The 'description' field does not have a 'license' subfield" ); } // FIXME: we should use updated = (String)aux.get("updated"); updated = CommonUtils.getFormatedDateAsDate(); POILabel poilabel = new POILabel(poiid, typeId, value, sourceId, language, licenseId, updated); POILabel.savePOILabel(con, poilabel, log); } //Generate poilabel for 'name' and 'position' in the POI table. So we can know from which source 'name' and 'position' comes from. //In these cases the 'value' field does not make sense, so we insert "-1" instead of null //name LabelType labeltype = LabelType.getLabelTypeClassByName(con, "name", log); Integer name_labeltypeid; if (labeltype == null) { //the ' name' LabelType has not yet been inserted (probably it should during initialization). Create it labeltype = new LabelType("name"); name_labeltypeid = LabelType.saveLabelType(con, labeltype, log); } else { name_labeltypeid = labeltype.getId(); } sourceId = Source.getSourceClassByName(con, name_sourceName, log).getId(); POILabel poilabel_aux = new POILabel(poiid, name_labeltypeid, name_sourceName, sourceId, null, null, updated); POILabel.savePOILabel(con, poilabel_aux, log); //position. Here we do not need to save the position (analog as with name) labeltype = LabelType.getLabelTypeClassByName(con, "position", log); Integer position_labeltypeid; if (labeltype == null) { //the ' position' LabelType has not yet been inserted (probably it should during initialization). Create it labeltype = new LabelType("position"); position_labeltypeid = LabelType.saveLabelType(con, labeltype, log); } else { position_labeltypeid = labeltype.getId(); } sourceId = Source.getSourceClassByName(con, position_sourceName, log).getId(); poilabel_aux = new POILabel(poiid, position_labeltypeid, position_sourceName, sourceId, null, null, updated); POILabel.savePOILabel(con, poilabel_aux, log); //Insert link in the POILabel table. The link field has as term values: web, url try { object = poi.getJSONArray("link"); } catch (JSONException e) { object = null; log.debug( "Fusion.save(). Saving POI. Fusion object does not have any link. No label to insert in the POILabel table"); } if (object != null) { for (int i = 0; i < object.length(); i++) { aux = object.getJSONObject(i); //'term' should always be there //type = (String)aux.get("term"); We do not use this as we may find 'web' in poiproxy and 'url' in dbpedia type = "link"; labeltype = LabelType.getLabelTypeClassByName(con, type, log); if (labeltype == null) { labeltype = new LabelType(type); typeId = LabelType.saveLabelType(con, labeltype, log); } else { typeId = LabelType.getLabelTypeClassByName(con, type, log).getId(); } //'value' should always be there value = (String) aux.get("value"); //'source' should always be there source = (String) aux.get("base"); sourceId = Source.getSourceClassByName(con, source, log).getId(); try { language = (String) aux.get("lang"); } catch (JSONException e) { language = ""; //og.debug("Saving POI. The 'link' field does not have a 'lang' subfield" ); } try { license = (String) aux.get("license"); licenseId = License.getLicenseClassByName(con, license, log).getId(); } catch (JSONException e) { licenseId = null; //log.debug("Saving POI. The 'link' field does not have a 'license' subfield" ); } // FIXME: we should use updated = (String)aux.get("updated"); updated = CommonUtils.getFormatedDateAsDate(); POILabel poilabel = new POILabel(poiid, typeId, value, sourceId, language, licenseId, updated); POILabel.savePOILabel(con, poilabel, log); } } //Insert all additional labels in the POILabel table. try { object = poi.getJSONArray("label"); } catch (JSONException e) { object = null; log.debug( "Fusion.save(). Saving POI. Fusion object does not have any labels. No label to insert in the POILabel table"); } if (object != null) { for (int i = 0; i < object.length(); i++) { aux = object.getJSONObject(i); //'term' should always be there type = (String) aux.get("term"); labeltype = LabelType.getLabelTypeClassByName(con, type, log); if (labeltype == null) { labeltype = new LabelType(type); typeId = LabelType.saveLabelType(con, labeltype, log); } else { typeId = LabelType.getLabelTypeClassByName(con, type, log).getId(); } //Skip id term. It will be translated in the POISource table if (!type.equalsIgnoreCase("id")) { //'value' should always be there. However some POIS do not have a value field in the description object, // so we need to catch this potential Exception try { value = (String) aux.get("value"); } catch (JSONException e) { value = " "; log.debug( "Fusion.save(). Saving POI. The POI has a label, but no value field. It will be empty"); } //'source' should always be there source = (String) aux.get("base"); sourceId = Source.getSourceClassByName(con, source, log).getId(); try { language = (String) aux.get("lang"); } catch (JSONException e) { language = ""; //log.debug("Saving POI. This 'label' field does not have a 'lang' subfield" ); } try { license = (String) aux.get("license"); License license_aux = License.getLicenseClassByName(con, license, log); if (license_aux == null) licenseId = null; else licenseId = license_aux.getId(); } catch (Exception e) { licenseId = null; //log.debug("Saving POI. This 'label' field does not have a 'license' subfield" ); } // FIXME: we should use updated = (String)aux.get("updated"); updated = CommonUtils.getFormatedDateAsDate(); POILabel poilabel = new POILabel(poiid, typeId, value, sourceId, language, licenseId, updated); POILabel.savePOILabel(con, poilabel, log); } } } } } catch (Exception e) { System.out.println("Error.Fusion. save(): " + e.getMessage()); log.error("Error.Fusion.save(): " + e.getMessage()); } return poiid; }
From source file:test.TestJSON.java
public static void main(String[] args) { try {/*from www . j a va 2 s . c o m*/ String origin = "{\n" + " \"retCode\": 1,\n" + " \"retDesc\": \"?\",\n" + " \"clerkInfo\": {\n" + " \"MemberID\": \"\",\n" + " \"StatusCode\": \"2011\",\n" + " \"StatusText\": \"??\",\n" + " \"MemberText\": \"\",\n" + " \"MemberPhone\": \"\",\n" + " \"MemberName\": \"\",\n" + " \"IntegralName\": \"\",\n" + " \"TradingType\": \"\",\n" + " \"ResidualIntegral\": \"\",\n" + " \"CardType\": \"\"\n" + " }\n" + "}"; JSONObject jsonObj = new JSONObject(origin); String clerkInfo = jsonObj.getString("clerkInfo"); JSONObject jsonObjArray = new JSONObject(clerkInfo); JSONArray jsonArray = jsonObjArray.getJSONArray(clerkInfo); System.out.println(clerkInfo); } catch (JSONException ex) { System.out.println(ex.getMessage()); } }
From source file:com.sciamlab.ckan4j.exception.CKANException.java
public CKANException(JSONException e) { super("JSONException"); this.error = new JSONObject(); this.error.put("__type", "JSONException"); this.error.put("message", e.getMessage()); }
From source file:com.eTilbudsavis.etasdk.network.RequestQueue.java
private void appendRequestNetworkLog(Request<?> r) { JSONObject log = r.getNetworkLog();// w w w . j a va 2 s .c o m try { log.put("method", r.getMethod().toString()); log.put("url", Utils.requestToUrlAndQueryString(r)); log.put(HTTP.CONTENT_TYPE, r.getBodyContentType()); log.put("headers", new JSONObject(r.getHeaders())); log.put("time", Utils.dateToString(new Date())); } catch (JSONException e) { EtaLog.e(TAG, e.getMessage(), e); } }