1、数据接收接口:
这个可以考虑最简单的Servlet方法,而且效率较高;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;import java.util.Date;import java.util.Enumeration;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.google.gson.Gson;
import com.hgh.receive.Receive;import com.hgh.receive.ReturnJson;public class kdniaoLogistics2 extends HttpServlet{
private Gson gson=new Gson(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Enumeration<String> list = req.getParameterNames(); //这个主要是为了从一个数据结构得到连续数据
Receive re = null; while (list.hasMoreElements()) { String str = (String) list.nextElement(); //获取参数名称 String str1=req.getParameter(str); //获取对应参数名称的内容 f(str.equals("RequestData")){ //此处是判断参数名称 re = gson.fromJson(str1, Receive.class); //gson字符串转为对象;这个类非常的好用,强力推荐 LinkQueue.getLinkQueue().add(re); //这两个都是对参数和参数名的操作,我是采用队列形式存储参数,解决对方传参数量过大,且速度快导致系统接收不全 //这个只需要对象的操作,如需队列相关的操作请见下一博客;}
} SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); PrintWriter fs = resp.getWriter(); //准备返回接收信息 ReturnJson returnjson; //返回参数对象,按API文档定义 if(re!=null){ //判断接收到的信息转为对象后有没有值,或者有没有正常的接收到数据 returnjson = new ReturnJson("1282148",sdf.format(new Date()), "true", ""); System.out.println(gson.toJson(returnjson));//将对象转成JSON输出 }else{ returnjson = new ReturnJson("1282148",sdf.format(new Date()), "false", "未能有效接收到数据"); System.out.println(gson.toJson(returnjson)); } fs.write(gson.toJson(returnjson)); //直接返回信息 }}