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.
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