Create a gist now

Instantly share code, notes, and snippets.

anonymous /Java API Secret
Created Nov 18, 2015

What would you like to do?
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.json.JSONObject;
import java.io.*;
public class MetanumberApiExample {
private static String url = "http://tyson.rci.sc.edu/metanumber/api/v1.0/engine/";
public static void main(String[] args) {
//Create metanumber equation to be evaluated.
String equation = "3*m + 5*ft + 25*inch";
//Replace all / with |
equation = equation.replace('/','|');
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url+equation);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
byte[] responseBody = method.getResponseBody();
JSONObject result = new JSONObject(new String(responseBody));
System.out.println(result.getString("result"));
} catch (HttpException e) {
System.out.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment