1

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?

4
  • It doesn't seem you start the Handler Commented Apr 15, 2016 at 23:52
  • Are you getting the user input value in HOST ? Commented Apr 15, 2016 at 23:54
  • Remove getText() from onCreate() and put in onClick() Commented Apr 16, 2016 at 0:07
  • yes jgm the user input should saved in HOST Commented Apr 16, 2016 at 0:09

1 Answer 1

1

That's because you assign the value to HOST at OnCreate. At that time, that text box is still empty yet.

You may want to do it in the OnClick event

String client_str = et.getText().toString();
HOST = Client_str;

At this time, HOST shall contain the IP address the user typed. Also, you shall not start the thread at OnCreate. At that time, HOST has nothing yet.

Sign up to request clarification or add additional context in comments.

3 Comments

thank you... please can you edit my code. your comment not clear to me.
how can i send a textview content to the c# application please??
Could you be more specific? Sometime just pass a string would be enough

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.