最近做项目,要用到spring的定时器。
在某个时间,要系统自动的对积分进行结算。在执行方法的过程中,需要用到从页面获取到得一个数据(或者从一个js接口获取到得数据)~~!
定时器执行的时候,和页面没关系呀~~~
想不通。该怎么获取~!
http://www.hkfilmart.com/weeklyboxoffice.asp? --这是接口
lang=en&wbid=494 --这是参数列表
上面也可以写为:String result = PostUtil.post("http://www.hkfilmart.com/weeklyboxoffice.asp?","lang=en&wbid=494");
像lang, wbid就是参数,en,494就是参数对应的值.
如果参数值为中文,可能需要encode,Server才能识别。
你从页面通过ajax请求发送数据到后台action中,再action把数据传给定时器不就行了
通过HttpURLConnection从一个url获取数据,这个url可能是一个js file.
例如: http://example.com/xx.js; 获取到数据后,再解析。 然后把解析后的数据传给
Spring定时任务。
Ajax请求定时发送数据,可以在spring的定时器之前几秒。
public class PostUtil {
/*
* used for geting data from server or posting data to server.
*/
public static String post(String uri,String data){
String result = "";
try {
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CommonDefn.URL_CONNECTION_TIMEOUT);
connection.setReadTimeout(CommonDefn.URL_READ_TIMEOUT);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
//encode the postdata
String postdata = data;
out.print(postdata);
out.close();
BufferedReader in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
in.close();
}
catch(Exception e){
e.printStackTrace();
result = "-1"; //may be there's some error happened....
}
return result;
}
}
[code="java"]////////////////////////////////////////////////////////////////////////////////
// COPYRIGHT (C) 2008 TELECOM DIGITAL MEDIA ("TDMEDIA").
// ALL RIGHTS RESERVED.
//
// THIS IS CONFIDENTIAL AND PROPRIETARY INTELLECTUAL PROPERTY OWNED BY AND
// CREATED ON BEHALF OF TDMEDIA. ANY FORM OF DISTRIBUTION, COPY,
// MODIFICATION WITHOUT THE WRITTEN CONSENT FROM TDMEDIA IS STRICTLY
// PROHIBITED.
////////////////////////////////////////////////////////////////////////////////
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/*
Responsibility for "get","post" data.
*/
public class PostUtil {
/*
used for geting data from server or posting data to server.
*/
public static String post(String uri,String data){
String result = "";
try {
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(30000);
connection.setReadTimeout(30000);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
//encode the postdata
String postdata = data;
out.print(postdata);
out.close();
BufferedReader in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
in.close();
}
catch(Exception e){
e.printStackTrace();
result = "-1"; //may be there's some error happened....
}
return result;
}
public static void main(String[] args) {
//get data from : http://www.hkfilmart.com/weeklyboxoffice.asp?lang=en&wbid=494
String result = PostUtil.post("http://www.hkfilmart.com/weeklyboxoffice.asp?lang=en","wbid=494");
System.out.println(result);
}
}
[/code]
in, out是相对的。一个File /Stream可以作为另一个File/Stream的input/output.
Linux的Pipe就是这样的。 那个循环是用来按行读取数据,直到结束。