Java做接口与安卓的对接

public String CarrierQueryPort(){
       HttpServletRequest request=this.getRequest();
         //此处必须进行数据压缩
       ByteArrayOutputStream outStream = new ByteArrayOutputStream();
       GZIPOutputStream gzipOut = null;
       HttpServletResponse response = this.getResponse();
       JSONArray jsonArray=new JSONArray();
       JSONObject jsonObj = null;
       BufferedReader br = null;
       //用来接收拼装完成的json
       String appMsg = "";

        //查询条件
        //安卓端传过来的json中的key值
        String carrierOutNo = "";
        String unitName = "";
        String exportCode = "";
        Date qStartDate = null;
        Date qEndDate = null;
        int startIndex = 0;
        int pageSize = 0;

        //求符合条件的合计数
        String msgTotalNum = "";

        try{
            gzipOut = new GZIPOutputStream(outStream);

            //接收json
            //采用流的方式取出json数据,并将读出来的字符串拼装成json
            br=new BufferedReader(
                    new InputStreamReader((ServletInputStream)request.getInputStream()) );
            String line=null;
            StringBuffer sb=new StringBuffer();
            while( (line=br.readLine())!=null ){
                sb.append(line);
            }
            appMsg=sb.toString();

            try{
                //如果传递过来的数据有异常,如乱码导致的双引号丢失造成无法正常拼装成json数据做的异常处理
                jsonObj=JSONObject.fromObject(appMsg);  
            }catch(Exception e){
                gzipOut.write(0);
                gzipOut.close();
                return NONE;
            }


            //获取查询条件值
            startIndex=jsonObj.getInt("startIndex");
            pageSize=jsonObj.getInt("endIndex")-startIndex+1;
            carrierOutNo=jsonObj.getString("carrierOutNo");
            exportCode=jsonObj.getString("exportCode");

            unitName=jsonObj.getString("unitName");
            unitName=new String(new String(unitName.getBytes(),"utf-8").getBytes(),"GBK");  

            //PublicFunction写的公共类strToDate方法                          
qStartDate=PublicFunction.getDate(jsonObj.getString("qStartDate"));
            qEndDate=PublicFunction.getDate(jsonObj.getString("qEndDate"));

            //放入query
            DetachedCriteria query=DetachedCriteria.forClass(ExportCarrier.class);
            this.addParameters(query, carrierOutNo, unitName, 
                exportCode,qStartDate,qEndDate);

            //符合条件json数据,具体实现代码不贴出
            jsonArray = JSONArray.fromObject(carrierService.getCarrierTree(query,pageSize,startIndex));


            query=DetachedCriteria.forClass(ExportCarrier.class);
            this.addParameters(query, carrierOutNo, unitName, 
                    exportCode,qStartDate,qEndDate);

            //符合记录数,具体实现代码不贴出           msgTotalNum=carrierService.getEntityCountByCriteria(query)+"";

            jsonObj.put("carrierList", jsonArray);
            jsonObj.put("msgTotalNum", msgTotalNum);
            jsonObj.remove("carrierOutNo");
            jsonObj.remove("exportCode");
            jsonObj.remove("unitName");
            System.out.println(jsonObj.toString());
            if(jsonObj!=null){
            //压缩传输出去
            gzipOut.write(jsonObj.toString().getBytes("utf-8"));
                gzipOut.close();

                byte[] temp = outStream.toByteArray();

                response.setHeader("Content-Encoding","gzip");
                response.setHeader("Content-Length", temp.length+"");
                response.getOutputStream().write(temp);
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        return NONE;
    }

你可能感兴趣的:(Java接口)