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;
}