List of usage examples for org.json JSONObject remove
public Object remove(String key)
From source file:eu.codeplumbers.cosi.services.CosiCallService.java
public void sendChangesToCozy() { List<Call> unSyncedCalls = Call.getAllUnsynced(); int i = 0;/* ww w . j a v a2s. c om*/ for (Call call : unSyncedCalls) { URL urlO = null; try { JSONObject jsonObject = call.toJsonObject(); mBuilder.setProgress(unSyncedCalls.size(), i, false); mBuilder.setContentText("Syncing " + jsonObject.getString("docType") + ":"); mNotifyManager.notify(notification_id, mBuilder.build()); EventBus.getDefault() .post(new CallSyncEvent(SYNC_MESSAGE, getString(R.string.lbl_calls_send_changes))); String remoteId = jsonObject.getString("remoteId"); String requestMethod = ""; if (remoteId.isEmpty()) { urlO = new URL(syncUrl); requestMethod = "POST"; } else { urlO = new URL(syncUrl + remoteId + "/"); requestMethod = "PUT"; } HttpURLConnection conn = (HttpURLConnection) urlO.openConnection(); conn.setConnectTimeout(5000); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Authorization", authHeader); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod(requestMethod); // set request body jsonObject.remove("remoteId"); long objectId = jsonObject.getLong("id"); jsonObject.remove("id"); OutputStream os = conn.getOutputStream(); os.write(jsonObject.toString().getBytes("UTF-8")); os.flush(); // read the response InputStream in = new BufferedInputStream(conn.getInputStream()); StringWriter writer = new StringWriter(); IOUtils.copy(in, writer, "UTF-8"); String result = writer.toString(); JSONObject jsonObjectResult = new JSONObject(result); if (jsonObjectResult != null && jsonObjectResult.has("_id")) { result = jsonObjectResult.getString("_id"); call.setRemoteId(result); call.save(); } in.close(); conn.disconnect(); } catch (MalformedURLException e) { EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage())); stopSelf(); } catch (ProtocolException e) { EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage())); stopSelf(); } catch (IOException e) { EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage())); stopSelf(); } catch (JSONException e) { EventBus.getDefault().post(new CallSyncEvent(SERVICE_ERROR, e.getLocalizedMessage())); stopSelf(); } i++; } }
From source file:org.collectionspace.chain.csp.webui.record.RecordRead.java
private JSONObject getHierarchy(Storage storage, JSONObject fields) throws JSONException, ExistException, UnimplementedException, UnderlyingStorageException { for (Relationship r : record.getSpec().getAllRelations()) { if (r.showSiblings()) { //JSONObject temp = new JSONObject(); //temp.put("_primary", true); JSONArray children = new JSONArray(); //children.put(temp); fields.put(r.getSiblingParent(), children); if (fields.has(r.getID())) { //String broadterm = fields.getString(r.getID()); String child = r.getSiblingChild(); if (fields.has(child)) { String broader = fields.getString(child); JSONObject restrict = new JSONObject(); restrict.put("dst", broader); restrict.put("type", "hasBroader"); JSONObject reldata = storage.getPathsJSON("relations/hierarchical", restrict); fields.remove(child); for (int i = 0; i < reldata.getJSONObject("moredata").length(); i++) { String[] reld = (String[]) reldata.get("listItems"); String hcsid = reld[i]; JSONObject mored = reldata.getJSONObject("moredata").getJSONObject(hcsid); //it's name is JSONObject siblings = new JSONObject(); if (!fields.getString("csid").equals(mored.getString("subjectcsid"))) { siblings.put(child, mored.getString("subjectrefname")); children.put(siblings); }// w w w. j a va2s .c o m } } fields.put(r.getSiblingParent(), children); } } //add empty array if necessary if (!fields.has(r.getID()) && r.mustExistInSpec()) { if (r.getObject().equals("n")) { JSONObject temp = new JSONObject(); temp.put("_primary", true); JSONArray at = new JSONArray(); at.put(temp); fields.put(r.getID(), at); } else { fields.put(r.getID(), ""); } } } return fields; }
From source file:net.openwatch.acluaz.fragment.FormFragment.java
public JSONObject toJson(ViewGroup container, JSONObject json) { String TAG = "FormFragment-ToJSON"; if (container == null) { Log.e(TAG, "null container passed to toJson"); return new JSONObject(); }// www . j av a 2s .co m if (json == null) json = new JSONObject(); View view; for (int x = 0; x < container.getChildCount(); x++) { view = container.getChildAt(x); if (EditText.class.isInstance(view)) { if (view.getTag() != null) { if (((EditText) view).getText().toString().compareTo("") == 0) continue; // skip blank input try { Log.i(TAG, "Mapping: " + view.getTag().toString() + " value: " + ((EditText) view).getText().toString()); if (view.getTag().toString().compareTo(getString(R.string.zipcode_tag)) == 0) json.put(view.getTag().toString(), Integer.parseInt(((EditText) view).getText().toString())); else json.put(view.getTag().toString(), ((EditText) view).getText().toString()); } catch (JSONException e) { Log.e(TAG, "Error jsonifying text input"); e.printStackTrace(); } } } else if (CompoundButton.class.isAssignableFrom(view.getClass())) { if (view.getTag() != null) { // if location toggle, bundle location if (((String) view.getTag()).compareTo(getString(R.string.device_location_tag)) == 0 && view.getTag(R.id.view_tag) != null) { if (((CompoundButton) view).isChecked()) { try { json.put(getString(R.string.device_lat), ((Location) view.getTag(R.id.view_tag)).getLatitude()); json.put(getString(R.string.device_lon), ((Location) view.getTag(R.id.view_tag)).getLongitude()); } catch (JSONException e) { Log.e(TAG, "Error jsonifying toggle input"); e.printStackTrace(); } } } else if (((String) view.getTag()).compareTo(getString(R.string.device_location_tag)) == 0 && view.getTag(R.id.view_tag) == null) { // no location tagged, get last known LocationManager lm = (LocationManager) container.getContext() .getSystemService(Context.LOCATION_SERVICE); Location last = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); try { json.put(getString(R.string.device_lat), last.getLatitude()); json.put(getString(R.string.device_lon), last.getLongitude()); } catch (JSONException e) { Log.e(TAG, "Error jsonifying last location"); e.printStackTrace(); } catch (NullPointerException e2) { Log.e(TAG, "No current or historical location info on this device"); } } } } // combine date and time fields into a single datetime if (json.has(getString(R.string.date_tag)) && json.has(getString(R.string.time_tag))) { Log.i(TAG, "found date and time tag, let's smush 'em"); try { //TESTING //String datetime = combineDateAndTime(json.getString(getString(R.string.date_tag)), json.getString(getString(R.string.time_tag))); //Log.i(TAG,"datetime: " + datetime); json.put(getString(R.string.date_tag), combineDateAndTime(json.getString(getString(R.string.date_tag)), json.getString(getString(R.string.time_tag)))); Log.i(TAG, json.toString()); //json.remove(getString(R.string.date_tag)); json.remove(getString(R.string.time_tag)); } catch (JSONException e) { Log.e(TAG, "Error creating json datetime field from date and time"); e.printStackTrace(); } } } Log.i(TAG, "toJson: " + json.toString()); return json; }
From source file:at.alladin.rmbt.android.util.GetMapOptionsInfoTask.java
/** * //from www. ja v a2s .c om */ @Override protected void onPostExecute(final JSONObject result) { if (serverConn.hasError()) hasError = true; else if (result != null) { try { final JSONObject mapSettingsObject = result.getJSONObject("mapfilter"); // / // //////////////////////////////////////////////////// // MAP / CHOOSE final JSONArray mapTypeArray = mapSettingsObject.getJSONArray("mapTypes"); // Log.d(DEBUG_TAG, mapTypeArray.toString(4)); // / final ArrayList<MapListSection> mapListSectionList = new ArrayList<MapListSection>(); for (int cnt = 0; cnt < mapTypeArray.length(); cnt++) { final JSONObject t = mapTypeArray.getJSONObject(cnt); final String sectionTitle = t.getString("title"); final JSONArray objectOptionsArray = t.getJSONArray("options"); // / final List<MapListEntry> mapListEntryList = new ArrayList<MapListEntry>(); for (int cnt2 = 0; cnt2 < objectOptionsArray.length(); cnt2++) { final JSONObject s = objectOptionsArray.getJSONObject(cnt2); final String entryTitle = s.getString("title"); final String entrySummary = s.getString("summary"); final String value = s.getString("map_options"); final String overlayType = s.getString("overlay_type"); final MapListEntry mapListEntry = new MapListEntry(entryTitle, entrySummary); mapListEntry.setKey("map_options"); mapListEntry.setValue(value); mapListEntry.setOverlayType(overlayType); mapListEntryList.add(mapListEntry); } final MapListSection mapListSection = new MapListSection(sectionTitle, mapListEntryList); mapListSectionList.add(mapListSection); } // //////////////////////////////////////////////////// // MAP / FILTER final JSONObject mapFiltersObject = mapSettingsObject.getJSONObject("mapFilters"); final HashMap<String, List<MapListSection>> mapFilterListSectionListHash = new HashMap<String, List<MapListSection>>(); // Log.d(DEBUG_TAG, mapFilterArray.toString(4)); for (final String typeKey : new String[] { "mobile", "wifi", "browser" }) { final JSONArray mapFilterArray = mapFiltersObject.getJSONArray(typeKey); final List<MapListSection> mapFilterListSectionList = new ArrayList<MapListSection>(); mapFilterListSectionListHash.put(typeKey, mapFilterListSectionList); // add map appearance option (satellite, no satellite) final MapListSection appearanceSection = new MapListSection( activity.getString(R.string.map_appearance_header), Arrays.asList( new MapListEntry(activity.getString(R.string.map_appearance_nosat_title), activity.getString(R.string.map_appearance_nosat_summary), true, MapProperties.MAP_SAT_KEY, MapProperties.MAP_NOSAT_VALUE, true), new MapListEntry(activity.getString(R.string.map_appearance_sat_title), activity.getString(R.string.map_appearance_sat_summary), MapProperties.MAP_SAT_KEY, MapProperties.MAP_SAT_VALUE))); mapFilterListSectionList.add(appearanceSection); // add overlay option (heatmap, points) final MapListSection overlaySection = new MapListSection( activity.getString(R.string.map_overlay_header), Arrays.asList( new MapListEntry( activity.getString(R.string.map_overlay_auto_title), activity.getString(R.string.map_overlay_auto_summary), true, MapProperties.MAP_OVERLAY_KEY, MapOverlay.AUTO.name(), true), new MapListEntry(activity.getString(R.string.map_overlay_heatmap_title), activity.getString(R.string.map_overlay_heatmap_summary), MapProperties.MAP_OVERLAY_KEY, MapOverlay.HEATMAP.name()), new MapListEntry(activity.getString(R.string.map_overlay_points_title), activity.getString(R.string.map_overlay_points_summary), MapProperties.MAP_OVERLAY_KEY, MapOverlay.POINTS.name()), new MapListEntry(activity.getString(R.string.map_overlay_regions_title), activity.getString(R.string.map_overlay_regions_summary), MapProperties.MAP_OVERLAY_KEY, MapOverlay.REGIONS.name()), new MapListEntry(activity.getString(R.string.map_overlay_municipality_title), activity.getString(R.string.map_overlay_municipality_summary), MapProperties.MAP_OVERLAY_KEY, MapOverlay.MUNICIPALITY.name()), new MapListEntry(activity.getString(R.string.map_overlay_settlements_title), activity.getString(R.string.map_overlay_settlements_summary), MapProperties.MAP_OVERLAY_KEY, MapOverlay.SETTLEMENTS.name()), new MapListEntry(activity.getString(R.string.map_overlay_whitespots_title), activity.getString(R.string.map_overlay_whitespots_summary), MapProperties.MAP_OVERLAY_KEY, MapOverlay.WHITESPOTS.name()))); mapFilterListSectionList.add(overlaySection); // add other filter options for (int cnt = 0; cnt < mapFilterArray.length(); cnt++) { final JSONObject t = mapFilterArray.getJSONObject(cnt); final String sectionTitle = t.getString("title"); final JSONArray objectOptionsArray = t.getJSONArray("options"); // / final List<MapListEntry> mapListEntryList = new ArrayList<MapListEntry>(); boolean haveDefault = false; for (int cnt2 = 0; cnt2 < objectOptionsArray.length(); cnt2++) { final JSONObject s = objectOptionsArray.getJSONObject(cnt2); final String entryTitle = s.getString("title"); final String entrySummary = s.getString("summary"); final boolean entryDefault = s.optBoolean("default", false); s.remove("title"); s.remove("summary"); s.remove("default"); // final MapListEntry mapListEntry = new MapListEntry(entryTitle, entrySummary); // final JSONArray sArray = s.names(); if (sArray != null && sArray.length() > 0) { final String key = sArray.getString(0); mapListEntry.setKey(key); mapListEntry.setValue(s.getString(key)); } mapListEntry.setChecked(entryDefault && !haveDefault); mapListEntry.setDefault(entryDefault); if (entryDefault) haveDefault = true; // / mapListEntryList.add(mapListEntry); } if (!haveDefault && mapListEntryList.size() > 0) { final MapListEntry first = mapListEntryList.get(0); first.setChecked(true); // set first if we had no default first.setDefault(true); } final MapListSection mapListSection = new MapListSection(sectionTitle, mapListEntryList); mapFilterListSectionList.add(mapListSection); } } // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // map type final MapListEntry entry = mapListSectionList.get(0).getMapListEntryList().get(0); activity.setCurrentMapType(entry); activity.setMapTypeListSectionList(mapListSectionList); activity.setMapFilterListSectionListMap(mapFilterListSectionListHash); } catch (final JSONException e) { e.printStackTrace(); } } else Log.i(DEBUG_TAG, "LEERE LISTE"); if (endTaskListener != null) { final JSONArray array = new JSONArray(); array.put(result); endTaskListener.taskEnded(array); } }
From source file:com.joyfulmongo.db.OpCmdIncrease.java
@Override public void onUpdate(String colname, JSONObject parseObject) { Double amount = mObj.getDouble(IncProps.amount.toString()); JSONObject field = new JSONObject(); field.put(key, amount);/* w w w . j a v a2 s. co m*/ parseObject.remove(key); parseObject.put(CMD_INC, field); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java
@Override public Pair<JSONObject, byte[]> splitRaw(JSONObject json) { byte[] raw = FastBase64.decode(json.optString(DATA)); json.remove(DATA); return new Pair<JSONObject, byte[]>(json, raw); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java
public Pair<JSONObject, byte[]> handleUnprocessed(Context context, JSONObject msg) { if (!msg.has(DATA)) { return null; }//from ww w . j a v a2s . co m byte[] bytes = FastBase64.decode(msg.optString(DATA)); msg.remove(DATA); return new Pair<JSONObject, byte[]>(msg, bytes); }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java
@Override public Pair<JSONObject, byte[]> handleOutgoing(JSONObject json) { byte[] bytes = FastBase64.decode(json.optString(DATA)); json.remove(DATA); return new Pair<JSONObject, byte[]>(json, bytes); }
From source file:com.krayzk9s.imgurholo.ui.SingleImageFragment.java
private void getIndents(JSONObject comment, int currentIndent) { JSONArray children;/*w w w . j a v a 2 s . c om*/ try { comment.put("indent", currentIndent); ArrayList<JSONObject> childrenArray = null; if (comment.has("children")) { children = comment.getJSONArray("children"); childrenArray = new ArrayList<JSONObject>(); for (int i = 0; i < children.length(); i++) childrenArray.add(children.getJSONObject(i)); if (sort.equals(getString(R.string.newComment))) Collections.sort(childrenArray, new JSONNewestComparator()); else if (sort.equals(getString(R.string.topComment))) Collections.sort(childrenArray, new JSONTopComparator()); comment.remove("children"); } JSONParcelable commentParse = new JSONParcelable(); commentParse.setJSONObject(comment); commentArray.add(commentParse); if (childrenArray != null) { for (JSONObject child : childrenArray) { getIndents(child, currentIndent + 1); } } } catch (JSONException e) { Log.e("Error5!", e.toString()); } }
From source file:com.brainasylum.andruid.UniqueIdentifierManager.java
public synchronized void clearUdid(Context context) { // if permissions are granted, remove the identifier from // the storage bundle... if (canWriteExternalStorage(context)) { File file = new File(Environment.getExternalStorageDirectory() + "/" + FILE_DIR, FILE_NAME); if (file.exists()) { try { // FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[(int) file.length()]; fis.read(buffer);/*w w w. ja v a 2 s. c o m*/ fis.close(); JSONObject root = new JSONObject(new String(buffer)); // String applicationName = context.getApplicationInfo().packageName; // if (root.has(applicationName)) { JSONObject values = root.getJSONObject(applicationName); if (values.has(_identifier.getKey())) { // remove the identifier value and if it's the // last one in the object remove the application // node itself values.remove(_identifier.getKey()); if (values.length() == 0) { root.remove(applicationName); } // FileOutputStream fos = new FileOutputStream(file); fos.write(root.toString().getBytes()); fos.close(); } } } catch (IOException e) { Log.e(TAG, "I/O error with file " + file.getAbsolutePath(), e); e.printStackTrace(); } catch (JSONException e) { Log.e(TAG, "JSON error with file " + file.getAbsolutePath(), e); e.printStackTrace(); } } } // ... then remove it from the shared preferences... SharedPreferences prefs = context.getSharedPreferences(_identifier.getTag(), Context.MODE_PRIVATE); prefs.edit().remove(_identifier.getKey()).commit(); // ... and clear the instance cached value. _udid = null; }