Skip to content

Instantly share code, notes, and snippets.

/Java API Secret

Created November 18, 2015 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/af5d96f41d9089604505 to your computer and use it in GitHub Desktop.
Save anonymous/af5d96f41d9089604505 to your computer and use it in GitHub Desktop.
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