【70分!急!】代码并未往下执行!
首先上代码。
- 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; }
这个函数是AsyncTask的函数,大家都懂的,主要功能是与服务器连接并接受数据和解析数据。
问题来了,在那个while循环(只循环一次,因为从服务器接收的数据只有40byte)后的代码就不执行,这时我看了logcat有显示:request time failed: java.net.SocketException: Address family not supported by protocol。
这是为什么呢?我都没中断与服务器的连接,何况我接收完了数据,应该往下执行解析数据的啊,为什么不往下执行了呢?
求大神们帮忙看看我的代码,小弟在此谢了!毕设的,紧啊!只要能解决问题,我马上给分,只有70分了。
[解决办法]
是你服务端问题吧?
[解决办法]
检查一下e1.printStackTrace();和e.printStackTrace();这2个地方有没有输出错误日志。