Java如何通过Socket传递JSON对象

时间:2018-02-14 22:00:57

标签: java android json sockets

我有一个Java ServerSocket(现在从键盘输入)。我试图将其转换为JSON,将其传递到套接字并在Android应用程序中读取它。最终我将获得真实的消息。问题是,当我运行应用程序时,代码似乎挂在in.readLine()中的客户端; 不会抛出任何异常,但永远不会触发in.readLine之后的打印消息。 我不知道如何通过套接字传递JSON,并在客户端获取它。

服务器:

public void run(){
        JSONObject json = new JSONObject();

        try{
            while(true){
                System.out.println("Please enter your input: ");
                String input = scanner.nextLine();

                out = new OutputStreamWriter(this.socket.getOutputStream(), StandardCharsets.UTF_8);

                System.out.println("Input was : " + input);
                json.put("message", input);
                json.put("message2", "A second message");                

                System.out.println(json.toString());
                out.write(json.toString());
                Thread.sleep(3000);
            }



        } catch (IOException e) {
            e.printStackTrace();
        }catch (InterruptedException e)
        {
            System.out.println(e.getMessage());
        }
    }

客户端(Android)

public void run() {

                        Log.i("CHCI_FRAGMENT", "run of ChciInnerMessageLoop");
                        // create a new Client Socket.
                        try {
                            //10.0.2.2 local machine address
                            // sluggo IP:  140.102.98.76
                            // my IP 10.4.31.254
                            socket = new Socket(serverAddress, serverPort);
                            in = new BufferedReader(
                                    new InputStreamReader(socket.getInputStream()));
                            PrintWriter out =
                                    new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

                            while(!"end".equals(socketMessage)) {
                                Log.i("CHCI_FRAGMENT", "LIne 213 ");
                                socketMessage = in.readLine();
                                Log.i("CHCI_FRAGMENT", "LIne 215 ");
                                jsonObject = new JSONObject(socketMessage);
                                Log.i("CHCI_FRAGMENT", "LIne 217");
                                runOnUiThread(new Thread(new Runnable() {
                                    public void run() {
                                     //   routeMessage(socketMessage);
                                        routeMessage(jsonObject);
                                    }
                                }));
                            }
                        } catch (IOException e) {
                            //TODO
                            Log.i("CHCI_FRAGMENT", "Exception " + e.getMessage());
                        }
                        catch (JSONException e){
                            Log.i("CHCI_FRAGMENT", "JSON Exception " + e.getMessage());
                        }catch (Exception e)
                        {
                            Log.i("CHCI_FRAGMENT", "JSON Exception " + e.getMessage());
                        }

                    }
                });

1 个答案:

答案 0 :(得分:0)

像往常一样。

你正在读行,但你不是在写行。

使用PrintWriter,将out.write(...)更改为out.println(...)

每次循环都不要创建新的PrintWriter