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 8920913
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:23:57+00:00 2026-06-15T06:23:57+00:00

Hi I am creating a client-server application with an android device as the client.

  • 0

Hi I am creating a client-server application with an android device as the client. In my application, my client (android device) sends a request and the server has a response to the request.
I noticed that any method that causes the main thread to wait has to be done on a different thread. I am trying to do that but in my case use a ThreadExecutor. I implemented a helper Runnable class with I pass my ObjectInputStream to but for some reason, its always null. I have checked on a number of questions here but it seems most of them are about how to pass from one activity to another. However I would like to pass from my activity to my helper class and I am not sure of what to do.

Here is my code, I hope it makes more sense.

    package com.dozie.service;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.StringWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;

    import org.w3c.dom.Document;
    import org.w3c.dom.Element;

    import com.dcloud.common.Event;
    import com.dcloud.common.Message;

    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.text.Editable;
    import android.text.TextWatcher;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class RegisterActivity extends Activity {

        private Button registerButton;
        private Button cancelButton;
        private TextView errorText;
        private EditText firstnameField, lastnameField, passwordField, cPasswordField, usernameField;
        private String fname, lname, pword, cPword, uname;
        private Socket socket;
        private TextWatcher fnameWatcher, lnameWatcher, pwordWatcher, cPwordWatcher, unameWatcher;
        protected ObjectOutputStream os;
        protected ObjectInputStream is;
        private ExecutorService threadPool;
        private RegisterWorker worker;
        public static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})";
        public static final String USERNAME_PATTERN = "^[A-z0-9_-]{5,20}$"; 
        public static final String TAG = RegisterActivity.class.getName();
        public static final int SAMEFIRSTUSER = 100; //same first name and username,
        public static final int SAMELASTUSER = 200;  //same last name and username
        public static final int SAMEPASSFIRST = 300; //same password and firstname
        public static final int SAMEPASSLAST = 400; //same password and lastname
        public static final int SAMEPASSUSER = 500; //same password and username
        public static final int USERNOTMATCH = 600; //username does not match
        public static final int PASSNOTMATCH = 700; //password does not match
        public static final int CPASS_DIFF = 800; //password and confirm password are different
        public static final int GOODINPUT = 1; //every input is good

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_register);
            registerButton = (Button) findViewById(R.id.register_id);
            cancelButton = (Button) findViewById(R.id.cancel_id);
            firstnameField = (EditText) findViewById(R.id.firstname_id);
            lastnameField = (EditText) findViewById(R.id.lastname_id);
            passwordField = (EditText) findViewById(R.id.rpassword_id);
            cPasswordField = (EditText) findViewById(R.id.rcpassword_id);
            usernameField = (EditText) findViewById(R.id.rusername_id);
            errorText = (TextView) findViewById(R.id.errorText_id);
            fname="";lname="";pword="";cPword="";uname="";

            //initialize TextWatchers
            initializeWatchers();
            // add TextWatchers
            firstnameField.addTextChangedListener(fnameWatcher);
            lastnameField.addTextChangedListener(lnameWatcher);
            passwordField.addTextChangedListener(pwordWatcher);
            cPasswordField.addTextChangedListener(cPwordWatcher);
            usernameField.addTextChangedListener(unameWatcher);
            cancelButton.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    firstnameField.setText("");
                    lastnameField.setText("");
                    passwordField.setText("");
                    cPasswordField.setText("");
                    usernameField.setText("");
                    setResult(RESULT_CANCELED);
                    finish();
                }

            });
        }

        @Override
        public void onStart()
        {
            super.onStart();
            Log.d(TAG, "onStart()");
            Intent intent = getIntent();
            final String address = intent.getStringExtra("ipAddress");
            final int port = intent.getIntExtra("port", 6060);
            threadPool = Executors.newFixedThreadPool(2);
            new Thread(new Runnable()
            {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    try {
                        Log.i(TAG, "Trying to Connect to "+ address + " on " + port);
                        socket = new Socket(address, port);
                        Log.i(TAG, "Connection established!");
                        os = new ObjectOutputStream(new DataOutputStream(socket.getOutputStream()));
                        is = new ObjectInputStream(new DataInputStream(socket.getInputStream()));

                        Log.i(TAG, "Client connected to server.");
                    } catch (UnknownHostException e) {
                        // TODO Auto-generated catch block
                        Log.e(TAG, "UnknownHostException", e);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Log.e(TAG, "I/O Exception", e);
                    }

                }

            }).start();
    //      threadPool.execute(worker = new RegisterWorker(is));;
        }


        @Override
        public void onPause()
        {
            super.onPause();
            Log.d(TAG, "onPause()");
            threadPool.shutdown();
            Message out = new Message("disconnect");
            out.setRequest(Event.Request.DISCONNECT);
            try {
                os.writeObject(out);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e(TAG, "I/O Exception", e);
            }
            finally
            {
                try {
                    is.close();
                    os.close();
                    socket.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            Log.i(TAG, "socket closed");
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_register, menu);
            return true;
        }

        public int validateInput(String [] input)
        {
            String firstname = input[0];
            String lastname = input[1];
            String password = input[2];
            String cpassword = input[3];
            String username = input[4];

            Pattern pattern = Pattern.compile(PASSWORD_PATTERN);
            Matcher matcher = pattern.matcher(password);
            Pattern p = Pattern.compile(USERNAME_PATTERN);
            Matcher m = p.matcher(username);
            if(firstname.equals(username))
                return RegisterActivity.SAMEFIRSTUSER;
            if(lastname.equals(username))
                return RegisterActivity.SAMELASTUSER;
            if(firstname.equals(password))
                return RegisterActivity.SAMEPASSFIRST;
            if(lastname.equals(password))
                return RegisterActivity.SAMEPASSLAST;
            if(password.equals(username))
                return RegisterActivity.SAMEPASSUSER;
            if(!password.equals(cpassword))
                return RegisterActivity.CPASS_DIFF;
            if(!matcher.matches())
                return RegisterActivity.PASSNOTMATCH;
            if(!m.matches())
                return RegisterActivity.USERNOTMATCH;
            return RegisterActivity.GOODINPUT;
        }

        public void registerUser(View v)
        {
            String [] input = getUserInformation();
            int result = validateInput(input);
            switch(result)
            {
            case RegisterActivity.GOODINPUT:
                Intent info = new Intent();
                info.putExtra("username", input[4]);
                registerUser(input);
                if(worker.getServerResponse() != null)
                {
                    if(worker.getServerResponse().getResponse() == Event.Response.REQUEST_SUCCESSFUL)
                    {
                        threadPool.shutdown();
                        setResult(RESULT_OK, info);
                        finish();
                        Log.i(TAG, "registerUser(): data input successful!");
                    }
                    else if(worker.getServerResponse().getResponse() == Event.Response.REQUEST_UNSUCCESSFUL)
                    {
                        threadPool.execute(worker);
                        Log.i(TAG, "registerUser(): data input unsuccessful!");
                    }
                }

                break;
            case RegisterActivity.SAMEFIRSTUSER:
                errorText.setText("username should not be the same as firstname");
                Log.i(TAG, "registerUser(): username same as firstname");break;
            case  RegisterActivity.SAMELASTUSER:
                errorText.setText("username should not be the same as lastname");
                Log.i(TAG, "registerUser(): username same as lastname");break;
            case  RegisterActivity.SAMEPASSFIRST:
                errorText.setText("password should not be the same as firstname");
                Log.i(TAG, "registerUser(): password same as firstname");break;
            case RegisterActivity.SAMEPASSLAST:
                errorText.setText("password should not be the same as lastname");
                Log.i(TAG, "registerUser(): password same as lastname");break;
            case RegisterActivity.SAMEPASSUSER:
                errorText.setText("password should not be the same as username");
                Log.i(TAG, "registerUser(): password same as username");break;
            case RegisterActivity.CPASS_DIFF:
                errorText.setText("password and confirm password are not the same");
                Log.i(TAG, "registerUser(): password and confirm password not the same");break;
            case RegisterActivity.PASSNOTMATCH:
                errorText.setText("password format wrong. Password should contain 6-20 characters\n" +
                        "characters containing at least 1 digit, 1 lower case alphabet and 1 upper case character");
                Log.i(TAG, "registerUser(): password format not correct");break;
            case RegisterActivity.USERNOTMATCH:
                errorText.setText("username format wrong. username should contain 5-20 characters " +
                        "and should have to special characters");
                Log.i(TAG, "registerUser(): username format not correct");break;
            default:
                Log.i(TAG, "registerUser(): Code not handled");break;
            }
        }

        public String []  getUserInformation()
        {
            String firstname = firstnameField.getText().toString();
            String lastname = lastnameField.getText().toString();
            String password = passwordField.getText().toString();
            String cpassword = cPasswordField.getText().toString();
            String username = usernameField.getText().toString();
            String [] input = {firstname, lastname, password, cpassword, username};
            return input;
        }

        public void initializeWatchers()
        {
            fnameWatcher = new TextWatcher()
            {

                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    fname = firstnameField.getText().toString();
                    fname.trim();
                    updateButton();
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                }

            };

            lnameWatcher = new TextWatcher()
            {
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    lname = lastnameField.getText().toString();
                    lname.trim();
                    updateButton();
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                }
            };

            pwordWatcher = new TextWatcher()
            {
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    pword = passwordField.getText().toString();
                    pword.trim();
                    updateButton();
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                }
            };

            cPwordWatcher = new TextWatcher()
            {
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    cPword = cPasswordField.getText().toString();
                    cPword.trim();
                    updateButton();
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                }

            };

            unameWatcher = new TextWatcher()
            {
                @Override
                public void afterTextChanged(Editable s) {
                    // TODO Auto-generated method stub
                    uname = usernameField.getText().toString();
                    uname.trim();
                    updateButton();
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    // TODO Auto-generated method stub

                }
            };
        }


        public void updateButton()
        {
            boolean enabled = fname.length()>0 && lname.length()>0 && pword.length()>0 
                    && cPword.length()>0 && uname.length() >0;
                    registerButton.setEnabled(enabled);
        }

        public void registerUser(String [] input)
        {

            try {
                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
                Document doc = docBuilder.newDocument();

                Element rootElement = doc.createElement("Register");
                doc.appendChild(rootElement);

                Element user = doc.createElement("user");
                rootElement.appendChild(user);

                Element firstname = doc.createElement("firstname");
                firstname.appendChild(doc.createTextNode(input[0]));
                user.appendChild(firstname);

                Element lastname = doc.createElement("lastname");
                lastname.appendChild(doc.createTextNode(input[1]));
                user.appendChild(lastname);

                Element password = doc.createElement("password");
                password.appendChild(doc.createTextNode(input[2]));
                user.appendChild(password);

                Element username = doc.createElement("username");
                username.appendChild(doc.createTextNode(input[4]));
                user.appendChild(username);

                StringWriter writer = new StringWriter();
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                transformer.transform(new DOMSource(doc), new StreamResult(writer));
                String content = writer.toString();

                Message out = new Message(content);
                out.setRequest(Event.Request.REGISTER);
                os.writeObject(out);
                Log.d(TAG, "registerUser(): Register message sent");


            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                Log.e(TAG, "onActivityReturn(): ParserConfigurationException error", e);
            }  catch (TransformerException e) {
                // TODO Auto-generated catch block
                Log.e(TAG, "onActivityReturn(): TransformerException error", e);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e(TAG, "onActivityReturn(): IOException error", e);
            } 
        }
    }

<p>And for my runnable class</p>






package com.dozie.service;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OptionalDataException;

import android.util.Log;

import com.dcloud.common.Event;
import com.dcloud.common.Message;



public class RegisterWorker implements Runnable{

    private ObjectInputStream is;
    private Message in;
    public static final String TAG = RegisterWorker.class.getName();

    public RegisterWorker(ObjectInputStream is)
    {
        this.is = is;
        if(this.is == null)
            System.out.println("null");

    }
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            while((in = (Message) is.readObject())!=null)
            {
                if(in.getResponse() == Event.Response.REQUEST_SUCCESSFUL)
                {
                    Log.d(TAG, "Login Response: "+in.getMessage());
                }
                else if(in.getResponse() == Event.Response.REQUEST_UNSUCCESSFUL)
                {
                    Log.d(TAG, "Login Response: "+in.getMessage());
                    //give reason for rejection
                }
            }
        } catch (OptionalDataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public Message getServerResponse()
    {
        return in;
    }

}
  • 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-15T06:23:58+00:00Added an answer on June 15, 2026 at 6:23 am

    I think the code you’re writing is way more complicated than it needs to be. If you want to pass variables to a Thread, which is what I think you’re trying to do, just do this:

    public Worker extends AsyncTask<Arguments, Void, Void> {
      // method names may be slightly incorrect, writing this from memory
      public void runInBackground(Arguments...args) {
        // do what you want to do in the background
      }
    }
    

    Then start your thread:

    Arguments args = new Arguments(arg1, arg2); // whatever you want to pass along
    new Worker().execute(args);
    

    Where Arguments can be any class, using this as an example.

    Hope this helps.

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

Sidebar

Related Questions

We are creating an extranet application that will use client/server SSL certificates to enhance
I'm creating an IM client/server application for the Android OS, and am currently putting
I am thinking of creating a multi-platform portable C++ server-client application. Is it even
I am creating a client program that talks to a server (which I programmed).
I'm creating a system with a JavaScript client that will communicate with the server
I am creating a desktop/client application that will be installed on +/- 5 PCs,
gcc 4.4.3 c89 I am creating a client server application and I will need
I am creating server-client application for iPhone. I want to communicate between two application
I am writing a netty client and server application that will write JVM GC
When creating a client server application (serversocket), would I create 2 seperate projects or

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.