Home
数式ツール
> Web API <
お問い合わせ
|
Web APIの使い方(共通)
各WebAPI説明ページに記載されたURLにアクセスしてください。
それぞれ XML / JSON (JSONP) / CSV形式に対応しています。(一部、画像形式のAPIもあります。)
引数はHTTP POSTメソッド/GETメソッドのどちらでも送信できます。
* * *
出力データの例:
(詳細は各WebAPIの仕様をご覧ください。)
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形式 (callback変数を指定)
FunctionA({"expression":"2+20","status":0,"message":"OK","count":1,"value":[{"calculatedvalue":"22.0"}]})
|
CSV形式
* * *
サンプルソース:
WebAPIを呼び出し、データを受け取ります。
JavaからXMLデータを取得(GETメソッド)
//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);
|
JavaからJSONデータを取得(POSTメソッド)
//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);
|
JavaScriptからJSONPデータを取得
<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-2024 rurihabachi. All rights reserved.
|