Amigos
Estou fazendo um cliente no android que vai acessar um server no desktop do java.
simples envio a string" teste" e o servidor responde “testado” então no cliente pego a resposta e imprimo na tela.
só que não funciona, o android envia a palavra “teste”, o servidor recebe, envia a resposta e o android trava nesta linha.
message = in.readUTF(); //**** trava aqui ****///
ja testei com BufferedReader + ReadLine, mas o resultado é o mesmo…
pesquisei por cliente socket para android os exemplos que eu vi somente enviam dados, mas não recebem…
por isso a pergunta acima é diferente o tratamento de sockets no android ??
segue o meu código:
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private TextView textView;
private Button button;
private String messsage;
private DataInputStream in;
private static final String CATEGORIA = "CLIENTE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textField = (EditText) findViewById(R.id.editText1); //reference to the text field
button = (Button) findViewById(R.id.button1); //reference to the send button
textView = (TextView)findViewById(R.id.textView1);
//Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
messsage = textField.getText().toString(); //get the text message on the text field
textField.setText(""); //Reset the text field to blank
try {
client = new Socket("10.0.2.2", 4444); //connect to server
// client = new Socket("192.168.1.4",1965); //connect to server
Log.i(CATEGORIA,"CONECTADO");
printwriter = new PrintWriter(client.getOutputStream(),true);
in = new DataInputStream(client.getInputStream());
printwriter.write(messsage); //write the message to output stream
printwriter.flush();
printwriter.close();
textView.setText("antes");
String message = null;
message = in.readUTF(); //**** trava aqui ****///
textView.setText(message);
client.close(); //closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
sds
j.silvestre