Call this cat API of Metadata Co., Ltd. in Java.

1.First of all

It is necessary to register as a user of Metadata Co., Ltd. and set a user ID and password.

To register as a user, please go to here.

2. Link with API

I wasn't sure how to link the API, but I tried various things and settled on the following sources. It seems to communicate with ** POST **, and if you set the image data in the image part, JSON will be returned successfully. I made the image data to be sent in Byte, so would you like to import this into the app?

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;

/**
 *What is this cat? 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";

    /**
     *Run.
     * @param bytes image data
     * @param fileName Image file name
     * @param userName username
     * @param password password
     * @return Judgment result
     * @throws ClientProtocolException Client protocol exception
     * @throws IOException IO exception
     */
    public static List<Object> exec(final byte[] bytes, final String fileName, final String userName, final String password)
            throws ClientProtocolException, IOException {

        //Return value
        List result = null;

        //Client creation
        final HttpClient client = HttpClientBuilder.create().build();
        //Charset when replacing with byte array before encoding
        final Charset charset = StandardCharsets.UTF_8;
        //The string you want to encode
        final String source = userName + ":" + password;
        //Encoding process
        final String encoding = Base64.getEncoder().encodeToString(source.getBytes(charset));
        final HttpPost post = new HttpPost(WhatCat.SUGGETS_URL);
        post.setHeader("Authorization", "Basic " + encoding);

        //Set image values in MultipartEntityBuilderi
        final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.addBinaryBody("image", bytes, ContentType.MULTIPART_FORM_DATA, fileName);

        post.setEntity(builder.build());

        //Get response
        final HttpResponse response = client.execute(post);
        final HttpEntity responseEntity = response.getEntity();

        if (response.getStatusLine().getStatusCode() == 200) {
            //Get value
            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;
    }

}

Recommended Posts

Call this cat API of Metadata Co., Ltd. in Java.
Call the Windows Notification API in Java
Zabbix API in Java
Call Amazon Product Advertising API 5.0 (PA-API v5) in Java
Java history in this world
Java Stream API in 5 minutes
How to call and use API in Java (Spring Boot)
Implementation of gzip in java
Implementation of tri-tree in Java
Sample code to call the Yahoo! Local Search API in Java
Generate CloudStack API URL in Java
List of members added in Java 9
Hit Zaim's API (OAuth 1.0) in Java
Parsing the COTOHA API in Java
Call TensorFlow Java API from Scala
List of types added in Java 9
JPA (Java Persistence API) in Eclipse
Implementation of like function in Java
Call the super method in Java
This and that of the implementation of date judgment within the period in Java
[Java] I participated in ABC-188 of Atcorder.
I tried using Elasticsearch API in Java
Implementation of DBlayer in Java (RDB, MySQL)
Get the result of POST in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Studying Java 8 (date API in java.time package)
Call GitHub API from Java Socket API part2
Call Java method from JavaScript executed in Java
Try using the Stream API in Java
Try using JSON format API in Java
The story of writing Java in Emacs
Call Visual Recognition in Watson Java SDK
Role of JSP in Web application [Java]
Discrimination of Enums in Java 7 and above