最近我完成了我的第一个客户端-服务器项目。它实际上很酷,但我有一个小问题。没有消息,但我想要带有完整名称和密码字段的 JSON。我正在将GSONjar 库用于JsonObject.
我的服务器
public class Server extends Thread {
private static final int port = 8607;
protected static String server_IP;
public static void main(String[] args) {
try {
InetAddress iAddress = InetAddress.getLocalHost();
server_IP = iAddress.getHostAddress();
System.out.println("Server IP : " + server_IP);
} catch (UnknownHostException e){
}
try {
//Server
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server has started");
//Accepting client
System.out.println("Waiting");
Socket socket = serverSocket.accept();
System.out.println("Client has been added");
//Send message to client
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write("Connection accepted");
bw.newLine();
bw.flush();
//receive json from client
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String json_string = br.readLine();
JsonParser jsonParser = new JsonParser();
JsonObject user = jsonParser.parse(json_string).getAsJsonObject();
System.out.println("JSON: " +user.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
我的服务器控制台
Server IP : 192.168.123.2
Server has started
Waiting
Client has been added
JSON: {"name":""}
Process finished with exit code 0
慕盖茨4494581
相关分类