1

I'm trying to send sms using Twilio. I'm using the code given in this post. But I'm getting response code as 201 instead of 200. Also the response <?xml version='1.0' encoding='UTF-8'?> is weird. Is there fault in code or the credentials?

 private class AsyncTaskRunner extends AsyncTask<String, String, String> {

        private String resp;


        @Override
        protected String doInBackground(String... params) {

            try{
                String twilioSID="XYZ";
                String twilioSecret="ABC";



                String urlStr = "https://"+twilioSID+":"+twilioSecret+"@api.twilio.com/2010-04-01/Accounts/"+twilioSID+"/SMS/Messages";

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(urlStr);
                String base64EncodedCredentials = "Basic "
                        + Base64.encodeToString(
                        ("XYZ" + ":" + "ABC").getBytes(),
                        Base64.NO_WRAP);


                httppost.setHeader("Authorization", base64EncodedCredentials);


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

                httppost.setEntity(new UrlEncodedFormEntity(
                        nameValuePairs));

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                int status = response.getStatusLine().getStatusCode();
                System.out.println("sammy_response_code "+status);
                HttpEntity entity = response.getEntity();
                if(entity != null){
                    resp = EntityUtils.toString(entity);
                }


            }catch (Exception e){
                System.out.println("sammy_Exception "+e);
            }


            return resp;
        }

            @Override
        protected void onPostExecute(String result) {
            //pd.dismiss();
                System.out.println("sammy_response "+result);

        }






    }
Community
  • 1
  • 1
Somnath Pal
  • 137
  • 1
  • 4
  • 10

2 Answers2

0

Please check all bellow:

201 response

  1. Is it the "To" phone number correct? It's always worth checking...
  2. Does the "To" phone have reception? If not, try moving to a location with reception and try again.
  3. Does the "To" phone receive SMS messages sent from other sources? Try using another phone to send the "To" phone a test message.
  4. Is the phone you are sending to roaming internationally? Twilio is not able to support international roaming for SMS at this time.
  5. Is the "To" phone number on a "Do Not Contact" list for the country or carrier? For Example, India has a particularly strict national Do Not Contact list. The "To" number will need to be removed from any Do Not Contact lists before it can receive SMS messages from Twilio.
  6. Does the country you are sending an SMS message to have a special prefix which must be used in order to send an SMS? Mexico and Argentina are examples of this.
  7. Did the end number's provider filter it out? Certain carriers will blacklist numbers that are sending a high volume of SMS. Try sending the same message from another Twilio long code. If it is properly received, you've likely encountered a carrier's volume threshold. Check out this FAQ for more information.
  8. Try multiple times. There's no harm in trying multiple times. Trying again is often the best way to prove to yourself and those helping you that you are doing everything correctly. Be sure to copy down the SMS Message SIDs for each attempt which doesn't arrive.
Ave
  • 4,338
  • 4
  • 40
  • 67
0

I got the response when I added ".json" after the URL. Now the response is coming even though the response code is 201.

Somnath Pal
  • 137
  • 1
  • 4
  • 10