-
-
Save anonymous/af5d96f41d9089604505 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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