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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:13:13+00:00 2026-06-04T12:13:13+00:00

After reading a few posts and reading on the developers page about ASYNCTASK, I

  • 0

After reading a few posts and reading on the developers page about ASYNCTASK, I came up with the following code, and assigned it to a button:

private class TalkToServerTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String response = "";
        try {
            InetAddress serverAddr = InetAddress.getByName(params[0]);
            Socket s = new Socket(serverAddr, Integer.valueOf(params[1]));

            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(s.getOutputStream())), true);

            // WHERE YOU ISSUE THE COMMANDS

            out.println(params[2]);
            // BufferedReader input = new BufferedReader(
            // new InputStreamReader(s.getInputStream()));

            DataInputStream dataInputStream = null;
            dataInputStream = new DataInputStream(s.getInputStream());
            st = dataInputStream.readLine().toString();
            // String st = s.readLine();
            // st = input.readLine();
            // read line(s)

            s.close();
            return st;

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        serverresponse = result;
    }

}

The idea being, that when a button is clicked, the ASyncTask sends the word “getDomains” to a console app running on my server, the server acts on this and sends back a string with the list of domains created on an email server.

I have verified that the server is receiving the command “getdomains”, and it in turn replies with a pipe-delimited string of domains. The problem being however, that I’ve set a Toast to pop up with the results of the socket transaction, and the toast shows nothing. If I hit the button again, the Toast shows the list of domains. To me, it seems as if the socket is first returning an empty

Here is the button code:

case R.id.btnDomains:
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(this);
            IpAddress = sp.getString("ipaddress", "0.0.0.0");
            Serverport = sp.getString("tcpport", "12345");
            buttonpressed = "domains";
            // TalkToServerTask task = new TalkToServerTask();
            new TalkToServerTask().execute(IpAddress, Serverport, "getDomains");

            Intent buttonActivity = new Intent(MainActivity.this, Rules.class);
            buttonActivity.putExtra(MainActivity.DOMAINLIST, serverresponse);

            Toast.makeText(getApplicationContext(), serverresponse,
                    Toast.LENGTH_SHORT).show();

the variable “serverresponse” is what at first is showing empty, but then shows the list of servers.

  • 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-04T12:13:15+00:00Added an answer on June 4, 2026 at 12:13 pm

    As Thepoosh mentioned, AsyncTask is working on a separate thread.

    Therefore the thread is being executed and didn’t get result yet when you first time press the button.

    What you should do is to show the data in onPostExecute method. Also you should pass the context to your AsyncTask.

    public TalkToServerTask(Context context)  {
        this.context = context;
    }
    
    @Override
    protected void onPostExecute(String result) {
        Intent buttonActivity = new Intent(context, Rules.class);
        buttonActivity.putExtra(MainActivity.DOMAINLIST, result);
    
        Toast.makeText(context.getApplicationContext(), result, Toast.LENGTH_SHORT).show();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to rotate Zxing display after reading a few questions and posts about
I have a few questions about this after reading the iPhone documentation on it:
After reading quite few articles on deployment, i am bit confused about the procedure.
After reading a few posts here I formulated this function which is sort of
After reading a few articles about Android configuration, I still don't know how to
I want to show custom animation in my ProgressDialog . After reading few posts,
After reading http://www.w3schools.com/tags/tag_noscript.asp confused about onclick event on noscript tag. Anybody knows the purpose?
After reading the mkdir(2) man page for the Unix system call with that name,
After reading a description about swapping pointer addresses on Stackoverflow, I have a question
after reading a few articles, I am still not sure I understand Multi-threading to

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.