package org.fan.Sendsms;import java.net.*;
import java.io.*;public class Sendnow
{
public void Sendsms(){};
public void Send(String des,String Content) throws IOException
{
Sendsms(des,Content);
}
public void Send(String [] des,String Content) throws IOException
{
for(int i=0;i<des.length;i++)
{
Sendsms(des[i],Content);
}
}
public void Sendsms(String des,String Content) 
throws java.io.IOException
{
try 
{
URL sms = new URL("http://202.120.107.211:8800/?PhoneNumber="+des+"&Text="+URLEncoder.encode(Content,"GB2312")+"&charset=GB2312" );

sms.openStream( );

System.out.println("sms发送完毕");
} catch (MalformedURLException e) 
{
System.err.println(e);
}
catch (IOException e) 
{
System.err.println(e);
}
    
}
}需求的API是Send(String des,String Content,Date date),新加参数date实现定期执行我的想法是用timer.schedule(Task,date,0)的方法实现,但不知道这里的以上代码定义成这里的Task求教

解决方案 »

  1.   

    Send(String des, String Content, Date date){
         Timer timer = new Timer();
         
         timer.schedule(new TimerTask(){
                             public void run(){
                                 //add your code here to send your msg;
                              }
                        }, date, 0);
    }继承并重写TimerTask里的run方法,就可以用timer.schedule(...)来指定时间触发它。
      

  2.   

    to larryzhao(Larry.Zhao) :
    thx
    我试了下,好象有点问题,
    public void run(){}因为是inner class而得不到des,Content的传值,
    还有继承TimerTask时也报了异常
    麻烦看看是否有误,代码和异常如下public class Sendnow extends TimerTask
    {
    public void Sendsms(){}; public void Send(String des,String Content,Date date) throws IOException
    {
    Sendsms(des,Content,date);
    } public void Sendsms(String des,String Content,Date date) 
    throws java.io.IOException
    {
    Timer timer = new Timer();
     
    timer.schedule(new TimerTask()
    {         
    public void run()
    {
    try 
    {
    System.out.println(des+Content+"sms发送完毕");
    }
    catch (MalformedURLException e) 
    {
    System.err.println(e);
    }
    catch (IOException e) 
    {
    System.err.println(e);
    }
    }
    }, date, 0);
    }
    }报错:
    Sendnow.java:10: Sendnow is not abstract and does not override abstract method r
    un() in java.util.TimerTask
    public class Sendnow extends TimerTask
           ^
    Sendnow.java:33: local variable des is accessed from within inner class; needs t
    o be declared final
    System.out.println(des+Content+"sms发送完毕");
                       ^
      

  3.   

    public class Sendnow
    {
    public void Sendsms()
    {
    }; public void Send(String des, String Content, Date date) throws IOException
    {
    Sendsms(des, Content, date);
    } public void Sendsms(String des, String Content, Date date) throws java.io.IOException
    {
    Timer timer = new Timer(); final String str1= des;
    final String str2= Content;
    timer.schedule(new TimerTask()
    {
    public void run()
    {
    try
    {
    System.out.println(str1 + str2 + "sms发送完毕");
    }
    catch (MalformedURLException e)
    {
    System.err.println(e);
    }
    catch (IOException e)
    {
    System.err.println(e);
    }
    }
    }, date, 0);
    }
    }
      

  4.   

    改好了,只要有一个TimerTask对象就好了,你用Sendnow去继承TimerTask就出了第一个问题了,我这种写法就是写起来方便而已,对于你的项目而言,你还是自己写一个如下SendTask extends TimerTask 里面记录你要的数据比较好。关于内部类的问题,我回答的时候的确没有考虑到你要访问的是局部变量,象下面这样改就可以使用父类的fields了import java.io.IOException;
    import java.util.Date;
    import java.util.Timer;
    import java.util.TimerTask;public class Sendnow {
    String theDes;
    String theContent; public void Sendsms() {
    }; public void Send(String des, String Content, Date date) throws IOException {
    Sendsms(des, Content, date);
    } public void Sendsms(String des, String Content, Date date)
    throws java.io.IOException {

    theDes = des;
    theContent = Content;

    Timer timer = new Timer(); timer.schedule(new TimerTask() {
    public void run() {
    System.out.println(Sendnow.this.theDes + Sendnow.this.theContent + "sms发送完毕");
    }
    }, date, 0);
    }
    }