Hello, I ran into a problem in my application. There is a server using ServerSocket. In which there is a small UI with three JTextField and JButton. In which I fill in the information and on pressing the button I send information to the client, which is written under Andoid. The problem is the following, I do not understand how threads work in Android while the application is running. And as soon as I send the message to the client, textView immediately doesn’t fill it. The application starts to crash and as soon as I poke into some widget, it sort of decomposes and only then displays the message on the textView. As soon as I send something from the server, the following message is displayed in the log of Android Studio
Only this thread has been created.
Suppose I have an "send" Button on the activity and an EditText. So after I send a message to the client from the server, I open it and put the focus on EditText by clicking on it, it starts to lag as soon as I click on it to open the application completely and the textView server. Also, when re-sending a message from the server, the textView is not updated, perhaps the reason is that the code is in the onCreate () function;
What could be the problem and how to deal with it?
public class Server{ private static BufferedWriter bufferedWriter; JFrame frame; JButton sendButton; JPanel panel; JTextField nameTextField, serviceTextField, percentTextField; public Server(){ frame = new JFrame("AutoRate discount"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(410,100); frame.setVisible(true); frame.setLayout(new BorderLayout()); sendButton = new JButton("Send discount"); nameTextField = new JTextField(10); serviceTextField = new JTextField(10); percentTextField = new JTextField(10); panel = new JPanel(); panel.add(nameTextField); panel.add(serviceTextField); panel.add(percentTextField); panel.add(sendButton); frame.getContentPane().add(BorderLayout.CENTER,panel); frame.setLocationRelativeTo(null); sendButton.addActionListener(new MyListener()); } private static final int portNumber = 60123; public static void main(String[] args) { Server server = new Server(); ServerSocket serverSocket = null; try{ System.out.println("Server started at port number " + portNumber); serverSocket = new ServerSocket(portNumber); // Waiting for client connection System.out.println("Waiting for client connection"); Socket socket = serverSocket.accept(); System.out.println("A client has connected"); // Send message to the client bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); //String s = server.nameTextField.getText() + server.serviceTextField.getText() + server.percentTextField; //bufferedWriter.write(s); // Receive message from client String data; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while((data = bufferedReader.readLine()) != null) System.out.println("Message from client " + data); System.out.println("Server has ended"); }catch(IOException e){ e.printStackTrace(); } } //Обработчик нажатия кнопки private class MyListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String s = nameTextField.getText() + serviceTextField.getText() + percentTextField.getText(); try { bufferedWriter.write(s); bufferedWriter.newLine(); bufferedWriter.flush(); } catch (IOException e1) { e1.printStackTrace(); } } }
}
Client Code:
public class MainActivity extends Activity { Socket socket = null; public String debuggingString = "DEBUG"; // We should use here Ethernet adapter VMware Network Adapter VMnet1: IpV4 public String hostname = "192.168.253.1"; // <-- paste your IPv4 here public int portNumber = 60123; TextView textView; BufferedReader bufferedReader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread() { @Override public void run() { try { //connecting Log.e(debuggingString, "Attempting to connect to server"); socket = new Socket(hostname, portNumber); //Send message to server BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); bw.write("this is a message from the client"); bw.newLine(); bw.flush(); //Receive message from server bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); textView = (TextView)findViewById(R.id.textView); textView.setText(bufferedReader.readLine()); } catch ( Exception e ) { Log.e(debuggingString, e.getMessage()); } } }.start(); }