如何在我的 Android 应用程序中编写 StreamReader 程序?
我要开发一个 Android 应用程序,它从服务器接收消息,但我可以连接到服务器,但无法接收消息,因为当我单击按钮连接时会阻止。我认为这是因为循环,但不知道如何以其他方式解决这种情况,感谢您的帮助。
简而言之,我想知道是否有另一种方法可以检测我何时收到消息并呈现它。
更新:我找到了这样的解决方案:
String IP;
String comando;
PrintWriter msgout;
BufferedReader msgin;
Socket socket;
private ConnectedThread mConnectedThread;
public void connect(){ //Function to Button connect
IP = edit.getText().toString();
try {
InetAddress serverAddr = InetAddress.getByName(IP); //TCPServer.SERVERIP
Socket socket = new Socket(serverAddr, 4444); //Porta 4444 serverAddr
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
msgout = out;
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
msgin = in;
zav.text1.setText("Connectado.");
//Wait to mensages
mConnectedThread = new ConnectedThread();
mConnectedThread.start();
} catch (Exception e) {
text1.setText("Erro! Não conectado." + e);
}
}
public void send(String message){
try {
msg.println(message);
} catch(Exception e) {
text1.setText("Erro! Comando não enviado." + e);
}
}
private class ConnectedThread extends Thread {
public void run(){
while(true){
//Read
try {
String str = msgin.readLine();
text1.setText(str);
} catch(Exception e) {
text1.setText("Error" + e);
}
}
}
}
I am to develop an Android application that receives a message from server but I can connect to server but can't receive a message because when I clicked on Button connect blocks. I think it's because of the cycle but do not know how to resolve this situation in another way and would appreciate your help.
In short I want to know if there's another way to detect when I receive a message and presents it.
Update: I found the solution like this:
String IP;
String comando;
PrintWriter msgout;
BufferedReader msgin;
Socket socket;
private ConnectedThread mConnectedThread;
public void connect(){ //Function to Button connect
IP = edit.getText().toString();
try {
InetAddress serverAddr = InetAddress.getByName(IP); //TCPServer.SERVERIP
Socket socket = new Socket(serverAddr, 4444); //Porta 4444 serverAddr
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
msgout = out;
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
msgin = in;
zav.text1.setText("Connectado.");
//Wait to mensages
mConnectedThread = new ConnectedThread();
mConnectedThread.start();
} catch (Exception e) {
text1.setText("Erro! Não conectado." + e);
}
}
public void send(String message){
try {
msg.println(message);
} catch(Exception e) {
text1.setText("Erro! Comando não enviado." + e);
}
}
private class ConnectedThread extends Thread {
public void run(){
while(true){
//Read
try {
String str = msgin.readLine();
text1.setText(str);
} catch(Exception e) {
text1.setText("Error" + e);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试过推送通知吗?这是一个很好的起点 http://tokudu.com /2010/如何实现 Android 的推送通知/
Have you tried Push Notification? Here is a good place to start http://tokudu.com/2010/how-to-implement-push-notifications-for-android/