4

I am trying to send an SMS using Sinch but I am getting 404 and 500 errors from the server.

I read Sinch documentation and my code matches the examples. Is there something missing?

You can see my code below. It crashes when I'm trying to read the InputStream.

 public static boolean sendSMS(String message) {

        try {
            String phoneNumber = "00351961234567";
            URL url = new URL("https://messagingapi.sinch.com/v1/sms/ " + phoneNumber);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            String userCredentials = "application\\" + Constants.SINCH_APP_KEY + ":" + Constants.SINCH_APP_SECRET;

            byte[] encoded = Base64.encodeBase64(userCredentials.getBytes());
            String basicAuth = "Basic " + new String(encoded);
            connection.setRequestProperty("Authorization", basicAuth);

            String postData = "{\"From\":\"00351913470050\" \"Message\":\"" + message + "\"}";
            OutputStream os = connection.getOutputStream();
            os.write(postData.getBytes());
            int status = connection.getResponseCode();

            StringBuilder response = new StringBuilder();
            InputStreamReader is = new InputStreamReader(connection.getInputStream());
            BufferedReader br = new BufferedReader(is);
            String line;
            while ( (line = br.readLine()) != null)
                response.append(line);
            br.close();
            os.close();

            Log.i("SMS", response.toString());

            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }
  • On Android Studio, in line "InputStreamReader is = new InputStreamReader(connection.getInputStream());" it generates the exception FileNotFoundException – Carlos Peixoto Mar 20 '15 at 11:05
  • Can you post the message you're getting along with the error? Sometimes those are more specific, and it could be as simple as not having any money on your account to send SMS with. – mravca Mar 23 '15 at 23:27
  • I'm getting {"errorCode":40001,"message":"Country code PT is not whitelisted for messages"}. I sent an email to Sinch support because on their website they support Portugal communications so let's wait – Carlos Peixoto Mar 24 '15 at 17:56
  • Its working now right? – cjensen May 18 '15 at 23:52
  • Yes, Is working! Thank you cjensen – Carlos Peixoto Jun 24 '15 at 08:27
  • hello @cjensen i have also save issue with sinch send sms from app to phone it not working for me will you please tell how can i send sms from app to phone using sinch? – faisal iqbal Mar 27 '17 at 05:04

1 Answers1

1

All countries that we support are now whitelisted. And you should not have this problem

cjensen
  • 2,703
  • 1
  • 16
  • 15
  • Hi cjensen! The problem happened again only for 91 (Vodafone PT) numbers. I sent an email to Sinch Support. The Receiver Phone doens't ring when a call is made – Carlos Peixoto Jul 20 '15 at 11:19