Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4101960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:45:52+00:00 2026-05-20T20:45:52+00:00

I am developing this app and i need at some point to send data

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T20:45:52+00:00Added an answer on May 20, 2026 at 8:45 pm

    Since network connections can take up time and make your app unresponsive if done on the UI thread, you should probably do them in an AsyncTask. If your AsyncTask is sufficiently complex it could be worth implementing in its own java file as a proper public class.

    This answer would give you the basic skeleton. AsyncTask makes it easy to interact with the UI at the beginning and the end of your background loading, if you needed to show some progress indication.

    Thread tends to be a worse choice as you are still left with the problem of UI interaction when your thread completes as well as making sure that the thread doesn’t continue its work after your activity finishes; an AsyncTask can be also be cancelled and then onCancelled will get executed instead of onPostExecute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This past summer I was developing a basic ASP.NET/SQL Server CRUD app, and unit
I am developing a Java app where several computers need to keep some a
I am developing a web app that accesses some external JSON data. I'm currently
We are developing an android app, and i need some analytics to track usage
I have this application I'm developing in JSP and I wish to export some
I'm developing a 3-column website using a layout like this: <div id='left' style='left: 0;
I'm developing an Asp.Net 3.5 application and I need to print some application generated
Currently, I am developing an app for a China customer. China customer are mostly
I'm developing a mechanism for interchanging data between two or more processes using shared
I am developing an iPhone app and would like to create some sort of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.