首先上代码。
- Java code
protected String doInBackground(String... arg0) { // TODO Auto-generated method stub String TempString = null; try { System.out.println("I get 4 !"); socket = new Socket("192.168.170.108",6000); out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); TempString = getLocalMacAddress(); out.println("1"); out.flush(); ins = socket.getInputStream(); } catch (UnknownHostException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } byte[] temp = new byte[1024]; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); try{ int length; while((length=ins.read(temp))!= -1){ System.out.println("length is: "+length); buffer.write(temp, 0, length); System.out.println("I get 10 !"); } int Size = buffer.size(); System.out.println("Size is: "+Size); byte[] FileInfo = new byte[Size]; for(int c = 0; c < Size; ++c){ FileInfo[c] = 0; } System.out.println("I get 5 !"); FileInfo = buffer.toByteArray(); //number记录的为该医生的病人总数 int number = 0; for(int i = 0; i < FileInfo.length; ++i){ if(FileInfo[i] == 0){ System.out.println("FileInfo["+i+"] = "+FileInfo[i]); ++number; } } number = number/3; byte[] pID = null; byte[] pBedID = null; byte[] pName = null; //循环接收这三个病人信息 for(int j = 0; j < number; ++j){ //标记文件名称结束的末端 int endFlag = 0; //从文件大小Int后面开始检测文件名称结束标志 int z =0+j*30; while(FileInfo[z] != 0){ ++z; } System.out.println("I get 6 !"); endFlag = z; pID = new byte[z - (0+j*30)]; //获取病人号 char pID[10] System.arraycopy(FileInfo, 0+j*30, pID, 0, endFlag); z = 10+j*30; while(FileInfo[z] != 0){ ++z; } endFlag = z; pBedID = new byte[z - (10+j*30)]; //获取病人床号 char pBedID[10] System.arraycopy(FileInfo, 10+j*30, pID, 0, endFlag-10); z = 20+j*30; while(FileInfo[z] != 0){ ++z; } endFlag = z; pName = new byte[z - (20+j*30)]; //获取病人姓名char pName[20] System.arraycopy(FileInfo, 20+j*30, pID, 0, endFlag-20); String ID = new String(pID, "GBK"); String BedID = new String(pBedID, "GBK"); String Name = new String(pName, "GBK"); record.ID = ID; record.BedID = BedID; record.Name = Name; Message msg = listHandler.obtainMessage(); msg.obj = record; listHandler.sendMessage(msg); } out.close(); ins.close(); socket.close(); }catch(Exception e){ e.printStackTrace(); } return null; }