JUNAID ALI
JUNAID ALI

Reputation: 41

Facing some error in implementing twilio programmable voice call in java android studio

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

Upvotes: 4

Views: 456

Answers (1)

Marcos Placona
Marcos Placona

Reputation: 21720

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.

Upvotes: 2

Related Questions