0

Well, Android 4.0 doesn't allow networking on UI thread. So I made a new thread.

public class NetThread extends Thread {
    BukkitController bc;
    Socket sock;
    ObjectOutputStream outstream;
    ObjectInputStream instream;
    boolean loaded=false;
    String ServerIP="192.168.123.103";
    String id="ssw";
    String pw="ssw";
    ArrayList<String> plist=new ArrayList<String>();
public void run(){
    try {
        sock=new Socket(ServerIP,3560);
        outstream=new ObjectOutputStream(sock.getOutputStream());
        instream=new ObjectInputStream(sock.getInputStream());
        //LOGIN
        outstream.writeObject("SIGN "+id+" "+pw);
    } catch (Exception e){}
        Object tmp;
        try{
        outstream.writeObject("PLIST");
        plist=(ArrayList<String>) instream.readObject();
        } catch ( Exception e){
        }


    loaded=true;
    while(true){}
}
public void Write(String msg){
    try {
        outstream.writeObject(msg);
    } catch ( Exception e){
    }
}

}

And in the UI thread, I made the object :

m_nt=new NetThread();
m_nt.start();

After testing with LogCat, I found that the source stops at:

sock=new Socket(ServerIP,3560);

How can I solve this? I had no error, just frozen there.

1
  • 1
    can post the log with exception
    – santhu
    May 10, 2012 at 8:47

3 Answers 3

0

You cannot perform network IO on the UI thread on Honeycombe. Technically it is possible on earlier versions of Android, but is a really bad idea as it will cause your app to stop responding, and can result in the OS killing your app for being badly behaved. You'll need to run a background process or use AsyncTask to perform your network transaction on a background thread.

There is an article about a linkPainless Threading on the Android developer site which is a good introduction to this, and will provide you with much better depth of answer than can be realistically provided here.

1
  • Well, it was a simple problem - free DNS service by 'DYNDNS' didn't work. Thanks for your responce.
    – Namnamseo
    May 15, 2012 at 13:52
0

Use this:

try{
sock=new Socket(InetAddress.getByName(ServerIP),3560);
//more here
}
catch {Exception e}
{
Log.e("MyThread", "Error " + e.getMessage());
e.printStackTrace();
}
1
  • Maybe you don't have acces to that ip and port. At work I don't have access outside the network and I get timeout exception. This exception came after 2-3 minutes. Maybe this is your problem. Try from command line telnet <ip> <port> to see if you have acces to that address.
    – Ahyle Blue
    May 10, 2012 at 20:10
0

I recommend using an Asynctask for handling your network code. though I have to admit it is strange your code just stops. If the server is not available you should get an exception.

for some additional information on AsyncTasks check Android developer

1
  • Well, it was a simple problem - free DNS service by 'DYNDNS' didn't work. Thanks for your responce.
    – Namnamseo
    May 15, 2012 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.