rurihabachi
Japanese Home



Home

Math Tools



>Web API<

How to use

Calculation
Function(x)
F(x)-x graph
Differentiation



Feedback









How to use Web APIs

Access to the Web API URLs listed in the specification pages.
Each Web API returns XML / JSON (JSONP) / CSV data. (Some APIs return images.)

You can submit required parameters via either the HTTP POST or GET method.

      *      *      *

Return data samples:
 (See also each specification page for more details.)

  XML
<?xml version="1.0" encoding="UTF-8"?>

<result function="calculate" >
  <expression>2+20</expression>
  <status>0</status>
  <message>OK</message>
  <count>1</count>
  <value>
    <calculatedvalue>22.0</calculatedvalue>
  </value>
</result>

  JSON
{"expression":"2+20","status":0,"message":"OK","count":1,"value":[{"calculatedvalue":"22.0"}]}

  JSONP (using callback parameter)
FunctionA({"expression":"2+20","status":0,"message":"OK","count":1,"value":[{"calculatedvalue":"22.0"}]})

  CSV
"2+20",0,"OK",1
"22.0"


      *      *      *

Source code examples:
  calling Web API and receiving data

  Get XML data in Java (GET method)
//connect to the URL
URL url = new URL("http://www.rurihabachi.com/web/webapi/calculator/xml?exp=" +
        URLEncoder.encode("2+20", "UTF-8"));
URLConnection urlcon = url.openConnection();
InputStream stream = urlcon.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String document = "";
String ln = System.getProperty("line.separator");

//get data
String line = reader.readLine();
while(line != null){
    if ("".equals(document)) {
        document = line;
    } else {
        document = document + ln + line;
    }
    line = reader.readLine();
}

System.out.println(document);
reader.close();

String startTag = "";
String endTag = "
";
int idxfrom = document.indexOf(startTag) + startTag.length();
int idxto = document.indexOf(endTag);
String result = document.substring(idxfrom, idxto);
System.out.println("result=" + result);

  Get JSON data in Java (POST method)
//connect to the URL
URL url = new URL("http://www.rurihabachi.com/web/webapi/calculator/json");
URLConnection urlcon = url.openConnection();
HttpURLConnection httpcon = (HttpURLConnection) urlcon;

httpcon.setDoOutput(true);
httpcon.setDoInput(true);
httpcon.setRequestMethod("POST");

//set parameters
OutputStream outstream = httpcon.getOutputStream();
Writer writer = new OutputStreamWriter(outstream);
writer.write("exp=" + URLEncoder.encode("2+20", "UTF-8"));
writer.flush();
writer.close();

//get data
InputStream stream = httpcon.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String document = reader.readLine();
reader.close();

String start = "\"calculatedvalue\":\"";
String end = "\"}";
int idxfrom = document.indexOf(start) + start.length();
int idxto = document.indexOf(end);
String result = document.substring(idxfrom, idxto);
System.out.println("result=" + result);

  Get JSONP data in Java Script
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function getCalcAnswer() {
    var exp = document.getElementById('exp').value;
    var target = document.createElement('script');
    target.charset = 'UTF-8';
    target.src = "http://www.rurihabachi.com/web/webapi/calculator/json?exp=" +
            encodeURIComponent(exp) + "&callback=setResult";
    document.body.appendChild(target);
}
function setResult(result) {
    if ( result.count > 0 ) {
        document.getElementById('result').innerHTML = result.expression +
            '=' + result.value[0].calculatedvalue;
    }
}
</script>
</head>

<body>
<input type="text" id="exp" size=100>
<input type="button" value="calc" onclick="getCalcAnswer()">
<span id="result"></span>
</body>
</html>



Copyright (C) 2011-2020 rurihabachi. All rights reserved.