使用循环

时间:2016-02-04 03:40:46

标签: java twilio

我目前在从文本文件中读取电话号码时遇到问题,然后让他们遍历电话号码列表并使用Twilio发送短信。

String ACCOUNT_SID = "ACCOUNT_SID"; 
String AUTH_TOKEN = "AUTH_TOKEN";
String fileName = "phoneNumbers.txt";
String line = null;
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

List<NameValuePair> params = new ArrayList<>();
try
{
    FileReader fileReader = new FileReader(fileName);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    while((line = bufferedReader.readLine()) != null)
    {
        System.out.println(line);
        params.add(new BasicNameValuePair("To", line));
        params.add(new BasicNameValuePair("From", "+TWILIO_NUMBER")); 
        params.add(new BasicNameValuePair("Body", "TEST: Test message, please ignore."));
        MessageFactory messageFactory = client.getAccount().getMessageFactory(); 
        Message message = messageFactory.create(params);
        System.out.println(message.getTo() + " " + message.getBody()); 
    }
    bufferedReader.close();
    System.out.println("All messages sent.");
}
catch(FileNotFoundException ex)
{
    System.out.println("Unable to open file.");
}
catch(IOException ex)
{
    System.out.println("Error reading file.");
}   catch (TwilioRestException ex) {
        Logger.getLogger(alert.class.getName()).log(Level.SEVERE, null, ex);
    }

这是我得到的输出:

+PHONE_NUMBER_ONE
+PHONE_NUMBER_ONE Sent from your Twilio trial account - TEST: Test message, please ignore.
+PHONE_NUMBER_TWO
+PHONE_NUMBER_ONE Sent from your Twilio trial account - TEST: Test message, please ignore.
All messages sent.

我可以使用System.out.println(行)读取这两个电话号码,但是当我将它添加到params.add行时,程序不会读取它。我的循环有问题吗,我该如何解决?

非常感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:0)

在while循环的末尾添加了params.clear(),现在一切正常。非常感谢您的帮助