Android-Client se bloquea al conectarse a Java-Server

i have watched a tutorial, and with the code of this, my phone phone chrashes every time it trys to send the text.

Cliente:

    textField = (EditText) findViewById(R.id.editText1);
    button = (Button) findViewById(R.id.button1);   

    button.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        messsage = textField.getText().toString(); 
        textField.setText("");   

        try {

         client = new Socket(InetAddress.getByName("jarves-server.no-ip.info"), 8000);  
         printwriter = new PrintWriter(client.getOutputStream(),true);
         printwriter.write(messsage);  

         printwriter.flush();
         printwriter.close();
         client.close();   

        } catch (UnknownHostException e) {
         e.printStackTrace();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      });

Servidor:

public static void main(String[] args) {
    try {
        serverSocket = new ServerSocket(8000);

    } catch (IOException e) {
        System.out.println("Could not listen on port");
    }

    System.out.println("Server started.");

    while (true) {
        try {

            clientSocket = serverSocket.accept();   
            inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader); 
            message = bufferedReader.readLine();

            System.out.println(message);
            inputStreamReader.close();
            clientSocket.close();

        } catch (IOException ex) {
        }
    }
}

I really don`t know what is wrong with the code. Maybe it could be also the DynDns i have set up with No-IP and our Speedport W723V and a Port forwarding on Port 8000 to my PC where the Server is running.

Hope for help!

preguntado el 28 de mayo de 14 a las 13:05

1 Respuestas

If you had looked in the LogCat you would have seen a NetworkOnMainThreadException. You have to place your socket code in an AsyncTask or Thread.

contestado el 28 de mayo de 14 a las 17:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.