|
楼主 |
发表于 2018-3-1 15:53:40
|
显示全部楼层
本帖最后由 bxj975 于 2018-3-1 17:46 编辑
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONArray;
//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.action.ActionContext;
public class FetchExpend {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成的方法存根
try {
FetchExpend restUtil = new FetchExpend();
SqlExpend sqlexpend = new SqlExpend();
String resultString = restUtil.load("http://****:92/api/logistics/finance/getOrdersFeeRecord", "beginDate=2018-01-20&endDate=2018-01-21");
JSONObject jsonObject = JSONObject.parseObject(resultString);
String resp = jsonObject.getString("resp");
JSONObject json = JSONObject.parseObject(resp);
JSONArray photoArray = json.getJSONArray("rows");
System.out.print(photoArray.size());
for (int i = 0; i < photoArray.size(); i++) {
JSONObject object = (JSONObject) photoArray.get(i);
Expenditure expend = new Expenditure();
expend.setOrderCode(object.getString("orderCode"));
expend.setFee_name(object.getString("fee_name"));
expend.setFee(object.getDouble("fee"));
// expend.setCreate_time(sqlexpend.convert(object.getString("create_time")));
expend.setCreate_time(object.getString("create_time"));
expend.setIncome(object.getDouble("income"));
expend.setSiteName(object.getString("siteName"));
expend.setAddress(object.getString("address"));
// System.out.print(expend.getAddress());
int result = sqlexpend.insert(expend);
System.out.print(result);
}
} catch (Exception e) {
// TODO: handle exception
System.out.print(e.getMessage());
}
}
private static JSONObject ret = new JSONObject();
public static JSONObject fetchExp(JSONObject params, ActionContext context) throws Exception {
// 前后端调用传参的时候,js对中文字符串的编码处理:
// var name = encodeURI(‘内容’);
// java接收参数以后,转码为utf-8
// URLEncoder.encode(request.getParameter(“name”),”utf-8″);
String dateBegin = URLEncoder.encode(params.getString("dateB"),"utf-8");
String dateEnd = URLEncoder.encode(params.getString("dateE"),"utf-8");
// System.out.print(dateBegin,dateEnd);
int num = 0;
FetchExpend restUtil = new FetchExpend();
SqlExpend sqlexpend = new SqlExpend();
String resultString = restUtil.load("http://****:92/api/logistics/finance/getOrdersFeeRecord", "beginDate=2018-01-20&endDate=2018-01-21");//"beginDate=" + dateBegin + "&endDate=" + dateEnd);
System.out.print(resultString);
JSONObject jsonObject = JSONObject.parseObject(resultString);
String resp = jsonObject.getString("resp");
JSONObject json = JSONObject.parseObject(resp);
JSONArray photoArray = json.getJSONArray("rows");
System.out.print(photoArray.size());
for (int i = 0; i < photoArray.size(); i++) {
JSONObject object = (JSONObject) photoArray.get(i);
Expenditure expend = new Expenditure();
expend.setOrderCode(object.getString("orderCode"));
expend.setFee_name(object.getString("fee_name"));
expend.setFee(object.getDouble("fee"));
// expend.setCreate_time(sqlexpend.convert(object.getString("create_time")));
expend.setCreate_time(object.getString("create_time"));
expend.setIncome(object.getDouble("income"));
expend.setSiteName(object.getString("siteName"));
expend.setAddress(object.getString("address"));
int result = sqlexpend.insert(expend);
System.out.print(object.getString("siteName"));
num += result;
}
ret.put("result", num); // String.valueOf(n));
return ret;
}
public String load(String url, String query) throws Exception {
URL restURL = new URL(url);
/*
* 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类
* 的子类HttpURLConnection
*/
HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
// 请求方式
conn.setRequestMethod("POST");
// 设置是否从httpUrlConnection读入,默认情况下是true;
// httpUrlConnection.setDoInput(true);
conn.setDoOutput(true);
// allowUserInteraction 如果为 true,则在允许用户交互(例如弹出一个验证对话框)的上下文中对此 URL 进行检查。
conn.setAllowUserInteraction(false);
PrintStream ps = new PrintStream(conn.getOutputStream());
ps.print(query);
ps.close();
BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line, resultStr = "";
while (null != (line = bReader.readLine())) {
resultStr += line;
}
// System.out.println("3412412---" + resultStr);
bReader.close();
// System.out.println(resultStr);
return resultStr;
}
}
|
|