src/org/ohmage/lib/OhmageApi.java
private byte[] makeRequest(final URL url, 
			final Map<String, Object> postParameters, final boolean isForm) 
		throws ApiException, RequestErrorException {
		
		// Create a default client to use to connect with.
		HttpClient httpClient = new DefaultHttpClient();
		
		// Build the request based on the parameters.
		HttpRequestBase request;
		if(postParameters == null) {
			// This is a GET request and the parameters are encoded in the URL.
			try {
				request = new HttpGet(url.toURI());
			}
			catch(URISyntaxException e) {
				throw new ApiException("There was an error building the request.", e);
			}
		}
		else {
			// This is a POST request and the parameter list must be built.
			try {
				HttpPost postRequest = new HttpPost(url.toURI());
				request = postRequest;
				
				// Build the parameter map which is called a "HttpEntity".
				HttpEntity entity;
				// It is a "multipart/form-data" request and the parameters
				// must be assigned to the 'entity'.
				if(isForm) {
					MultipartEntity multipartEntity = new MultipartEntity();
					entity = multipartEntity;
					
					for(String key : postParameters.keySet()) {
						Object value = postParameters.get(key);
						
						// If it is a File, add it to the entity list as an 
						// attachment.
						if(value instanceof File) {
							multipartEntity.addPart(key, new FileBody((File) value));
						}
						// Otherwise, get its string value and add it as such.
						else if(value != null) {
							try {
								multipartEntity.addPart(key, new StringBody(String.valueOf(value)));
							}
							catch(UnsupportedEncodingException e) {
								throw new ApiException("The value for key '" + key + "' could not be encoded.", e);
							}
						}
					}
				}
				// It is a "application/x-www-form-urlencoded" request and the
				// parameters can each be added as a string-string pair.
				else {
					List<BasicNameValuePair> items = new ArrayList<BasicNameValuePair>(postParameters.size());
					
					for(String key : postParameters.keySet()) {
						Object value = postParameters.get(key);
						
						if(value != null) {
							items.add(new BasicNameValuePair(key, String.valueOf(value)));
						}
					}
					
					try {
						entity = new UrlEncodedFormEntity(items);
					}
					catch(UnsupportedEncodingException e) {
						throw new ApiException("The parameter list could not be properly encoded.", e);
					}
				}
				postRequest.setEntity(entity);
			}
			catch(URISyntaxException e) {
				throw new ApiException("There was an error building the request.", e);
			}
		}
		
		// Make the request and get the response.
		HttpResponse httpResponse;
		try {
			httpResponse = httpClient.execute(request);
		}
		catch(ClientProtocolException e) {
			throw new ApiException("An HTTP protocol error occurred.", e);
		}
		catch(IOException e) {
			throw new ApiException("The connection was aborted.", e);
		}
		
		// Check the status code.
		int statusCode = httpResponse.getStatusLine().getStatusCode();
		// If it is a redirect, get the new location and remake the request.
		if((statusCode == 301) || (statusCode == 302)) {
			String newLocation = httpResponse.getFirstHeader("Location").getValue();
			
			try {
				return makeRequest(new URL(newLocation), postParameters, isForm);
			}
			catch(MalformedURLException e) {
				throw new ApiException("The server returned a bad redirect address: " + newLocation, e);
			}
		}
		// Otherwise, if it is is a non-success code, fail the request.
		else if(statusCode != 200) {
			throw new ApiException("There was an error connecting to the server: " + statusCode);
		}
		
		// Retrieve the server's response as an InputStream.
		InputStream content;
		try {
			content = httpResponse.getEntity().getContent();
		}
		catch(IOException e) {
			throw new ApiException("There was an error connecting to the response from the server.", e);
		}
		
		// Read the results as a byte array. This is used instead of a string 
		// to allow the function to me more open to different types of return 
		// values such as text, images, etc.
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] chunk = new byte[CHUNK_SIZE];
		int amountRead;
		try {
			while((amountRead = content.read(chunk)) != -1) {
				baos.write(chunk, 0, amountRead);
			}
		}
		catch(IOException e) {
			throw new ApiException("There was an error reading from the server.", e);
		}
		byte[] result = baos.toByteArray();
		
		// Finally, check the Content-Type to see if it suggests that this is
		// an ohmage JSON result. If so, check if it failed and, if so, throw
		// an exception.
		Header[] headers = httpResponse.getHeaders(CONTENT_TYPE_HEADER);
		String contentType = headers[0].getValue();
		if(CONTENT_TYPE_HTML.equals(contentType)) {
			checkFailure(result);
		}
		
		// Return the byte array.
		return result;
	}
/**
	 * Makes a call to the URL. The call will be a GET if 'postParameters' is
	 * null and a POST if 'postParameters' is non-null, even if it is empty. If
	 * it is a POST, 'isForm' will set it to be a "multipart/form-data" 
	 * request, but if it is set to false it will default to a 
	 * "application/x-www-form-urlencoded" request.<br />
	 * <br />
	 * If the response has a Content-Type that suggests that it is JSON, it 
	 * will check if the ohmage result is success or failure and throw an 
	 * exception if it is failure.
	 * 
	 * @param url The URL which dictates the location to which the request
	 * 			  should be made. This includes the scheme, which must be an
	 * 			  HTTP scheme ("http" or "https"), a domain, an optional port,
	 * 			  a path, and an optional query string. 
	 * 
	 * @param postParameters A map of keys to values for a POST call. The 
	 * 						 values must be a byte array to facilitate sending
	 * 						 things other than text such as images. If this is
	 * 						 null, the request will be a GET request; if this
	 * 						 is non-null, even if it is empty, it will force it
	 * 						 to be a POST call.
	 * 
	 * @param isForm This flag is used if the 'postParameters' is non-null and
	 * 				 indicates whether this POST should be a 
	 * 				 "multipart/form-data" request or not.
	 * 
	 * @return Returns the result from the server as a byte array. 
	 * 
	 * @throws ApiException Thrown if the URL is not an HTTP URL or if there
	 * 						   was an error communicating with the server.
	 */
Makes a call to the URL. The call will be a GET if 'postParameters' is
null and a POST if 'postParameters' is non-null, even if it is empty.

@param url The URL which dictates the location to which the request
should be made. This includes the scheme, which must be an
HTTP scheme ("http" or "https"), a domain, an optional port,
a path, and an optional query string.

@param postParameters A map of keys to values for a POST call. The
values must be a byte array to facilitate sending
things other than text such as images. If this is
null, the request will be a GET request; if this
is non-null, even if it is empty, it will force it
to be a POST call.

@param isForm This flag is used if the 'postParameters' is non-null and
indicates whether this POST should be a
"multipart/form-data" request or not.

@return Returns the result from the server as a byte array.

@throws ApiException Thrown if the URL is not an HTTP URL or if there
was an error communicating with the server.
Makes a call to the URL . The call will be a GET if ' postParameters ' is null and a POST if ' postParameters ' is non - null even if it is empty . @param url The URL which dictates the location to which the request should be made . This includes the scheme which must be an HTTP scheme ( " http " or " https " ) a domain an optional port a path and an optional query string . @param postParameters A map of keys to values for a POST call . The values must be a byte array to facilitate sending things other than text such as images . If this is null the request will be a GET request ; if this is non - null even if it is empty it will force it to be a POST call . @param isForm This flag is used if the ' postParameters ' is non - null and indicates whether this POST should be a " multipart / form - data " request or not . @return Returns the result from the server as a byte array . @throws ApiException Thrown if the URL is not an HTTP URL or if there was an error communicating with the server .
private byte[] makeRequest(final URL url, 
			final Map<String, Object> postParameters, final boolean isForm) 
		throws ApiException, RequestErrorException {
		HttpClient httpClient = new DefaultHttpClient();
		HttpRequestBase request;
		if(postParameters == null) {
			try {
				request = new HttpGet(url.toURI());
			}
			catch(URISyntaxException e) {
				throw new ApiException("There was an error building the request.", e);
			}
		}
		else {
			try {
				HttpPost postRequest = new HttpPost(url.toURI());
				request = postRequest;
				HttpEntity entity;
				if(isForm) {
					MultipartEntity multipartEntity = new MultipartEntity();
					entity = multipartEntity;
					for(String key : postParameters.keySet()) {
						Object value = postParameters.get(key);
						if(value instanceof File) {
							multipartEntity.addPart(key, new FileBody((File) value));
						}
						else if(value != null) {
							try {
								multipartEntity.addPart(key, new StringBody(String.valueOf(value)));
							}
							catch(UnsupportedEncodingException e) {
								throw new ApiException("The value for key '" + key + "' could not be encoded.", e);
							}
						}
					}
				}
				else {
					List<BasicNameValuePair> items = new ArrayList<BasicNameValuePair>(postParameters.size());
					for(String key : postParameters.keySet()) {
						Object value = postParameters.get(key);
						if(value != null) {
							items.add(new BasicNameValuePair(key, String.valueOf(value)));
						}
					}
					try {
						entity = new UrlEncodedFormEntity(items);
					}
					catch(UnsupportedEncodingException e) {
						throw new ApiException("The parameter list could not be properly encoded.", e);
					}
				}
				postRequest.setEntity(entity);
			}
			catch(URISyntaxException e) {
				throw new ApiException("There was an error building the request.", e);
			}
		}
		HttpResponse httpResponse;
		try {
			httpResponse = httpClient.execute(request);
		}
		catch(ClientProtocolException e) {
			throw new ApiException("An HTTP protocol error occurred.", e);
		}
		catch(IOException e) {
			throw new ApiException("The connection was aborted.", e);
		}
		int statusCode = httpResponse.getStatusLine().getStatusCode();
		if((statusCode == 301) || (statusCode == 302)) {
			String newLocation = httpResponse.getFirstHeader("Location").getValue();
			try {
				return makeRequest(new URL(newLocation), postParameters, isForm);
			}
			catch(MalformedURLException e) {
				throw new ApiException("The server returned a bad redirect address: " + newLocation, e);
			}
		}
		else if(statusCode != 200) {
			throw new ApiException("There was an error connecting to the server: " + statusCode);
		}
		InputStream content;
		try {
			content = httpResponse.getEntity().getContent();
		}
		catch(IOException e) {
			throw new ApiException("There was an error connecting to the response from the server.", e);
		}
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] chunk = new byte[CHUNK_SIZE];
		int amountRead;
		try {
			while((amountRead = content.read(chunk)) != -1) {
				baos.write(chunk, 0, amountRead);
			}
		}
		catch(IOException e) {
			throw new ApiException("There was an error reading from the server.", e);
		}
		byte[] result = baos.toByteArray();
		Header[] headers = httpResponse.getHeaders(CONTENT_TYPE_HEADER);
		String contentType = headers[0].getValue();
		if(CONTENT_TYPE_HTML.equals(contentType)) {
			checkFailure(result);
		}
		return result;
	}
private byte [ ] makeRequest ( final URL url , final Map < String , Object > postParameters , final boolean isForm ) throws ApiException , RequestErrorException { HttpClient httpClient = new DefaultHttpClient ( ) ; HttpRequestBase request ; if ( postParameters == null ) { try { request = new HttpGet ( url . toURI ( ) ) ; } catch ( URISyntaxException e ) { throw new ApiException ( "There was an error building the request." , e ) ; } } else { try { HttpPost postRequest = new HttpPost ( url . toURI ( ) ) ; request = postRequest ; HttpEntity entity ; if ( isForm ) { MultipartEntity multipartEntity = new MultipartEntity ( ) ; entity = multipartEntity ; for ( String key : postParameters . keySet ( ) ) { Object value = postParameters . get ( key ) ; if ( value instanceof File ) { multipartEntity . addPart ( key , new FileBody ( ( File ) value ) ) ; } else if ( value != null ) { try { multipartEntity . addPart ( key , new StringBody ( String . valueOf ( value ) ) ) ; } catch ( UnsupportedEncodingException e ) { throw new ApiException ( "The value for key '" + key + "' could not be encoded." , e ) ; } } } } else { List < BasicNameValuePair > items = new ArrayList < BasicNameValuePair > ( postParameters . size ( ) ) ; for ( String key : postParameters . keySet ( ) ) { Object value = postParameters . get ( key ) ; if ( value != null ) { items . add ( new BasicNameValuePair ( key , String . valueOf ( value ) ) ) ; } } try { entity = new UrlEncodedFormEntity ( items ) ; } catch ( UnsupportedEncodingException e ) { throw new ApiException ( "The parameter list could not be properly encoded." , e ) ; } } postRequest . setEntity ( entity ) ; } catch ( URISyntaxException e ) { throw new ApiException ( "There was an error building the request." , e ) ; } } HttpResponse httpResponse ; try { httpResponse = httpClient . execute ( request ) ; } catch ( ClientProtocolException e ) { throw new ApiException ( "An HTTP protocol error occurred." , e ) ; } catch ( IOException e ) { throw new ApiException ( "The connection was aborted." , e ) ; } int statusCode = httpResponse . getStatusLine ( ) . getStatusCode ( ) ; if ( ( statusCode == 301 ) || ( statusCode == 302 ) ) { String newLocation = httpResponse . getFirstHeader ( "Location" ) . getValue ( ) ; try { return makeRequest ( new URL ( newLocation ) , postParameters , isForm ) ; } catch ( MalformedURLException e ) { throw new ApiException ( "The server returned a bad redirect address: " + newLocation , e ) ; } } else if ( statusCode != 200 ) { throw new ApiException ( "There was an error connecting to the server: " + statusCode ) ; } InputStream content ; try { content = httpResponse . getEntity ( ) . getContent ( ) ; } catch ( IOException e ) { throw new ApiException ( "There was an error connecting to the response from the server." , e ) ; } ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; byte [ ] chunk = new byte [ CHUNK_SIZE ] ; int amountRead ; try { while ( ( amountRead = content . read ( chunk ) ) != - 1 ) { baos . write ( chunk , 0 , amountRead ) ; } } catch ( IOException e ) { throw new ApiException ( "There was an error reading from the server." , e ) ; } byte [ ] result = baos . toByteArray ( ) ; Header [ ] headers = httpResponse . getHeaders ( CONTENT_TYPE_HEADER ) ; String contentType = headers [ 0 ] . getValue ( ) ; if ( CONTENT_TYPE_HTML . equals ( contentType ) ) { checkFailure ( result ) ; } return result ; }
Makes a call to the URL.
Makes a call to the URL .
// Create a default client to use to connect with. // Build the request based on the parameters. // This is a GET request and the parameters are encoded in the URL. // This is a POST request and the parameter list must be built. // Build the parameter map which is called a "HttpEntity". // It is a "multipart/form-data" request and the parameters // must be assigned to the 'entity'. // If it is a File, add it to the entity list as an  // attachment. // Otherwise, get its string value and add it as such. // It is a "application/x-www-form-urlencoded" request and the // parameters can each be added as a string-string pair. // Make the request and get the response. // Check the status code. // If it is a redirect, get the new location and remake the request. // Otherwise, if it is is a non-success code, fail the request. // Retrieve the server's response as an InputStream. // Read the results as a byte array. This is used instead of a string  // to allow the function to me more open to different types of return  // values such as text, images, etc. // Finally, check the Content-Type to see if it suggests that this is // an ohmage JSON result. If so, check if it failed and, if so, throw // an exception. // Return the byte array.