I am developing this app and i need at some point to send data (doubles and strings mostly) to a server. I will be using TCP socket and DataOutput/InputStreams. I was wondering what would be the best way to do this. Should i have a seperate class to handle the connection with write/read methods implemented or maybe just define Sockets/Streams etc in my main Activity class in onCreate()? Is the first way even possible? Any examples would be appreciated.
ps. Should i use a different Thread to handle the connection?
edit.
So if i got it right, this should be correct:
public class ConnectionHandler extends AsyncTask<Void, Void, Void>{
public static String serverip = "192.168.1.100";
public static int serverport = 7777;
Socket s;
public DataInputStream dis;
public DataOutputStream dos;
public int message;
@Override
protected Void doInBackground(Void... params) {
    try {
        Log.i("AsyncTank", "doInBackgoung: Creating Socket");
        s = new Socket(serverip, serverport);
    } catch (Exception e) {
        Log.i("AsyncTank", "doInBackgoung: Cannot create Socket");
    }
    if (s.isConnected()) {
        try {
            dis = (DataInputStream) s.getInputStream();
            dos = (DataOutputStream) s.getOutputStream();
            Log.i("AsyncTank", "doInBackgoung: Socket created, Streams assigned");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket not connected");
            e.printStackTrace();
        }
    } else {
        Log.i("AsyncTank", "doInBackgoung: Cannot assign Streams, Socket is closed");
    }
    return null;
}
public void writeToStream(double lat, double lon) {
    try {
        if (s.isConnected()){
            Log.i("AsynkTask", "writeToStream : Writing lat, lon");
            dos.writeDouble(lat);
            dos.writeDouble(lon);
        } else {
            Log.i("AsynkTask", "writeToStream : Cannot write to stream, Socket is closed");
        }
    } catch (Exception e) {
        Log.i("AsynkTask", "writeToStream : Writing failed");
    }
}
public int readFromStream() {
    try {
        if (s.isConnected()) {
            Log.i("AsynkTask", "readFromStream : Reading message");
            message = dis.readInt();
        } else {
            Log.i("AsynkTask", "readFromStream : Cannot Read, Socket is closed");
        }
    } catch (Exception e) {
        Log.i("AsynkTask", "readFromStream : Writing failed");
    }
    return message;
}
}
and i would use something like this in my Activity class:
ConnectionHandler conhandler = new ConnectionHandler();
    conhandler.execute();
    conhandler.writeToStream(lat , lon);
 
     
    