2014-02-07 3 views
0

기본적으로 나는 양면 간의 소켓 연결을하고 있습니다. 자바 서버와 안드로이드 클라이언트. Gson 라이브러리를 사용하여 String Json으로 객체를 변환하고 Android 클라이언트의 WriteUTF 메소드를 통해 객체를 전송합니다. 하지만 자바 서버에서 응답을 얻으려고하면 java.io.DataInputStream.readUnsignedShort (알 수없는 소스)에서 "java.io.EOFException"을 얻습니다. 어떻게,이 메서드를 사용하지 않았거나 (readUnsignedShort) Short를 보내지 않았다면? 여기에 코드가 있습니다.WriteUTF 및 ReadUTF를 사용하면 문자열을 작성하고 다른 쪽에서 짧은 예외가 생길 수 있습니까?

try { 
       DataInputStream in = new DataInputStream (s3.getInputStream()); 
       DataOutputStream out = new DataOutputStream(s3.getOutputStream()); 

       ap = (ArrayList<Produto>) InternalStorage.readObject(getBaseContext(),"pedido"); 

       for(int i=0;i<ap.size()&& ap.size()>0;i++){ 

         Produto p = new Produto("", "", 0f, "", "", 0); 
         p = ap.get(i); 
         Gson gson = new Gson(); 
         String json = gson.toJson(p); 
         out.writeUTF(json); 
         out.flush(); 

        } 

그리고 자바 서버에서 읽기 : 안드로이드 클라이언트에서 작성

try { 
     // Get input from the client 
     DataInputStream in = new DataInputStream (server.getInputStream()); 
     DataOutputStream out = new DataOutputStream(server.getOutputStream()); 


     while((line = in.readUTF()) != null) { 
      boolean error = false; 

      String json = line; 
      Gson gson = new Gson(); 
      Produto p2 = gson.fromJson(json, Produto.class); 
      c = dbDelegate.getCommandByNumber(p2.getMesa()); 
      p = dbDelegate.getProductByName(p2.getNome()); 
      if(c!=null && p!=null){ 
       error = false; 
      inCommand.setCommand(c); 
      inCommand.setDateAndTime(new Timestamp(System.currentTimeMillis())); 
      inCommand.setProduct(p); 
      inCommand.setLogin(FishingBoardFrameCashRegister.getInstance().getLogin()); 
      inCommand.setQuantity(p2.getQuantidade()); 
      inCommand.setPrice(String.valueOf((p2.getValor()*p2.getQuantidade()))); 
      inCommand.setObs(p2.getObs()); 
      inCommand.setProductName(p.getName()); 
      inCommand.setType(Product.ALL); 
      inCommand.setSession(0); 
      inCommand.setCommandSession(c.getSession()); 
      if(dbDelegate.insertProductInCommand(inCommand) == DbDelegate.CASH_CLOSED){ 
       error = true; 
      } 
      FishingBoardFrameCashRegister.getInstance().updateTable(); 
      }else{ 
       error = true; 
      } 

로그가 너무 도움이 될 수는 :

trueIOException on socket listen: java.io.EOFException 
java.io.EOFException 
at java.io.DataInputStream.readUnsignedShort(Unknown Source) 
at java.io.DataInputStream.readUTF(Unknown Source) 
at com.keymax.server.doComms3.run(Server3.java:71) 

Server3.java:71이 라인 서버 코드 : while ((line = in.readUTF())! = null) {

답변

0

readUTF으로 전화하면 아무 데이터도 남지 않기 때문에 여기에 EOFException이 필요하다고 생각합니다.

실제로 문자열의 수보다 readUTF 번 이상 호출하지 않도록 내가, 다른 쪽 끝에서 다음 readInt를 작성하려고 얼마나 많은 문자열 밖으로 쓰기 루프 전에 writeInt를 사용하여이 문제를 해결하고, 것입니다 스트림에서 사용할 수 있습니다.

writeUTF은 문자열의 길이와 문자열의 데이터를 씁니다. 이유는 readUnsignedShortEOFException 인 이유를 설명합니다.

+0

포인트를 얻었습니다. Excelent 솔루션! 나는 여기에 내 결과를 게시하는 것보다 그것을 시도 할게요. 감사 ! –