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求教
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求教
解决方案 »
- JAVA监听键盘事件怎么实现退格
- 求助:dom4j写好xml文件后输出的xml文件是正确的,但本地文件没有发生改变
- ItemEvent.DESELECTED情况下不做处理怎么回事啊??
- Java和C/C++/C#/VB/Delphi/Perl/PHP/ASM/哪个好?
- javabean里获得session变量
- 在linux下 怎么下 jdk,java运行环境呢,怎么配的呢? 高分求助啊
- JBuilder 9 中整合 jdk1.5 求助!!!高手来
- 大家这么多人用jbuilder9,你们是在哪里下载的???
- java中“/usr/local/site/mysql/bin/mysql -u root >dbname.txt”应该怎样写?
- :除使用vj6之外,我可用什麽办法把java编译成exe,快帮帮小妹!!
- 两道JAVA多项选择题~~
- jdbc初学提问,请高手不惜赐教
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(...)来指定时间触发它。
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发送完毕");
^
{
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);
}
}
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);
}
}