How to send SMS using Twilio in my android application?

后端 未结 6 1787
既然无缘
既然无缘 2020-12-08 17:15

In my android application I have created one button, when I had pressed on the button I want to send message.So for that I have created one java class and written twilio cod

6条回答
  •  难免孤独
    2020-12-08 18:11

    I have used HttpPost method to send sms in that i have passed my url with base authentication here is my code

    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 nameValuePairs = new ArrayList();
                            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) {
    
                        }
                    }
    

    It is working well.

提交回复
热议问题