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

  • Home
  • SEARCH
  • 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 8212869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:55:00+00:00 2026-06-07T10:55:00+00:00

I am currently creating a project that needs to have a simple async task

  • 0

I am currently creating a project that needs to have a simple async task to take care of a thread running behind the scenes. The user needs to login. I am using another class called PVAndroid Client that supplies useful methods and has an XML serializer form packets for me. I am completely new to working with threads or doing anything with servers, so this may be completely wrong or somewhat right.

I get the data the user entered: the ip address and port, their username (I split this into first and last name), their region they selected. I encrypt their password, and attempt to connect to the tcp using ip address and port number. I am trying to work in the async task but am kind of confused on what I should do. Can anyone guide me in the right direction and help me out?

Thank you I really appreciate it.

    private TcpClient myTcpClient = null;
    private UdpClient udpClient;
    private static final String USERNAME_SHARED_PREFS = "username";
    private static final String PASSWORD_SHARED_PREFS = "password";
    private static final String IP_ADDRESS_SHARED_PREFS = "ipAddressPref";
    private static final String PORT_SHARED_PREFS = "portNumberPref";
    private String encryptedNameLoginActivity, encryptPassLoginActivity;
    private EditText userText, passText;
    private String getIpAddressSharedPrefs, getPortNumberPrefs;
    private String getUserNameValue;
    private String getPasswordValue;
    private String fName, lName;
    private SharedPreferences settings;
    private Editor myEditor;
    private boolean getCheckedRemember;
    private boolean resultCheck = false;
    private int portNum;
    private Button submitButton;
    private String userMACVARIABLE = "";
    private String regionSelected, gridSelected;
    private Spinner regSpinner, gridSpinner;
    PVDCAndroidClient client;
    private int userNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        client = new PVDCAndroidClient();
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Take care of getting user's login information:
        submitButton = (Button) findViewById(R.id.submitButton);
        userText = (EditText) findViewById(R.id.nameTextBox);
        passText = (EditText) findViewById(R.id.passwordTextBox);
        regSpinner = (Spinner) findViewById(R.id.regionSpinner);

        // grid selected as well? sometime?
        regSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v,
                    int position, long rowId) {
                regionSelected = regSpinner.getItemAtPosition(position)
                        .toString();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
        submitButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                settings = PreferenceManager
                        .getDefaultSharedPreferences(AndroidClientCompnt.this);

                getIpAddressSharedPrefs = settings.getString(
                        IP_ADDRESS_SHARED_PREFS, "");
                portNum = Integer.parseInt(settings.getString(
                        PORT_SHARED_PREFS, ""));

                if (getIpAddressSharedPrefs.length() != 0 && portNum != 0) {
                    if (userText.length() != 0 && passText.length() != 0) {
                        try {

                            try {
                                // encrypting the user's password.
                                encryptPassLoginActivity = Secure.encrypt(passText
                                        .toString());
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                            // first connect attempt.
                            myTcpClient = new TcpClient();
                            myTcpClient.connect(getIpAddressSharedPrefs,
                                    portNum);
                            // here is where I want to call Async to do login
                            // or do whatever else.

                            UploadTask task = new UploadTask();
                            task.execute();


                        } catch (Exception e) {
                            Toast.makeText(getApplicationContext(),
                                    "Could not connect.", Toast.LENGTH_LONG)
                                    .show();
                            e.printStackTrace();
                        }
                    }

                }
            }
        });
    }

    private class UploadTask extends AsyncTask<String, Integer, Void> 
    {
        @Override
        protected void onPreExecute() {
            Toast.makeText(getApplicationContext(), "Loading...",
                    Toast.LENGTH_LONG).show();
                            }

        @Override
        protected Void doInBackground(String... names) {

                    resultCheck = myTcpClient.connect(getIpAddressSharedPrefs,
                            portNum);


                    if (resultCheck == true) {
                        while (myTcpClient.getUserNum() < 0) {
                            // num set? session? with proxy server?
                        }

                        String[] firstAndLast;

                        String spcDelmt = " ";

                        firstAndLast = userText.toString().split(spcDelmt);

                        fName = firstAndLast[0];
                        lName = firstAndLast[1];

                        // set up the tcp client to sent the information to the
                        // server.
                        client.login(fName, lName, encryptPassLoginActivity,regionSelected, 128, 128, 20);

                    } else {
                        Toast.makeText(getApplicationContext(),
                                "Connection not successful", Toast.LENGTH_LONG)
                                .show();
                    }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Toast.makeText(getApplicationContext(), "Connected",
                    Toast.LENGTH_LONG).show();
        }
    }

}
  • 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-06-07T10:55:01+00:00Added an answer on June 7, 2026 at 10:55 am

    First

    @Override
    protected Void doInBackground(String...params) {
        new Thread (new Runnable() {
           // ...
        }
    }
    

    Never do this again. There is no need to create new Thread in doInBackground method which actually running on background Thread. So remove it.

    The advice to you is tricky because you need to read about Threads, work with Connection etc. So the best advice to you is to read some tutorials, examples of basic applications and read references. So you can start here:

    Android TCP Client and Server Communication Programming–Illustrated with Example

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

Sidebar

Related Questions

I'm currently learning rails by creating a simple project management app. I've gotten to
I'm currently in a process of creating a new C project and I have
I am creating a simple project that will allow me to upload and download
We have a setup project that currently adds Project Output's from different visual studio
I have been tasked with refactoring a project that currently uses an EAV model
I'm currently creating my first real life project in Pervasive. The task is to
I'm currently working on a project that requires that I have a div stacked
I have a solution that contains an ASP.NET Web Site (file-based) project. I'm currently
I have a project that we need to set up a simple branching strategy
I currently use visual studio 2008 for creating projects that can run on windows.

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.