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!