Building a one-on-one messaging app for Android with Java backend

Hey everyone, I’m working on a messaging app and could use some help. I’ve got the server part done in Java, but I’m stuck with the Android client.

The weird thing is, when I test both server and client in Java, everything works fine. But when I try to make the client for Android, it’s not cooperating.

I’ve set up a simple UI with a text view for displaying messages, an edit text for typing, and a button to send. I’m attempting to open a socket connection to ‘127.0.0.1’ on port 1234.

My main issues are:

  1. Am I on the right track with my approach?
  2. Why is my socket object null inside the handshake method?

I’m encountering a NetworkOnMainThreadException when trying to send a message. I tried a simplified version of my code that resembles a basic chat activity:

public class ChatActivity extends AppCompatActivity {
    private TextView chatView;
    private EditText messageInput;
    private Button sendButton;
    private Socket chatSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);

        initializeViews();
        connectToServer();
    }

    private void connectToServer() {
        new Thread(() -> {
            try {
                chatSocket = new Socket("127.0.0.1", 1234);
                // Start message handling
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }

    // Other methods for sending and receiving messages
}

Any ideas on what I might be doing wrong or how to resolve this issue? Thanks!

I’ve encountered similar issues when developing messaging apps for Android. Here’s what I’ve learned:

Firstly, using ‘127.0.0.1’ won’t work on an Android device or emulator. You need to use your computer’s actual IP address when testing. Find this in your network settings.

Secondly, Android strictly forbids network operations on the main thread. While you’ve moved socket creation to a background thread, ensure that all network tasks (sending and receiving messages) execute on a separate thread.

Consider using AsyncTask or Kotlin coroutines for effective asynchronous programming. Robust error handling is equally crucial; wrap your socket operations in try-catch blocks to handle exceptions promptly. Socket programming can be challenging, and thorough testing is key to overcoming these hurdles.

hey, I’ve dealt with similar stuff before. make sure you’re not using ‘127.0.0.1’ on Android - it won’t work. use your actual computer IP. also, move ALL network stuff to separate threads, not just socket creation. AsyncTask or coroutines can help. good luck and let us know how it goes!

hey there! have you considered using websockets instead of raw sockets? they’re easier to work with on android and can handle real-time messaging better. also, don’t forget to use your computer’s actual ip address when testing. what kind of features are you planning for your app? sounds like an interesting project!