免费天气预报自动发送,拒绝移动的收费天气服务!
2009-03-02 12:59
716 查看
几个月前于空余时间写了一个免费天气预报自动发送小工具,挺实用,运行至今,同学朋友至今都说好,下面说说怎么用吧!
1.注册移动的即时聊天工具飞信(fetion),并把需要发送天气预报的号码加为好友。
2.到http://www.it-adv.net/网站上下载飞信软件,因为这个经过改造的飞信支持命令行,而官方飞信软件不支持命令行。将下载好的软件放到C:/fetion下,并在环境变量path中添加C:/fetion/fetion.exe,这个是用作命令行中自动登录飞信。
3.在C盘根目录下新建一个文件夹weather,这个用于放自动发送程序源代码和相关批处理文件。weather下面共有五个文件,下面逐一介绍。第一个文件是getWeather.java,代码如下:
第二个文件是SendMsg.java,代码如下:
第三个文件是fetion.bat,用于自动登录飞信,其中-u后面的是你自己的手机号码,-p后面是你登录飞信的密码,代码如下:
第四个文件是SendWeather.bat,代码如下:
第五个文件是PhoneCfg.properties,里面包含了你要发送天气预报的城市和手机号码,格式为城市名称=手机号码,手机号码....,其中城市名称要用一个工具转换,方法为进入命令行,输入native2ascii,然后回车,输入城市名称,就可以得到这个城市的unicode,如果需要增加城市和手机号码,只需新增一行即可,实例如下:
4.最后的工作,将两个java文件编译一下,设置一个任务计划,目标文件为SendWeather.bat,时间是你想要发送的时间,完成!
ps:这个工具我已经运行了几个月,效果很好!
1.注册移动的即时聊天工具飞信(fetion),并把需要发送天气预报的号码加为好友。
2.到http://www.it-adv.net/网站上下载飞信软件,因为这个经过改造的飞信支持命令行,而官方飞信软件不支持命令行。将下载好的软件放到C:/fetion下,并在环境变量path中添加C:/fetion/fetion.exe,这个是用作命令行中自动登录飞信。
3.在C盘根目录下新建一个文件夹weather,这个用于放自动发送程序源代码和相关批处理文件。weather下面共有五个文件,下面逐一介绍。第一个文件是getWeather.java,代码如下:
import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; import java.net.URLConnection; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class getWeather { /** * 获取SOAP的请求头,并替换其中的标志符号为用户输入的城市 * * * @param city * 用户输入的城市名称 * @return 客户将要发送给服务器的SOAP请求 */ private static String getSoapRequest(String city) { StringBuilder sb = new StringBuilder(); sb .append("<?xml version=/"1.0/" encoding=/"utf-8/"?>" + "<soap:Envelope xmlns:xsi=/"http://www.w3.org/2001/XMLSchema-instance/" " + "xmlns:xsd=/"http://www.w3.org/2001/XMLSchema/" " + "xmlns:soap=/"http://schemas.xmlsoap.org/soap/envelope//">" + "<soap:Body> <getWeatherbyCityName xmlns=/"http://WebXml.com.cn//">" + "<theCityName>" + city + "</theCityName> </getWeatherbyCityName>" + "</soap:Body></soap:Envelope>"); return sb.toString(); } /** * 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流 * * * @param city * 用户输入的城市名称 * @return 服务器端返回的输入流,供客户端读取 * @throws Exception */ private static InputStream getSoapInputStream(String city) throws Exception { try { String soap = getSoapRequest(city); if (soap == null) { return null; } URL url = new URL( "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"); URLConnection conn = url.openConnection(); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", Integer.toString(soap .length())); conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); conn.setRequestProperty("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName"); OutputStream os = conn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8"); osw.write(soap); osw.flush(); osw.close(); InputStream is = conn.getInputStream(); return is; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 对服务器端返回的XML进行解析 * * * @param city * 用户输入的城市名称 * @return 字符串 用,分割 */ public static String getCityWeather(String city) { try { Document doc; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); InputStream is = getSoapInputStream(city); doc = db.parse(is); NodeList nl = doc.getElementsByTagName("string"); StringBuffer sb = new StringBuffer(); for (int count = 0; count < nl.getLength(); count++) { Node n = nl.item(count); if(n.getFirstChild().getNodeValue().equals("查询结果为空!")) { sb = new StringBuffer("#") ; break ; } sb.append(n.getFirstChild().getNodeValue() + "#/n"); } is.close(); return sb.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } }
第二个文件是SendMsg.java,代码如下:
import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Date; import java.util.Iterator; import java.util.Properties; import java.util.Set; public class SendMsg { Process pro=null; static boolean start=false; String getMsg(String city){ StringBuffer msg=new StringBuffer(); String[] infos=new String[100]; String info=getWeather.getCityWeather(city); if(info.equals("#")) return msg.append("该城市暂不被支持天气预报服务!").toString(); else infos = info.split("#") ; //msg.append("72小时天气预报 "); msg.append(infos[0]+infos[1]);//城市名称 msg.append(infos[4]+"发布");//发布时间 msg.append(infos[6]);//日期 msg.append(infos[5]+infos[7]);//天气细则 msg.append(infos[13]);//第二天的时间 msg.append(infos[12]+infos[14]);//第二天的天气细则 msg.append(infos[18]);//第三天的时间 msg.append(infos[17]+infos[19]);//第三天的天气细则 return msg.toString(); } public void Send(String toPhoneNos,String city) throws IOException, InterruptedException{ if (!start){ pro=Runtime.getRuntime().exec("cmd.exe /c start fetion.bat"); start=true; Thread.sleep(60000); } DatagramSocket datagramsocket = new DatagramSocket(); String[] toPhoneNo=new String[100]; //String str="sms 13406956704 回短信呀"; // 格式:sms 手机号 短信内容. 手机号为0时代表自己 String message=getMsg(city); toPhoneNo=toPhoneNos.split(","); for(int i=0;i<toPhoneNo.length;i++){ String str="sms "+toPhoneNo[i]+" "+message; InetAddress ia=InetAddress.getByName("127.0.0.1"); DatagramPacket p=new DatagramPacket(str.getBytes(), str.getBytes().length, ia, Integer.parseInt("40000")); datagramsocket.send(p); System.out.println("has send to "+toPhoneNo[i]+" "+message); System.out.println("*************************************************************************"); Thread.sleep(10000); } datagramsocket.disconnect(); datagramsocket.close(); } public void DestroyProcess() throws IOException, InterruptedException{ Thread.sleep(10000); Runtime.getRuntime().exec("cmd.exe /c tskill fetion"); Runtime.getRuntime().exec("cmd.exe /c tskill cmd"); } public void sendall() throws UnsupportedEncodingException, IOException, InterruptedException{ InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("PhoneCfg.properties"); Properties p = new Properties(); try { p.load(inputStream); } catch (IOException e1) { e1.printStackTrace(); } Set set=p.keySet(); Iterator it=set.iterator(); while(it.hasNext()){ String city=it.next().toString(); System.out.println(p.getProperty(city).toString()+ city); this.Send(p.getProperty(city).toString(), city); } this.DestroyProcess(); } /** * @param args */ public static void main(String[] args) throws Exception{ //int hours; //while(true){ //hours=new Date().getHours(); //System.out.println(hours); //if(14==hours){ SendMsg s=new SendMsg(); s.sendall(); //} //Thread.currentThread().sleep(1800000); //} } }
第三个文件是fetion.bat,用于自动登录飞信,其中-u后面的是你自己的手机号码,-p后面是你登录飞信的密码,代码如下:
cd C:/fetion fetion.exe -u 1363650**** -p ******
第四个文件是SendWeather.bat,代码如下:
java SendMsg pause
第五个文件是PhoneCfg.properties,里面包含了你要发送天气预报的城市和手机号码,格式为城市名称=手机号码,手机号码....,其中城市名称要用一个工具转换,方法为进入命令行,输入native2ascii,然后回车,输入城市名称,就可以得到这个城市的unicode,如果需要增加城市和手机号码,只需新增一行即可,实例如下:
//shanghai /u4e0a/u6d77=709346364,15121034241,13636502895,13774481009,13601841630,15001706169,13701978976,13764184996,13916711579,13611652753 //shangqiu /u5546/u4e18=13781577581,15093672105 //qingdao /u9752/u5c9b=15064251802,15020096359,15963009179 //shijiazhuang /u77f3/u5bb6/u5e84=13513379342 //jiaozuo /u7126/u4f5c=13569127147
4.最后的工作,将两个java文件编译一下,设置一个任务计划,目标文件为SendWeather.bat,时间是你想要发送的时间,完成!
ps:这个工具我已经运行了几个月,效果很好!
相关文章推荐
- 完全免费的手机短信天气预报与免费发送祝福短信(谷歌短信服务)
- 完全免费的手机短信天气预报与免费发送祝福短信(谷歌短信服务)
- httpd服务停止自动发送告警邮件
- 在SQL Server 集成服务中自动获取SharePoint的PowerPivot报表的图表并生成PDF及发送邮件的操作!
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- 免费超大量邮件发送服务Amazon SES和Mailgun提供SMTP和API支持
- 开机自动启动服务并定时发送请求
- 在SQL Server 集成服务中自动获取SharePoint的PowerPivot报表的图表并生成PDF及发送邮件的操作!
- 免费天气预报代码 滚动天气预报代码 大全
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- Google日历中可以导入外部的ical数据实现免费发送天气预报
- 移动飞信WEB发送服务接口
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- git 远程版本库,github提供服务原理,git自动更新发送邮件
- “软件免费,服务收费”此路不通?
- 移动设备上载文件的免费服务
- git 远程版本库,github提供服务原理,git自动更新发送邮件