0

I want to write Tcp Client class on android but I couldnt convert its all method to doinbackground form. all code works in doinbackground method but when I changed it gives NetworkOnMainThreadException.

public class Client extends AsyncTask<Void, Void, Void> {
String playername = null;
Socket clientSocket;
DataOutputStream outToServer;
BufferedReader inFromServer;

public Client() {}

@Override
protected Void doInBackground(Void... params) {
    try {
        clientSocket = new Socket("192.168.1.7", 9999);
        outToServer = new DataOutputStream(clientSocket.getOutputStream());
        inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    } catch(Exception e) {
        e.printStackTrace();
    }
    return null;
}

public boolean isIn(String name) {
    try{
        String sentence;
        String modifiedSentence;
        if(clientSocket.isConnected()) {
            sentence = "|isnamein|" + name + "|";
            outToServer.writeBytes(sentence + '\n');
            modifiedSentence = inFromServer.readLine();
            String[] playername = parse(modifiedSentence);
            System.out.println("FROM SERVER: " + modifiedSentence);
            clientSocket.close();
        } else {
            System.out.println("Not connected");
        }
    } catch(Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}
csselo
  • 25
  • 4
  • Please show your stacktrace – ligi Apr 17 '16 at 12:13
  • Although your question was marked as a duplicate, I think your question has less to do with the "duplicate" question and more to do with what code is executed on which threads here. See my answer, it'll hopefully point you in the right direction. – Magnus Apr 17 '16 at 12:28

1 Answers1

1

It looks like your method isIn() is meant to be called from outside the AsyncTask. In that case, it will be executed on the main/UI thread. You have to handle all the network access code inside doInBackground() (or at least call it from doInBackground).

The easiest way to think about these things is to realize that Client is not a separate thread in itself - it's actually doInBackground() that is the "thread" here. All other methods (such as isIn()) are just regular methods, it is irrelevant that they are defined inside Client, they will still be executed on the main thread if you don't call them from doInBackground().

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • I need to add many method like isIn so I couldnt add it in doinbackground – csselo Apr 17 '16 at 13:15
  • Well, you still need to call them from another thread than the main/UI thread, i.e. `doInBackground` if you're using `AsyncTask`. Otherwise you can put the code in `isIn()` inside a `Runnable` or `Thread` to make it execute in a new thread. – Magnus Apr 17 '16 at 13:24
  • I tried to convert my client class to runnable because I always use it to get data but nothing changes.it gives same error. is there any different between inside a runnable or ruunable class – csselo Apr 17 '16 at 14:00
  • solved when I add it to oncreate StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); – csselo Apr 17 '16 at 15:15
  • Keep in mind that that is not really a proper solution, it just means you disable `StrictMode` by calling `permitAll()`, which cures the symptom but not the cause. You're still doing networking on the main/UI thread, which is the cause of your problems. If your networking logic takes a long time, the UI will freeze. – Magnus Apr 17 '16 at 15:20