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);
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);