0

I am trying to code a simple chat app for Android so I have a chat server and I am trying to connect to it. So in the main class I have

final EditText etHost = (EditText) findViewById(R.id.entry1);
final EditText etPort = (EditText) findViewById(R.id.entry2);
final EditText messageBoard = (EditText) findViewById(R.id.editText1);
final EditText etSend = (EditText) findViewById(R.id.editText2);

soEd = new SocketAndEditText(etHost, etPort, messageBoard, etSend);
SetNetworking net = new SetNetworking();
SocketAndEditText net.execute(soEd);
final Button sendButton = (Button) findViewById(R.id.button2);

sendButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Messager mes = new Messager();
                mes.execute(soEd);
            }
        });

SocketAndEditText is a class where I simply keep the socket and four EditText components. The idea is that SetNetworking will establish the network connection and Messager will be sending messages. Both classes implement AsyncTask:

public class SetNetworking extends AsyncTask<SocketAndEditText, Void, Void> {
...

public class Messager extends AsyncTask<SocketAndEditText, Void, Void> {
...

But strangely in onPostExecute(Void result) in Messager when I try to do even a quite simple thing as

try {
    InetAddress address1 = InetAddress.getByName("130.237.161.23");
    boolean reachable = address1.isReachable(4456);
    messageBoard.setText("Is host reachable?" + reachable);
} catch (Exception e) {
    messageBoard.setText("Test not working! " + e.toString());
}

I get this:

Test not working! android.os.NetworkOnMainThreadException

Why since it is in the AsyncTask and not in the main thread?

RegedUser00x
  • 2,193
  • 5
  • 25
  • 32

2 Answers2

1

You cannot do network I/O in onPostExecute of an AsyncTask, since that method runs on the UI thread. You need to do all network activity in doInBackground.

Ted Hopp
  • 227,407
  • 48
  • 383
  • 507
  • Interesting! I guess this could help me solving http://stackoverflow.com/questions/6622101/networkonmainthread-exception-while-executing-httprequest-in-a-bound-service-fro/10037830#10037830 – Shine May 13 '12 at 21:12
  • That solved the problem but I have another one now: it returns false (i.e.) the address is not reachable and when I run the same code `InetAddress address1 = InetAddress.getByName("130.237.161.23"); boolean reachable = address1.isReachable(4456);` the result is true. In the manifest I have `` What might be the cause? – RegedUser00x May 13 '12 at 22:02
  • @RegedUser00x - I don't understand--what is returning false and what is returning true? It looks like the same code. Perhaps you should start a separate question showing the relevant code. – Ted Hopp May 13 '12 at 22:13
  • Here it is http://stackoverflow.com/questions/10575747/android-app-fails-to-get-access-to-the-network – RegedUser00x May 13 '12 at 22:29
0

Your onPostExecute() of Messager class is done on the Main thread. You are also doing network IO with

InetAddress.getByName("130.237.161.23");

D-Dᴙum
  • 7,460
  • 8
  • 55
  • 93