0

I can pass a string value to Asynctask, but i am unable to pass the array.

Can anyone please inform me, how can i do this ?

And I am want to pass that array to the server (//printwriter.write(messsages); ) ...HOW CAN I PASS THIS ?

{
String[] trymessage = new String[3];


for(int x=0;x<3;x++)
                {
                    trymessage[x]= Integer.toString(x);
                }
new Asynctask1().execute(trymesssage);      
            }


public class Asynctask1 extends AsyncTask<String, Void, Void> {
      private PrintWriter printwriter;      
  protected Void doInBackground(String... messages) {
    final String IP_ADDRESS = "192.168.0.4";
        final int DEST_PORT = 4444;

        try {
            Socket client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server
            printwriter = new PrintWriter(client.getOutputStream(), true);
          printwriter.write(messsages); // write the message to output stream

            printwriter.flush();
            printwriter.close();
            client.close();
        }                                       
    }
      else{}            
      return null;
  }
}                                      

I thought to change the Asynctask1 argument to be String, void, String[]. But no effective results.

user3739443
  • 71
  • 1
  • 1
  • 3

2 Answers2

0

An easy solution would be to just create a new constructor:

public class Asynctask1 extends AsyncTask<Void, Void, Void> {
    private String[] mMessages;

    public Asynctask1(String[] messages) {
        mMessages = messages;
    }

    // ...
}                                      
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
0

try changing the arguments of AsyncTask to be String[], Void, Void.

The first argument represents the type of parameter that will be send to doInBackground().

The third argument reresents the type of parameter that will be send to onPostExecute().

Try this:

    for(int x=0;x<3;x++)
    {
        trymessage[x]= Integer.toString(x);
    }
    new Asynctask1().execute(trymessage);
}

public class Asynctask1 extends AsyncTask<String[], Void, Void> {
    private PrintWriter printwriter;

    protected Void doInBackground(String[]... messages) {
        final String IP_ADDRESS = "192.168.0.4";
        final int DEST_PORT = 4444;
        String[] messageList = messages[0];
        if (messageList != null) {
            try {
                Socket client = new Socket(IP_ADDRESS, DEST_PORT); // connect to server

                for (String message : messageList) {
                    System.out.println(message);
                }

                printwriter = new PrintWriter(client.getOutputStream(), true);

                for (String message : messageList) {
                    printwriter.write(message); // write the message to output stream
                }

                printwriter.flush();
                printwriter.close();
                client.close();
            } catch (Exception e) {
                Log.e("Asynctask1@doInBackground", e.getMessage(), e);
            }
        }
        return null;
    }
}

Also, the method write() of PrintWriter doesn't allow you to send an Array, you will have to send it String by String