what's up guys. I'm having a problem here. The file is running but there are logical errors. I connected my android app to this java app. I'm trying to send multiple messages in a list of a JFrame, it shows, but it's not showing while the JFrame is running. It goes like this
(JFRAME STILL NOT SHOWING)I connect to java app
(JFRAME STILL NOT SHOWING)Send some messages, it shows in println Then after i close my android app
JFrame shows up from java app with the messages i sent.(The messages are stored in a JList)
I want it to be like, while the JFrame is showing, i want the values in Jlist to append. Please see this code. This is the code im using
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 Code:
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());
}
}
}
Thanks in advance guys!
Update: after further thinking, I am guessing: your problem could be
A) that your server sits there and waits for input from the client. Meaning: it loops on "new input" ... probably until it the client closes the connection.
and/or
B) that your "server" tries to update the UI within the same thread that accepts. So, try:
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);
}
Finally: if those ideas dont work out; you should step back - by making your setup easier (by taking Android out of the picture): create the client socket within your JFrame and have that one process be client/server at the same time. This will make debugging/experimenting much easier. Only when that works, move the client ... into another JVM, and then to "Android".
In other words: step back, and start with a very simple Client/Server test that does everything in one class. When that works, move on.