如何在 Android 中使用 ElasticSearch API

lid*_*dox 6 api android elasticsearch

谁能解释我如何在 Android 中使用 ElasticSearch API。有没有人成功地将 api 集成到 android 中?

我在 Gradle 中添加了以下依赖项:

compile 'org.elasticsearch.client:transport:5.2.1'
Run Code Online (Sandbox Code Playgroud)

当然,我遇到了问题:

错误:任务“:app:transformResourcesWithMergeJavaResForDebug”的执行失败。com.android.build.api.transform.TransformException:com.android.builder.packaging.DuplicateFileException:在 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\file-orgs-2 .apache.httpcomponents\httpasyncclient\4.1.2\95aa3e6fb520191a0970a73cf09f62948ee614be\httpasyncclient-4.1.2.jar File3:C:\Users\dude.gradle\caches\modules-2\xml.jackson.data. -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.

.

更新1:

好吧,我必须使用使用REST API的Android异步HTTP客户端,因为加入packagingOptions没有解决问题

lid*_*dox 4

好的,我找到了如何使用库从 Android 访问 REST API。查看有关Android 异步 Http 客户端Github的更多详细信息。

首先将权限添加到清单中

<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)

在 gradle 中添加:

compile 'com.loopj.android:android-async-http:1.4.9'
Run Code Online (Sandbox Code Playgroud)

现在您可以开始实现 REST API,如下所示:

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());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)