0

I am working on an Android application written in Kotlin that needs to perform a POST operation in Elasticsearch. However, when I try to execute the following line of code:

var client: HttpClient = clientBuilder.build()

I get a NoSuchFieldError with the following message:

2023-03-02 16:28:10.436 7192-7192/com.example.etaj E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.etaj, PID: 7192
java.lang.NoSuchFieldError: No static 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!classes4.dex)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:151)
    at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:977)
    at com.example.etaj.MainActivity.sendFile(MainActivity.kt:130)
    at com.example.etaj.MainActivity.onCreate$lambda-1(MainActivity.kt:57)
    at com.example.etaj.MainActivity.$r8$lambda$qEZx_WglTLbnt9F8F5QgOp6Bbj4(Unknown Source:0)
    at com.example.etaj.MainActivity$$ExternalSyntheticLambda2.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:7570)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1202)
    at android.view.View.performClickInternal(View.java:7525)
    at android.view.View.access$3900(View.java:836)
    at android.view.View$PerformClick.run(View.java:28680)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:254)
    at android.app.ActivityThread.main(ActivityThread.java:8243)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)

2023-03-02 16:28:10.462 7192-7192/com.example.etaj I/Process: Sending signal. PID: 7192 SIG: 9

I am using the following dependencies in my project:

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import co.elastic.clients.elasticsearch.ElasticsearchClient
import co.elastic.clients.elasticsearch.core.SearchResponse
import co.elastic.clients.json.jackson.JacksonJsonpMapper
import co.elastic.clients.transport.ElasticsearchTransport
import co.elastic.clients.transport.rest_client.RestClientTransport
import com.google.android.gms.analytics.ecommerce.Product
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.elasticsearch.client.RestClient
import java.io.File

Here's a code snippet to better understand what I am trying to do:

try {
    val fileName = "C:\\gbif_geo.json"
    val jsonFile = File(fileName)
    val entity =FileEntity(jsonFile,ContentType.APPLICATION_JSON)
    val post = HttpPost("http://localhost:9200/_bulk")
    post.entity = entity
    val clientBuilder = HttpClientBuilder.create()
    var client: HttpClient = clientBuilder.build()//error line
    post.addHeader("content-type", "text/plain")
    post.addHeader("Accept", "text/plain")
    val response: HttpResponse = client.execute(post)
    println("Response: $response")
}catch (e: Exception){
    t.setText("error")
}

Can anyone help me understand what's causing this error and how to fix it? Thanks in advance for your help!

  • 1
    What versions of the apache HttpClient and HttpCore libraries are you using? It sounds like you have a version mismatch – twinklehawk Mar 03 '23 at 14:15
  • does this have anything to do with Elasticsearch? seems like an SSL error that's unrealted – Nate Mar 03 '23 at 15:54
  • @Nate I am not sure what is causing the error. I used the code from this stackoverflow thread: https://stackoverflow.com/questions/50890487/i-want-to-send-json-file-to-elasticsearch-using-java – SCORPIOGA3T Mar 03 '23 at 16:01
  • @twinklehawk here are my dependencies: dependencies { implementation 'androidx.core:core-ktx:1.7.0' [...] implementation'org.apache.httpcomponents:httpclient:4.5.13' implementation 'org.apache.httpcomponents:httpcore:4.4.14' } – SCORPIOGA3T Mar 03 '23 at 16:03
  • Seems like a similar issue to https://stackoverflow.com/questions/32312932/using-apache-httpclient-with-android-sdk-23-nosuchmethod/32313284#32313284. What Android API version are you using? – twinklehawk Mar 04 '23 at 03:48
  • @twinklehawk all the material referenced in that post is outdated and none of the link work plus that post is 8years old – SCORPIOGA3T Mar 05 '23 at 16:29

0 Answers0