6

Can anyone explain how can I use the ElasticSearch API in Android. Does anyone successfully integrated the api in android?

I added following dependencies in Gradle:

compile 'org.elasticsearch.client:transport:5.2.1'

Of course I run into issues:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE File1: C:\Users\dude.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpcore-nio\4.4.5\f4be009e7505f6ceddf21e7960c759f413f15056\httpcore-nio-4.4.5.jar File2: C:\Users\dude.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpasyncclient\4.1.2\95aa3e6fb520191a0970a73cf09f62948ee614be\httpasyncclient-4.1.2.jar File3: C:\Users\dude.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.dataformat\jackson-dataformat-yaml\2.8.6\8bd44d50f9a6cdff9c7578ea39d524eb519e35ab\jackson-dataformat-yaml-2.8.6.jar File4: C:\Users\dude.gradle\caches\modules-2\files-2.1\org.apache.httpcomponents\httpcore\4.4.5\e7501a1b34325abb00d17dde96150604a0658b54\httpcore-4.4.5.jar File5: C:\Users\dude.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.8.6\2ef7b1cc34de149600f5e75bc2d5bf40de894e60\jackson-core-2.8.6.jar

.

UPDATE 1:

Well I have to use the REST API using Android Asynchronous Http Client, because adding packagingOptions does not solve the issue

Community
  • 1
  • 1
lidox
  • 1,901
  • 3
  • 21
  • 40
  • Possible duplicate of [Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'](http://stackoverflow.com/questions/34031395/errorexecution-failed-for-task-apptransformresourceswithmergejavaresfordebug) – Istiak Morsalin Feb 22 '17 at 10:46
  • 1
    adding packagingOptions does not solve the issue – lidox Feb 22 '17 at 10:49
  • 1
    They have support for android? I don't think so – Istiak Morsalin Feb 22 '17 at 11:24
  • @lidox can you update on this? Did you find a solution? Cause even I am stuck at the same. – Bugs Buggy Jun 28 '17 at 06:21
  • see my answer below. I figured it out. In addition, this is a second working example: https://github.com/lidox/nccn-distress-thermometer/blob/50629c63d78a3de47ffe3950ebb5ef03319ddd1c/NCCN/app/src/main/java/com/artursworld/nccn/controller/elasticsearch/ElasticRestClient.java – lidox Jun 28 '17 at 08:15

2 Answers2

4

Ok so I found out how to access a REST API from Android using a library. Check out more details on Android Asynchronous Http Client and Github.

First add the permission into the manifest

<uses-permission android:name="android.permission.INTERNET" />

In gradle add:

compile 'com.loopj.android:android-async-http:1.4.9'

Now you can start implementing the REST API like this:

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

public class ElasticRestClient {

    private static final String BASE_URL = "http://httpbin.org/"; //http://localhost:9200/
    private static final String CLASS_NAME = ElasticRestClient.class.getSimpleName();

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }

    public void getHttpRequest() {
        try {


            ElasticRestClient.get("get", null, new JsonHttpResponseHandler() { // instead of 'get' use twitter/tweet/1
                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    // If the response is JSONObject instead of expected JSONArray
                    Log.i(CLASS_NAME, "onSuccess: " + response.toString());
                }

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                    Log.i(CLASS_NAME, "onSuccess: " + response.toString());
                }

                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    Log.e(CLASS_NAME, "onFailure");
                    // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                }

                @Override
                public void onRetry(int retryNo) {
                    Log.i(CLASS_NAME, "onRetry " + retryNo);
                    // called when request is retried
                }
            });
        }
        catch (Exception e){
            Log.e(CLASS_NAME, e.getLocalizedMessage());
        }
    }
}
lidox
  • 1,901
  • 3
  • 21
  • 40
  • Can you provide a short example about how we add query into this and make a call? – Bugs Buggy Jun 29 '17 at 11:21
  • take a look at this: https://github.com/lidox/nccn-distress-thermometer/tree/master/NCCN/app/src/main/java/com/artursworld/nccn/controller/elasticsearch In ElasticQuestionnaire.java see the method post(...). At the moment I am very busy. I may make short example in future – lidox Jun 30 '17 at 06:49
3

I faced exactly the same issue when I tried to add the ES rest client in gradle:

compile 'org.elasticsearch.client:rest:5.4.0'

I finally resolved it by adding these lines at the top of my build.gradle file:

android {
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
    }
}

The lines above simply remove all duplicated dependencies. And after doing it, you can refer to the ES documentation for querying your data.

Maximilien Belinga
  • 3,076
  • 2
  • 25
  • 39