怎么了家伙。我在这里遇到了问题。文件正在运行但存在逻辑错误。我将我的Android应用程序连接到这个java应用程序。我试图在JFrame列表中发送多条消息,但是在JFrame运行时它没有显示。它就像这样
(JFRAME仍然没有显示)我连接到java app
(JFRAME仍然没有显示)发送一些消息,它在println中显示 然后我关闭我的Android应用程序
JFrame从java app显示我发送的消息。(消息存储在JList中)
我希望它像是,当JFrame显示时,我想要追加Jlist中的值。请参阅此代码。这是使用
的代码import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JList;
public class frmTestList extends JFrame {
public static final int portNumber = 60123;
private JPanel contentPane;
static String data;
static DefaultListModel model = new DefaultListModel();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
frmTestList frame = new frmTestList();
frame.setVisible(true);
try {
ServerSocket serverSocket = null;
serverSocket = new ServerSocket(portNumber);
Socket socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while((data = br.readLine()) != null ){
System.out.println("Message from the client: " + data);
model.addElement(data);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public frmTestList() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JList list = new JList(model);
contentPane.add(list, BorderLayout.CENTER);
}
}
Android代码:
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class MainActivity extends Activity {
Socket socket = null;
public String debuggingString = "DEBUG";
public String hostname = ""; // <-- my ipv4 is here
public int portNumber = 60123;
@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 br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Message from server: " + br.readLine());
} catch (
Exception e
)
{
Log.e(debuggingString, e.getMessage());
}
}
}.start();
}
;
public void sendMessage(View v) {
EditText editText = (EditText) findViewById(R.id.editText);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
bw.write(editText.getText().toString());
bw.newLine();
bw.flush();
} catch (Exception e) {
Log.e(debuggingString, e.getMessage());
}
}
}
先谢谢你们!
答案 0 :(得分:0)
更新:进一步思考后,我猜:你的问题可能是
A)您的服务器位于那里并等待来自客户端的输入。含义:它循环播放&#34;新输入&#34; ...可能直到客户关闭连接。
和/或
B)你的&#34;服务器&#34;尝试在接受的同一个线程内更新UI。所以,试试:
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line = null;
while( (line = br.readLine() ) != null) {
System.out.println("Message from server: " + br.readLine());
EventQueue.invokeLater(new Runnable() {
public void run() {
model.addElement(line);
}
最后:如果这些想法没有成功;你应该退一步 - 通过简化设置(通过将Android从图片中删除):在JFrame中创建客户端套接字,同时让一个进程成为客户端/服务器。这将使调试/实验更容易。只有当它工作时,将客户端...移动到另一个JVM,然后移动到&#34; Android&#34;。
换句话说:退一步,从一个非常简单的客户端/服务器测试开始,该测试可以在一个类中完成所有操作。如果有效,继续前进。
答案 1 :(得分:0)
您的JFrame未显示的原因是您从Event Dispatching Thread执行所有套接字读取。这样做会锁定所有UI更新。将套接字读取操作移动到其自己的Thread,并在更新JList模型时调用invokeLater。
public static void main(String[] args) {
frmTestList frame = new frmTestList();
EventQueue.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});
Runnable runnable = new Runnable() {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket socket = serverSocket.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while((data = br.readLine()) != null ) {
EventQueue.invokeLater(new Runnable() {
public void run() {
model.addElement(data);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
new Thread(runnable).start();
}