###前提・実現したいこと
AndroidでWeb APIを使用しようとしています。
JavaからMicrosoft Cognitive ServicesのEmotion APIを呼び出して顔画像の感情分析を行ってみる
このサイトを例にして
TryEmotionRecognitionByImage.java
試してみようと思いました。
###発生している問題・エラーメッセージ
先ほどのリンクにあるコードを使ってみると
29行目のCloseableHttpClient client = HttpClients.createDefault()でエラーが出ます。
エラーメッセージ Caused by: java.lang.NoSuchFieldError: No field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar:classes2.dex)
Java
1@Override 2 protected void onCreate(Bundle savedInstanceState) { 3 super.onCreate(savedInstanceState); 4 setContentView(R.layout.activity_main); 5 6 // 1. HTTPクライアントを生成 7 //ここでエラーが出る 8 try (CloseableHttpClient client = HttpClients.createDefault()) { 9 10 // 2. POST要求を生成 11 // 分析対象の画像のURLを指定するJSON文字列 12 String req = "{ \"url\": \"https://portalstoragewuprod.azureedge.net/media/Default/Media/EmotionAPI/Emotion.jpg\" }"; 13 // POSTメソッドを生成 14 HttpPost post = new HttpPost("EMOTION_API_URL"); 15 post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json"); 16 post.setHeader("Ocp-Apim-Subscription-Key", "SUBSCRIPTION"); 17 post.setEntity(new StringEntity(req)); 18 19 // 3. APIを呼び出し応答データを取得 20 try (CloseableHttpResponse response = client.execute(post)) { 21 String resp = EntityUtils.toString(response.getEntity()); 22 System.out.println(resp); 23 } 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } 27 } 28}
###補足情報(言語/FW/ツール等のバージョンなど)
言語はJava、AndroidStudio2.3.3、compileSdkVersion 25、buildToolsVersion "25.0.3"です。
利用ライブラリはHttpClient 4.5.3 (GA)です。
このエラーの原因は何なのでしょうか。また、どのように書き換えれば解決できるのでしょうか。教えていただきたいです。
あなたの回答
tips
プレビュー