Java

From MyMobileWorkers API
Jump to: navigation, search

Java can be used to test the API, although Apache and an external API may be needed to generate JSON content. PHP has built-in functions to create JSON and is therefore the quicker method of testing the API.

Creating a job

SendJob.java

public class ApiClient {
 
	private String apiURL = "INSERT_URL_HERE";
	private String apiVERSION = "INSERT_VERSION_HERE";
	private String apiURLEND = "INSERT_URL_END_HERE";
	private String apiKey = "INSERT_API_KEY_HERE";
 
	private void create(boolean validateOnly, boolean updateExisting, String json, String jobNumber)
	{
		File file = null;
		String endpointURL = apiURL + apiVERSION + apiURLEND;
 
		try 
		{
			// creating a new http client and post
			HttpClient client = new DefaultHttpClient();
			HttpPost post = new HttpPost(endpointUrl);
 
			// add the job json to the file
			FileUtils.writeStringToFile(file, json);
 
			// prepare each part of the request
			ContentBody bin = new FileBody(file);
			StringBody apiKeyContent = new StringBody(apiKey);
 
			// compile the request entity
			MultipartEntity reqEntity = new MultipartEntity();
			reqEntity.addPart("file", bin);
			reqEntity.addPart("api_key", apiKey);
			reqEntity.addPart("validate_only", new StringBody("" + validateOnly));
			reqEntity.addPart("update_existing", new StringBody("" + updateExisting));
			reqEntity.addPart("job_number", new StringBody(jobNumber));		
 
			// run the request
			post.setEntity(reqEntity);
			HttpResponse response = client.execute(post);
			HttpEntity resEntity = response.getEntity();
			String responseString = EntityUtils.toString(resEntity);
		}
		catch(RuntimeException rex)
		{
			throw rex;
		}
		catch(Exception ex)
		{
			throw new RuntimeException(ex);
		}
		finally
		{
			FileUtils.deleteQuietly(file);
		}
	}
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox