LoginSignup
1
1

More than 3 years have passed since last update.

メタデータ株式会社のこの猫なに猫APIをJavaで呼び出す。

Last updated at Posted at 2019-09-20

1.はじめに

メタデータ株式会社のユーザー登録を行い、ユーザーIDとパスワードを設定する必要があります。

ユーザー登録はこちらから登録を行ってください。

2.APIと連携する

APIの連携しようがいまいちわからなかったが、色々と試して以下のソースに落ち着いた。
POSTで通信するみたいで、imageの部分に画像データを設定するとうまくJSONが返ってくる。
画像データはByteで送るように作ってみたので、こちらをアプリに取り込んでみますか。

pom.xml
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.10</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.10</version>
</dependency>
WhatCat.java
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * この猫なに猫?API.
 *
 * @author H.Aoshima
 * @version 1.0
 *
 */
public final class WhatCat {

    /**
     * URI.
     */
    private static final String SUGGETS_URL = "http://whatcat.ap.mextractr.net/api_query";

    /**
     * 実行.
     * @param bytes 画像データ
     * @param fileName 画像ファイル名
     * @param userName ユーザ名
     * @param password パスワード
     * @return 判定結果
     * @throws ClientProtocolException クライアントプロトコル例外
     * @throws IOException IO例外
     */
    public static List<Object> exec(final byte[] bytes, final String fileName, final String userName, final String password)
            throws ClientProtocolException, IOException {

        // 返り値
        List result = null;

        // クライアント作成
        final HttpClient client = HttpClientBuilder.create().build();
        // エンコード前にバイト配列に置き換える際のCharset
        final Charset charset = StandardCharsets.UTF_8;
        // エンコードしたい文字列
        final String source = userName + ":" + password;
        // エンコード処理
        final String encoding = Base64.getEncoder().encodeToString(source.getBytes(charset));
        final HttpPost post = new HttpPost(WhatCat.SUGGETS_URL);
        post.setHeader("Authorization", "Basic " + encoding);

        // MultipartEntityBuilderiに画像の値を設定
        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("image", bytes, ContentType.MULTIPART_FORM_DATA, fileName);

        post.setEntity(builder.build());

        // レスポンスを取得
        final HttpResponse response = client.execute(post);
        final HttpEntity responseEntity = response.getEntity();

        if (response.getStatusLine().getStatusCode() == 200) {
            // 値の取得
            final InputStream is = responseEntity.getContent();
            final Reader r = new InputStreamReader(is, charset);

            ObjectMapper mapper = new ObjectMapper();
            result = mapper.readValue(r, new TypeReference<List<Object>>() {});
        }

        return result;
    }

}
1
1
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
1
1