繁体   English   中英

BufferedReader/Printwriter readLine 切断字符串的开头

[英]BufferedReader/Printwriter readLine cutting off beginning of a string

我正在尝试使用 java 创建一个服务器,当我从 BufferedReader (或打印写入器,我不知道是哪一个导致此问题)获得 output 时,它会切断它应该返回的内容的开头。 这是类似于我写的代码(很多都被遗漏了,这非常简化):


   class Server {

      //Server has similar functions to the client
      public void when(String header, myinterface do) throws Exception {
          String data = mybufferedreader.readLine();
          System.out.println(data); // this shows that the data is cutting off
          if(data!=null) {
              JSONObject jsondata = new JSONObject(data);
              String head = jsondata.getString("header");
              Object body = jsondata.get("body");
              if(header.equals(head)) { do.RUN(body) }
          }
      } 
      public void sendToClient(String header, Object body) throws Exception {
           JSONObject emission = new JSONObject();
           emission.put("header", header);
           emission.put("body",   body);
           myPrintWriter.println(emission.toString());
           myPrintWriter.flush();
      }
       
   }

   public class Main() {
      public static void main(String[] args) throws Exception {
           Server server = new Server(/*ip*/, /*port*/);

           while (true) {
              server.when("thingy", body -> {
                 //this makes an error saying JSON has to start with a "{"
              });
           }

           //this would be in a different script/jar, just adding it here
           Client client = new Client(/*ip*/, /*port*/);
           client.sendToServer("thingy", "this is my json body");
            
      }
   }

我知道如何解决这个问题,它会在控制台上打印类似'eader":"thingy", "body": "this is my json"}'的内容。(这主要发生在“body” object 较长时,或者真的很长)

我正在尝试使用 java 创建一个服务器,当我从 BufferedReader (或打印写入器,我不知道是哪一个导致此问题)获得 output 时,它会切断它应该返回的内容的开头。 这是类似于我写的代码(很多都被遗漏了,这非常简化):


   class Server {

      //Server has similar functions to the client
      public void when(String header, myinterface do) throws Exception {
          String data = mybufferedreader.readLine();
          System.out.println(data); // this shows that the data is cutting off
          if(data!=null) {
              JSONObject jsondata = new JSONObject(data);
              String head = jsondata.getString("header");
              Object body = jsondata.get("body");
              if(header.equals(head)) { do.RUN(body) }
          }
      } 
      public void sendToClient(String header, Object body) throws Exception {
           JSONObject emission = new JSONObject();
           emission.put("header", header);
           emission.put("body",   body);
           myPrintWriter.println(emission.toString());
           myPrintWriter.flush();
      }
       
   }

   public class Main() {
      public static void main(String[] args) throws Exception {
           Server server = new Server(/*ip*/, /*port*/);

           while (true) {
              server.when("thingy", body -> {
                 //this makes an error saying JSON has to start with a "{"
              });
           }

           //this would be in a different script/jar, just adding it here
           Client client = new Client(/*ip*/, /*port*/);
           client.sendToServer("thingy", "this is my json body");
            
      }
   }

我知道如何解决这个问题,它会在控制台上打印类似'eader":"thingy", "body": "this is my json"}'的内容。(这主要发生在“body” object 较长时,或者真的很长)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM