Android Studio中的Twilio示例 [英] Example of Twilio in Android Studio

查看:234
本文介绍了Android Studio中的Twilio示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我很难找到一个在Android中发送Twilio消息的示例.有人可以举一个示例以及我需要导入的jar文件吗?

解决方案

此处是Twilio开发人员的福音.

我认为您很难找到它的原因是,通常不建议直接使用Twilio在Android上发送消息.

这是因为要使用Twilio发送SMS消息,您将需要一个应用程序Sid和一个Auth Token,这是您使用Twilio的凭据.如果您要使用Java库发送此SMS,则意味着您的身份验证密钥与应用程序打包在一起,这也意味着任何对您的应用程序进行反编译的人都可以访问这些密钥,并可以使用您的帐户执行相同的操作.

我们通常建议这样做的方法是创建一个后端应用程序,然后向该应用程序发出HTTP请求.这样,您的密钥就存在于后端应用程序中,并且您可以根据自己的喜好对其进行配置,因此,即使您希望消息来自设备本身,也可以将其传递给后端应用程序.

此页面中,您将找到如何使用以下方式发送SMS消息所有最常见的编程语言中的Twilio.

一旦部署了其中之一,您要做的就是从您的应用发出HTTP请求.有很多不同的库可以帮助您做到这一点,但是我最喜欢的是Square的 OkHttp .

>

以下是使用它向后端应用发出HTTP请求的方式:

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  Response response = client.newCall(request).execute();
  return response.body().string();
}

希望这对您有帮助!

Well,I am getting difficulty finding out an example of Twilio message sending in Android.Could anybody site me an example along with what jar files do I need to import?

解决方案

Twilio developer evangelist here.

I think the reason you're having trouble finding that, is because sending messages on Android directly with Twilio is usually not recommended.

That is because to send an SMS message with Twilio, you will need an application Sid and an Auth Token, which are your credentials with Twilio. If you were to use the Java Library to send this SMS, it would mean your authentication keys are packaged with your application, which also means anyone who decompiles your application gets access to those keys and can use your account to do the same.

The way we usually recommend doing that is by creating a backend application, and then making an HTTP request to it. This way your keys live in the backend application, and you can configure it however you like, so even if you wanted the message to come from the device itself, you could just pass that to your backend application.

In this page you will find how to send SMS messages using Twilio in all the most common programming languages.

Once you have one of those deployed, all you need to do is make an HTTP request to it from your app. There are lots of different libraries to help you doing that, but my favourite is Square's OkHttp.

Here's how you could make an HTTP request to your backend app using it:

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  Response response = client.newCall(request).execute();
  return response.body().string();
}

Hope this helps you!

这篇关于Android Studio中的Twilio示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆