gpt4 book ai didi

android - 如何在我的 Android 应用程序中使用 Twilio 发送短信?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:10 25 4
gpt4 key购买 nike

在我的 android 应用程序中,我创建了一个按钮,当我按下按钮时我想发送消息。为此,我创建了一个 java 类并编写了 twilio 代码。

final TwilioRestClient client = new TwilioRestClient(
ACCOUNT_SID, AUTH_TOKEN);

// Get the main account (The one we used to authenticate the
// client)
final Account mainAccount = client.getAccount();

final SmsFactory messageFactory = mainAccount.getSmsFactory();
final Map<String, String> messageParams = new HashMap<String, String>();
messageParams.put("To", "+912342423423");
messageParams.put("From", "+132432432434");
messageParams.put("Body", "This is my message");
try {
messageFactory.create(messageParams);
} catch (TwilioRestException e) {
e.printStackTrace();
}

当我使用上面的代码时,它显示了一些错误,例如 java.lang.NoSuchMethodError: org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager

我在 lib 文件夹中只添加了一个 jar 文件 "twilio-java-sdk-3.3.10-jar-with-dependencies.jar "。

请告诉我我能做什么?

最佳答案

我已经使用 HttpPost 方法来 send sms因为我已经通过基本身份验证传递了我的网址,这是我的代码

HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost(
"https://api.twilio.com/2010-04-01/Accounts/{ACCOUNT_SID}/SMS/Messages");
String base64EncodedCredentials = "Basic "
+ Base64.encodeToString(
(ACCOUNT_SID + ":" + AUTH_TOKEN).getBytes(),
Base64.NO_WRAP);

httppost.setHeader("Authorization",
base64EncodedCredentials);
try {

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("From",
"+123424353534"));
nameValuePairs.add(new BasicNameValuePair("To",
"+914342423434"));
nameValuePairs.add(new BasicNameValuePair("Body",
"Welcome to Twilio"));

httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));

// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
System.out.println("Entity post is: "
+ EntityUtils.toString(entity));


} catch (ClientProtocolException e) {

} catch (IOException e) {

}
}

它运行良好。

关于android - 如何在我的 Android 应用程序中使用 Twilio 发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com