0

I have a class that send a message by socket and I want that the message, it's the text that the user put in the plain text that it's supposed to send. So I try to catch the message of the plain text in the class but it didn't work if someone knows how to import a variable of the main class to another class.

this is the code where I execute the class



    class client extends AsyncTask<String, Void, Void> {

                Handler handler = new Handler( );

                protected Void doInBackground(String... h) {


                    TextView t3;
                    final EditText send;
                    send = (EditText) findViewById( R.id.editText );
                    t3 = (TextView) findViewById( R.id.textView );

                    try {
                        handler.post( new Runnable() {

                            @Override


                            public void run() {
                                Toast.makeText(getApplicationContext(),"start client", Toast.LENGTH_LONG).show();
                            }
                        } );
                        WifiManager manager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                        ip = Formatter.formatIpAddress(manager.getConnectionInfo().getIpAddress());
                        String[] mess = h;
                        String messag_send=(mess+"<ip>"+ip);


                        sock = new Socket( "192.168.5.178", 5000 );


                        printWriter = new PrintWriter( sock.getOutputStream() );


                        printWriter.write(messag_send);


                        String line = "no";
                        printWriter.flush();
                        printWriter.close();
                        sock.close();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }

and to  import 


 client client = new client();

            client.execute(h);



  • Give it as a [parameter to your AsyncTask](https://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3). – Arthur Attout Oct 22 '19 at 14:34
  • thanks but when i ask to send a message it send [Ljava.lang.String;@7bab97d how can i change it to String text –  Oct 23 '19 at 11:18
  • This could be caused by several things. Either the argument you give to `printer.write()`(I assume that's what you use) **genuinely** contains this value, or you have declared your variable as an `Object` instead of a `String`. In the latter case, you should ensure you are dealing with `String` variables. In the former case, you have some investigation to do as to why this happens (you might be calling a `toString()` somewhere you're not expected to do it. – Arthur Attout Oct 23 '19 at 11:44
  • thanks, i will try to find the error and will let you know. –  Oct 23 '19 at 12:00
  • i didn't find the error i edit the code if you want to find the error. i you don't want because i need to ask another question i will understand. thanks if you try to find what his wrong . –  Oct 23 '19 at 13:33

1 Answers1

0

As you can see, you receive your variable h as a vararg. So h is really an Array of Strings.

protected Void doInBackground(String... h)

However, you don't really access its content, instead you write

String[] mess = h;
String messag_send=(mess+"<ip>"+ip);

Since mess is an array, you embed its toString() value in your message.

What you need to do instead is retrieving the first value of your array (which probably holds only one element anyway) like so

String mess = h[0];
String messag_send=(mess+"<ip>"+ip);
Arthur Attout
  • 2,701
  • 2
  • 26
  • 49