0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

JavaでCoincheckのpublicAPIを呼び出す

Last updated at Posted at 2021-04-24

呼び出したいAPI

このpublicAPIのtickerを試しに呼び出してみる。

呼び出す情報は次の通り。 
ticker情報

last 最後の取引の価格
bid 現在の買い注文の最高価格
ask 現在の売り注文の最安価格
high 24時間での最高取引価格
low 24時間での最安取引価格
volume 24時間での取引量
timestamp 現在の時刻

Javaで実装

以下のコードでAPIの疎通チェックを行っています。
まだいくつかのAPIがありますが、apiUrlを変更するだけで疎通確認ができます。

qiita sample.java 
		//CoinCheckのAPIの接続チェック
		String apiUrl = "https://coincheck.com/api/ticker";
		URL connectUrl = new URL(apiUrl);
		HttpURLConnection con = (HttpURLConnection) connectUrl.openConnection();
		System.out.println("レスポンスヘッダ:");
		System.out.println("レスポンスコード[" + con.getResponseCode() + "] " +
				"レスポンスメッセージ[" + con.getResponseMessage() + "]");

resultにGETの結果をJSON形式で格納する

qiita sample.java 
		//GETした結果をjson形式で出力する
		InputStream in = con.getInputStream();
		String encoding = con.getContentEncoding();
		if (null == encoding) {
			encoding = "UTF-8";

		}
		StringBuffer result = new StringBuffer();
		final InputStreamReader inReader = new InputStreamReader(in, encoding);
		final BufferedReader bufReader = new BufferedReader(inReader);
		String line = null;

		// 1行ずつテキストを読み込む
		while ((line = bufReader.readLine()) != null) {
			result.append(line);
		}

		bufReader.close();
		inReader.close();
		in.close();

		System.out.println(result);

JSONから抽出したデータをリストに格納して出力する

qiita sample.java 
		 JSONObject json = new JSONObject(result.toString());
		 int last = json.getInt("last");
		 int bid = json.getInt("bid");
		 int ask = json.getInt("ask");
		 int low = json.getInt("low");
		 double volume = json.getDouble("volume");
		 int timestamp = json.getInt("timestamp");

		 List<Object> ticker = new ArrayList<Object>(Arrays.asList(last,bid,ask,low,volume,timestamp));

		 System.out.println(ticker);

分析に使う為にCoinCheckのAPIを取得する実装を試しに書きました。とても冗長なのでまた綺麗にしておきます。

Github:https://github.com/YUJIjs/get-coincheck-api-value

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?