0

My requirement is I have an emulator listening on port 5001. How can I do a HTTP Post to this port from another instance of my emulator? What should be the URL? When I tried URL like, 10.0.2.15:5001, am getting exceptions(No response and target server failed to respond).

Also, even when I have the serversocket listening in a separate thread, am getting ANR exception. Please let me know if I have to do port re-direction or port forwarding.

Any example link/tutorial would be helpful(as I couldnt find one). Thanks in advance!

user1741274
  • 759
  • 3
  • 13
  • 25
  • did you try that ip at random? – njzk2 Oct 22 '12 at 12:03
  • No, I used the method get LocalIPAddress() given in http://stackoverflow.com/questions/1720346/how-to-get-the-android-emulators-ip-address – user1741274 Oct 22 '12 at 12:06
  • not going to work. the local ip is on a local network between your emulator and your computer. use your computer address, which, from an emulator, would be 10.0.2.2 and you may need a port redirection – njzk2 Oct 22 '12 at 12:32
  • Can you please explain your comment? – user1741274 Oct 22 '12 at 12:36
  • each emulator is on a virtual network with your computer. it is not accessible from anyone else, unless the proper routes are added. – njzk2 Oct 22 '12 at 14:14
  • Hi, even after redirecting the ports in both emulators, when I execute HTTPPost with IP address 10.0.2.2:5001, I am getting UnknownHostException. Please let me know how to proceed.@njzk2 – user1741274 Oct 23 '12 at 07:51

1 Answers1

0

try this ....

private class WriteToServer extends AsyncTask<Double, Void, Void> {

        private final String serverip = "10.0.2.15";
        private final int serverport = 5001;
        Socket s;
        private DataOutputStream dos = null;

        @Override
        protected Void doInBackground(Double... params) {

            try {

                // open the stream
                s = new Socket("10.0.2.15", 5001);
                dos = new DataOutputStream(s.getOutputStream());

                // write the passed double to the stream
                dos.writeDouble(params[0]);
                dos.flush();

            } catch (Exception e) {
                Log.i("AsyncTank", "something's gone wrong");
            }

            return null;
        }

for more info check... http://developer.android.com/reference/android/os/AsyncTask.html

Parag Ghetiya
  • 421
  • 2
  • 14