[学习笔记]外部调用本地接口返回JSON数据

package com.skyland.interactions.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import system.SysUserAccount;

import com.skyland.charge.ChargeItemDetail;
import com.skyland.dynajpa.DynaEntityManagerFactory;
import com.skyland.property.PropCustomer;
import com.skyland.utils.HttpInvoker;

/**
 * 信息同步接口
 * @author limiao
 *
 */
public class PropertyMessageInterface extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest req, HttpServletResponse rsp){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd HH:mm");
        String beginDate=req.getParameter("StartTime");
        String endDate=req.getParameter("EndTime");
        System.out.println("开始时间:"+beginDate);
        System.out.println("结束时间:"+endDate);
        Log log=LogFactory.getLog(PropertyMessageInterface.class);
        log.info("SynchronizationMessageInterfaceAction接口开始运行....");
        try {
            req.setCharacterEncoding("UTF-8");//设置请求编码
            rsp.setCharacterEncoding("UTF-8");//设置返回编码
            rsp.setContentType("text/html;charset=UTF-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        EntityManager jpa=DynaEntityManagerFactory.getInstance().createEntityManager();
        JSONObject json_res=new JSONObject();
        InteractionDataHelper helper = new InteractionDataHelper();
        String Code = "0";
        boolean IsSuccess=false;
        String Message="";
        try {    
            /*
             * 以下代码负责接口返回
             */
            //List<PropCustomer> customerlist = helper.findPropCustomerList(jpa,beginDate,endDate);
            try {
//                if (customerlist != null) {
//                    if (customerlist.size() > 0) {
//                     }
//                }    
                for (int i = 0; i < 5; i++) {
                    //PropCustomer customer=customerlist.get(i);
                    json_res.put("PropRegion_ID","3303");
                    json_res.put("PropBuilding_ID", "2205");
                    json_res.put("Floor", "10");
                    json_res.put("PropRoom_ID", "5713");
                    json_res.put("RoomNo", "5-324");
                    json_res.put("CustomerName", "孙小九("+i+")");
                    json_res.put("Telephone", "13061974393");
                    json_res.put("BuildArea", "60.4");
                    json_res.put("ReceiveTime", "2016-03-01");
                    PrintWriter out;
                    out = rsp.getWriter();
                    out.print(json_res.toString());// 返回消息
                }

            } catch (Exception e) {
                e.printStackTrace();
            }  
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(jpa.isOpen()){
                jpa.close();
            }
        }

        return null;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值