4

I'm implementing Twilio programmable voice call in java android studio as description they gave in their docs but facing some error when run

See the exception I received below:

Exception in thread "main" java.lang.NoSuchFieldError: INSTANCE
        at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.getDefaultRegistry(PoolingHttpClientConnectionManager.java:109)
        at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:116)
        at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:58)
        at com.twilio.http.TwilioRestClient$Builder.build(TwilioRestClient.java:102)
        at com.twilio.Twilio.getRestClient(Twilio.java:122)
        at com.twilio.base.Creator.create(Creator.java:45)
        at com.systema.twilliovoicecalltest.MakePhoneCall.main(MakePhoneCall.java:33)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

When I tried to exclude http libraries like this:

implementation(group: "com.twilio.sdk", name: "twilio", version: "7.34.1") {
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    exclude group: 'org.apache.httpcomponents', module: 'httpcore'
}

implementation(group: "com.sparkjava", name: "spark-core", version: "2.7.1") {
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    exclude group: 'org.apache.httpcomponents', module: 'httpcore'
}

implementation(group: "org.slf4j", name: "slf4j-simple", version: "1.7.21") {
    exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    exclude group: 'org.apache.httpcomponents', module: 'httpcore'
}

the exception changed to:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/ProtocolVersion
    at com.twilio.http.TwilioRestClient$Builder.build(TwilioRestClient.java:102)
    at com.twilio.Twilio.getRestClient(Twilio.java:122)
    at com.twilio.base.Creator.create(Creator.java:45)
    at com.systema.twilliovoicecalltest.MakePhoneCall.main(MakePhoneCall.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.ClassNotFoundException: org.apache.http.ProtocolVersion
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 9 more

Code

public static void main(String[] args) throws URISyntaxException {
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);

    String from = "+18572632XXX";
    String to = "+92**********";

    Call call = Call.creator(new PhoneNumber(to), new PhoneNumber(from),
            new URI("http://demo.twilio.com/docs/voice.xml")).create();


    System.out.println(call.getSid());
}

For the above code, I've included the following libraries

implementation group: "com.twilio.sdk", name: "twilio", version: "7.34.1"
implementation group: "com.sparkjava", name: "spark-core", version: "2.7.1"
implementation group: "org.slf4j", name: "slf4j-simple", version: "1.7.21"

As i have not much experience in this please help

1 Answer 1

2

Twilio developer evangelist here. As you've noticed, Android will have trouble using our Java SDK as one of its dependencies is the Apache library for HTTP. What you want to do though is use our Android SDK for making calls.

You can see an example of how to use it here, as well as go through the quickstart that will take you through the entire process.

In your example, you are using the Java SDK which beings a security problem into your application, as its now hard coding your account credentials into your final APK. I explain why that is not a good idea in this blog post.

Hope this helps you.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for your help, let me go through this that yo shared :)
That's awesome! Please make sure you mark the answer as correct for anyone with that same question in the future.
I marked it but they are saying my reputation is low

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.