Source:
public JSONObject createSensor(String name, String displayName, String deviceType, String dataType, String dataStructure, String sessionID) throws IOException, JSONException { if(name == null || name.isEmpty() || sessionID == null || sessionID.isEmpty()|| deviceType == null || deviceType.isEmpty()|| dataType == null || dataType.isEmpty()) throw new IOException("invalid input of name or sessionID or deviceType or dataType"); if(dataStructure == null) { dataStructure = ""; } if(displayName == null) { displayName = ""; } final String url = makeCSRestUrlFor(URL_SENSORS, null); JSONObject sensor = new JSONObject(); sensor.put("name", name); sensor.put("device_type", deviceType); sensor.put("display_name", displayName); sensor.put("pager_type", ""); sensor.put("data_type", dataType); sensor.put("data_structure",dataStructure); JSONObject postData = new JSONObject(); postData.put("sensor", sensor); Map<String, String> response = request(url, postData, sessionID, HTTP_METHOD_POST); String code = response.get(RESPONSE_CODE); int result = checkResponseCode(response.get(RESPONSE_CODE), "createSensor"); if (result != -2) { throw new IOException("Incorrect response of createSensor from CommonSense: " + code); } String locationHeader = response.get("location"); String[] split = locationHeader.split("/"); String id = split[split.length - 1]; if(id == null || id.isEmpty()){ Log.w(TAG, "No id for the newly created sensor"); return null; } sensor.put("sensor_id", id); return sensor; }

Target:
public JSONObject createSensor(String name, String displayName, String deviceType, String dataType, String dataStructure, String sessionID) throws IOException, RuntimeException { if(name == null || name.isEmpty() || sessionID == null || sessionID.isEmpty()|| deviceType == null || deviceType.isEmpty()|| dataType == null || dataType.isEmpty()) throw new IllegalArgumentException("invalid input of name or sessionID or deviceType or dataType"); if(dataStructure == null) { dataStructure = ""; } if(displayName == null) { displayName = ""; } final String url = makeCSRestUrlFor(URL_SENSORS, null); JSONObject sensor = new JSONObject(); JSONObject postData = new JSONObject(); try { sensor.put("name", name); sensor.put("device_type", deviceType); sensor.put("display_name", displayName); sensor.put("pager_type", ""); sensor.put("data_type", dataType); sensor.put("data_structure", dataStructure); postData.put("sensor", sensor); }catch(JSONException js){ throw new RuntimeException("createSensor failed to create the content for the request"); } Map<String, String> response = request(url, postData, sessionID, HTTP_METHOD_POST); String code = response.get(RESPONSE_CODE); int result = checkResponseCode(response.get(RESPONSE_CODE), "createSensor"); if (result != 1) { if(result == -2) throw new IOException("Request of createSensor is forbidden from CommonSense: " + code); else throw new IOException("Failed response other than forbidden of createSensor from CommonSense: " + code); } String locationHeader = response.get("location"); String[] split = locationHeader.split("/"); String id = split[split.length - 1]; if(id == null || id.isEmpty()){ Log.w(TAG, "No id for the newly created sensor"); return null; } try { sensor.put("sensor_id", id); }catch(JSONException js){ throw new RuntimeException("createSensor failed to put the session id into sensor object"); } return sensor; }