传出数据的类DataTransIn:其中 syncDataToServer()用来接收传入数据,具体代买解释如下
/** * 把byte数据传到服务器的具体传送部分 * @param b 数据字节数组 */ public boolean transDate(byte b[]){ HttpPost request = new HttpPost(StaticFinalVariable.URL_Str); ByteArrayEntity byteArrayEntity = new ByteArrayEntity(b); boolean isOk=false; try { request.setEntity(byteArrayEntity); HttpResponse response = new DefaultHttpClient().execute(request); Log.e("error",response.getStatusLine()+""); if(response.getStatusLine().getStatusCode()==200){ isOk=true; String msg = EntityUtils.toString(response.getEntity()); Log.e("error",msg); } } catch (Exception e) { e.printStackTrace(); } return isOk; } /** * 把数据传到服务器 * @param treeMap 数据map * @param id 四位整数的id号 * @param isZip 0表示不压缩,其他的表示压缩 */ public boolean syncDataToServer(TreeMap<String, Integer> treeMap,String id,boolean gzip) throws DigitException{ InDataTobyteArr(treeMap); addIdToArr(id); // compressCon(gzip); return transDate(); } /** * 自定义压缩下届 * @param treeMap * @param minlength 压缩下届 * @throws DigitException */ public void syncDataToServer(TreeMap<String, Integer> treeMap,String id,long minlength) throws DigitException{ if(sensorDataBytes.getSumByte()>minlength){ syncDataToServer(treeMap, id,true); } else{ syncDataToServer(treeMap,id,false); } } /** * 按照系统自定义的量去决定是否压缩 * @param treeMap * @throws DigitException */ public void syncDataToServer(TreeMap<String, Integer> treeMap,String id) throws DigitException{ if(sensorDataBytes.getSumByte()>StaticFinalVariable.MIN_LENGTH){ syncDataToServer(treeMap,id, true); } else{ syncDataToServer(treeMap, id,false); } }
服务器接收数据类DataTransOut:
其中用runResult()解析ServletInputStream,printMap()打印传感器数据map ,getId(),getSensorData(String) 获取相应的数据
代码如下:
/** * 将整个request 的流全部解析出来。 * @param request 传入的HttpServletRequest 参数 * @throws IOException */ public void runResult(HttpServletRequest request) throws IOException { readByteArray(request.getInputStream()); readId(StaticFinalVariable.ID_BYTES); outDataFrombyteArr(StaticFinalVariable.ID_BYTES); } /** * 供测试用,打印传感器数据的map; */ public void printMap(){ System.out.println(treeMap); } /** * 获取name 传感器的数值 * @param name 表示传感器名称 * @return */ public int getSensorData(String name){ return treeMap.get(name); } /** * 获取传入的id * @return */ public String getId(){ return String.valueOf(id); }
使用的范例代码:
button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub TreeMap<String, Integer> treeMap =new TreeMap<String, Integer> (); treeMap.put("hello", 1); treeMap.put("acceleration1",100); treeMap.put("temperature",2000); treeMap.put("acceleration2", 10000); treeMap.put("acceleration3",65535 ); try { Log.e("treeMap:",treeMap+""); if(trans.syncDataToServer(treeMap,id,false)){ toastShow("发送成功"); } else{ toastShow("发送失败"); } } catch (DigitException e) { // TODO Auto-generated catch block Toast.makeText(MainActivity.this,e.getMessage(), Toast.LENGTH_SHORT); e.printStackTrace(); } Log.e("error", "testEntity after test"); } });
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * 转换类 */ private DataTransOut trans=new DataTransOut(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); PrintWriter out = response.getWriter(); trans.runResult(request); trans.printMap(); System.out.println(trans.getId()); trans.getSensorData("acceleration1"); outputMap(); String msg ="hello"; out.print(msg); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); }