Search code examples
androidapache-mina

Apache Mina: IllegalStateException when calling connect


I am trying to make a simple message application on Android with Apache Mina. I am getting IllegalStateException when calling connect(new InetSocketAddress(HOSTNAME, PORT)).

Here is my client code:
http://pastebin.com/NR2H6X0t

Here is my server code:
http://pastebin.com/Q5fQnu2p

And here is the logcat output:

http://pastebin.com/Egsh0Ce4

I am testing my apllications in same wifi network with a tablet and phone.


Solution

  • Even I can't see anything wrong with your code. But the LogCat says org.apache.mina.transport.socket.nio.NioSocketConnector.connect() of NioSocketConnector.java class at line# 185) your application is throwing NetworkOnMainThreadException. First fix that than check if you are getting the same error.

    Apart from above I have a suggestion (for readability purpose): In connectToServerButtonClicked() of ClientActivity instead of using

    for (; ; ) { 
        //rest of your code 
    }
    

    better use

    while(true){
        //rest of your code
    } 
    

    Edited: On request of @alkis coping my answer from comment below to answer part for better redability

    NetworkOnMainThreadException : This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your network related code in AsyncTask.