0

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 void send_sms_to_phone(String message) {
        Log.e("SMS", "123");

        try {
            String phoneNumber = "+923005109105";
            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\\" + "984adbd2-31ae-4c73-b4f7-936f2ed4c033" + ":" + "ByV2ql/OWE6oASF/dRdUkw==";
            String userCredentials = "application\\" + appKey + ":" + appSecret;
            byte[] encoded = Base64.encodeBase64(userCredentials.getBytes());
            String basicAuth = "Basic " + new String(encoded);
            connection.setRequestProperty("Authorization", basicAuth);

            String postData = "{\"From\":\"+923005109105\" \"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.e("SMS", response.toString());

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

    }
faisal iqbal
  • 724
  • 1
  • 8
  • 20

0 Answers0