i wrote android code to obtain an IP address from user in EditText, then communicate the mobile with a computer ip which obtained by user in EditText.
this is my code, when I click on button nothing work:
public class MainActivity extends Activity {
Button b;
EditText et;
TextView tv;
private Socket socket;
int PORT = 4003;
String HOST; //HOST = " 192.168.2.1"
private Handler textview_handler_thread;
class ClientThread implements Runnable {
@Override
public void run()
{
try {
socket = new Socket(HOST, PORT);
Message msg = null;
while(true) {
msg = textview_handler_thread.obtainMessage();
msg.obj = "Process Completed Succesfully";
textview_handler_thread.sendMessage(msg);
}
}
catch (Exception e){
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
et = (EditText) findViewById(R.id.editText1);
HOST = et.getText().toString();
new Thread(new ClientThread()).start();
textview_handler_thread = new Handler() {
public void handleMessage(Message msg) {
Object o = msg.obj;
if (o == null)
o = "";
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(o.toString());
}
};
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
try {
DataOutputStream output_to_server = new DataOutputStream(socket.getOutputStream());
String client_str = et.getText().toString();
output_to_server.writeBytes(client_str);
output_to_server.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
The problem in HOST variable it shoul contain IP address from user. At first i wrote HOST = "192.168.2.1"; But now i want the user do that.
NOTE : if i wrote HOST = "192.168.2.1"; it is executed successfully. when I assigned the valu of HOST to edit Text CANT communicate with the computer WHY?
HOST?