src/org/ohmage/lib/OhmageApi.java
private String processJsonResponse(final byte[] response, 
			final String jsonKey) throws ApiException, RequestErrorException {
		
		JSONObject jsonResponse;
		try {
			jsonResponse = new JSONObject(new String(response));
		}
		catch(JSONException e) {
			throw new IllegalArgumentException(
					"The response is not valid JSON.", 
					e);
		}
		
		boolean success;
		try {
			success = Request.RESULT_SUCCESS.equals(
					jsonResponse.getString(Request.JSON_KEY_RESULT));
		}
		catch(JSONException e) {
			throw new IllegalArgumentException(
					"There is no '" + 
						Request.RESULT_SUCCESS + 
						"' key in the response, but it is valid JSON. " +
						"This indicates a deeper problem with the server.", 
					e);
		}
		
		if((success) && (jsonKey != null)) {
			try {
				return jsonResponse.getString(jsonKey);
			}
			catch(JSONException e) {
				throw new IllegalArgumentException(
						"The key '" + jsonKey + "' does not exist in the successful JSON response.", e);
			}
		}
		else if(success) {
			return null;
		}
		else {
			// Generate an ohmage-specific error.
			try {
				JSONObject error = jsonResponse.getJSONArray(
						Request.JSON_KEY_ERRORS).getJSONObject(0);
				
				String errorCodeString = 
					error.getString(Annotator.JSON_KEY_CODE);
				ErrorCode errorCode;
				try {
					errorCode = ErrorCode.getValue(errorCodeString);
				}
				catch(IllegalArgumentException e) {
					throw new ApiException("The error code was unknown.", e);
				}
				
				String errorText = error.getString(Annotator.JSON_KEY_TEXT);
				
				throw new RequestErrorException(errorCode, errorText);
			}
			catch(JSONException e) {
				throw new IllegalArgumentException(
						"The failed JSON response doesn't contain a proper error object.",
						e);
			}
		}
	}
/**
	 * Parses the byte array returned from the 
	 * {@link #makeRequest(URL, Map, boolean)} call, verifies that it is JSON,
	 * ensures that it was a successful call, and returns the string value
	 * associated with the given key.
	 * 
	 * @param response The byte array response as returned by 
	 * 				   {@link #makeRequest(URL, Map, boolean)}.
	 * 
	 * @param jsonKey The key string to use to get the response value from the
	 * 				  successful JSON response. If null, the response will be
	 * 				  checked for success or error and, if error, will throw an
	 * 				  exception.
	 * 
	 * @return Returns the value associated with the 'jsonKey'.
	 * 
	 * @throws IllegalArgumentException Thrown if the response cannot be 
	 * 									decoded into JSON or if there is no
	 * 									such key with the responded JSON.
	 * 
	 * @throws ApiException Thrown if the error code is unknown.
	 * 
	 * @throws RequestErrorException Thrown if the server returned a valid JSON
	 * 								 response, but the request failed.
	 * 
	 * @see #makeRequest(URL, Map, boolean)
	 */
Parses the byte array returned from the
#makeRequest(URL, Map, boolean) call, verifies that it is JSON,
ensures that it was a successful call, and returns the string value
associated with the given key.

@param response The byte array response as returned by
#makeRequest(URL, Map, boolean).

@param jsonKey The key string to use to get the response value from the
successful JSON response. If null, the response will be
checked for success or error and, if error, will throw an
exception.

@return Returns the value associated with the 'jsonKey'.

@throws IllegalArgumentException Thrown if the response cannot be
decoded into JSON or if there is no
such key with the responded JSON.

@throws ApiException Thrown if the error code is unknown.

@throws RequestErrorException Thrown if the server returned a valid JSON
response, but the request failed.


Parses the byte array returned from the #makeRequest ( URL Map boolean ) call verifies that it is JSON ensures that it was a successful call and returns the string value associated with the given key . @param response The byte array response as returned by #makeRequest ( URL Map boolean ) . @param jsonKey The key string to use to get the response value from the successful JSON response . If null the response will be checked for success or error and if error will throw an exception . @return Returns the value associated with the ' jsonKey ' . @throws IllegalArgumentException Thrown if the response cannot be decoded into JSON or if there is no such key with the responded JSON . @throws ApiException Thrown if the error code is unknown . @throws RequestErrorException Thrown if the server returned a valid JSON response but the request failed .
private String processJsonResponse(final byte[] response, 
			final String jsonKey) throws ApiException, RequestErrorException {
		JSONObject jsonResponse;
		try {
			jsonResponse = new JSONObject(new String(response));
		}
		catch(JSONException e) {
			throw new IllegalArgumentException(
					"The response is not valid JSON.", 
					e);
		}
		boolean success;
		try {
			success = Request.RESULT_SUCCESS.equals(
					jsonResponse.getString(Request.JSON_KEY_RESULT));
		}
		catch(JSONException e) {
			throw new IllegalArgumentException(
					"There is no '" + 
						Request.RESULT_SUCCESS + 
						"' key in the response, but it is valid JSON. " +
						"This indicates a deeper problem with the server.", 
					e);
		}
		if((success) && (jsonKey != null)) {
			try {
				return jsonResponse.getString(jsonKey);
			}
			catch(JSONException e) {
				throw new IllegalArgumentException(
						"The key '" + jsonKey + "' does not exist in the successful JSON response.", e);
			}
		}
		else if(success) {
			return null;
		}
		else {
			try {
				JSONObject error = jsonResponse.getJSONArray(
						Request.JSON_KEY_ERRORS).getJSONObject(0);
				String errorCodeString = 
					error.getString(Annotator.JSON_KEY_CODE);
				ErrorCode errorCode;
				try {
					errorCode = ErrorCode.getValue(errorCodeString);
				}
				catch(IllegalArgumentException e) {
					throw new ApiException("The error code was unknown.", e);
				}
				String errorText = error.getString(Annotator.JSON_KEY_TEXT);
				throw new RequestErrorException(errorCode, errorText);
			}
			catch(JSONException e) {
				throw new IllegalArgumentException(
						"The failed JSON response doesn't contain a proper error object.",
						e);
			}
		}
	}
private String processJsonResponse ( final byte [ ] response , final String jsonKey ) throws ApiException , RequestErrorException { JSONObject jsonResponse ; try { jsonResponse = new JSONObject ( new String ( response ) ) ; } catch ( JSONException e ) { throw new IllegalArgumentException ( "The response is not valid JSON." , e ) ; } boolean success ; try { success = Request . RESULT_SUCCESS . equals ( jsonResponse . getString ( Request . JSON_KEY_RESULT ) ) ; } catch ( JSONException e ) { throw new IllegalArgumentException ( "There is no '" + Request . RESULT_SUCCESS + "' key in the response, but it is valid JSON. " + "This indicates a deeper problem with the server." , e ) ; } if ( ( success ) && ( jsonKey != null ) ) { try { return jsonResponse . getString ( jsonKey ) ; } catch ( JSONException e ) { throw new IllegalArgumentException ( "The key '" + jsonKey + "' does not exist in the successful JSON response." , e ) ; } } else if ( success ) { return null ; } else { try { JSONObject error = jsonResponse . getJSONArray ( Request . JSON_KEY_ERRORS ) . getJSONObject ( 0 ) ; String errorCodeString = error . getString ( Annotator . JSON_KEY_CODE ) ; ErrorCode errorCode ; try { errorCode = ErrorCode . getValue ( errorCodeString ) ; } catch ( IllegalArgumentException e ) { throw new ApiException ( "The error code was unknown." , e ) ; } String errorText = error . getString ( Annotator . JSON_KEY_TEXT ) ; throw new RequestErrorException ( errorCode , errorText ) ; } catch ( JSONException e ) { throw new IllegalArgumentException ( "The failed JSON response doesn't contain a proper error object." , e ) ; } } }
Parses the byte array returned from the
 {@link #makeRequest(URL, Map, boolean)} call, verifies that it is JSON,
 ensures that it was a successful call, and returns the string value
 associated with the given key.
Parses the byte array returned from the { @link #makeRequest ( URL Map boolean ) } call verifies that it is JSON ensures that it was a successful call and returns the string value associated with the given key .
// Generate an ohmage-specific error.