I'm trying to use Twilio api(9.1.0) to send sms but i'm getting an error.
E/AndroidRuntime: FATAL EXCEPTION: Thread-7
Process: au.com.thermalog, PID: 2004
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.conn.ssl.SSLConnectionSocketFactory.getSocketFactory(SSLConnectionSocketFactory.java:194)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.getDefaultRegistry(PoolingHttpClientConnectionManager.java:116)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.<init>(PoolingHttpClientConnectionManager.java:123)
at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:70)
at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:45)
at com.twilio.http.NetworkHttpClient.<init>(NetworkHttpClient.java:36)
at com.twilio.http.TwilioRestClient$Builder.build(TwilioRestClient.java:143)
at com.twilio.Twilio.buildRestClient(Twilio.java:202)
at com.twilio.Twilio.getRestClient(Twilio.java:174)
at com.twilio.base.Creator.create(Creator.java:40)
at au.com.thermalog.service.SMSService.sendSMS(SMSService.java:41)
at au.com.thermalog.service.EmailService.lambda$sendEmail$0$au-com-thermalog-service-EmailService(EmailService.java:121)
at au.com.thermalog.service.EmailService$$ExternalSyntheticLambda0.run(Unknown Source:4)
at java.lang.Thread.run(Thread.java:923)
How can I resolve it?
Twilio.init();
Message message = Message.creator(
new PhoneNumber("+234353"),
"want to use Alphanumeric Id",
"Your message")
.create();
This is the code that I'm using.
Solution:
protected void sendSMS(String to, String sms) {
OkHttpClient client = new OkHttpClient();
String url = "https://api.twilio.com/2010-04-01/Accounts/ACC_SID/SMS/Messages";
String base64EncodedCredentials = "Basic " + Base64.encodeToString(ACC_SID+":"+AUTH_TOKEN).getBytes(), Base64.NO_WRAP);
RequestBody body = new FormBody.Builder()
.add("From","MG4c4e29ab......") // Here you put your Alphanumeric Id HexCode
.add("To", to)
.add("Body", sms)
.build();
Request request = new Request.Builder()
.url(url)
.post(body)
.header("Authorization", base64EncodedCredentials)
.build();
try {
Response response = client.newCall(request).execute();
Log.d(TAG, "sendSms: " + response.body().string());
} catch (IOException e) {
e.printStackTrace();
Log.d(TAG, "sendSms: " + e.getMessage());
}
}
add this dependency
implementation 'com.squareup.okhttp3:okhttp:4.9.0'
If you want to secure your account details, then you can get help from the official documentation.