Android使用Sinch向手机发送短信

时间:2017-03-27 06:09:48

标签: android android-sinch-api

我正在尝试使用Sinch发送短信,但我从服务器收到404和500错误。

我阅读了Sinch文档,我的代码与示例相符。有什么遗失的吗?

您可以在下面看到我的代码。当我尝试读取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();
        }

    }

0 个答案:

没有答案