3

I am trying to interact from android activity being tested on phone(USB) to a Servlet in Apache Tomcat7 on my laptop. I am creating a button. clicking on which I should go to the next "activity". However when I try to contact the server in between, it stops responding and does not go to the next page. I am working on Ubuntu as my laptop OS and OnePlus as my phone, if there is a need for this information.

Following is the code for Android activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent);

    Button submit = (Button) findViewById(R.id.button1);

    submit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            new Login1().execute();
        }
    });
}

private class Login1 extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... paramss) {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "http://192.168.1.100:8080/Server1/Server";

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("u", "username"));
        params.add(new BasicNameValuePair("p", "pass"));

        HttpPost httpPost = new HttpPost(url);
       Log.i("httpPost","success");
       try {

           httpPost.setEntity(new UrlEncodedFormEntity(params));
           Log.i("setEntity", "success");
           HttpResponse httpResponse = httpClient.execute(httpPost);
           Log.i("execute", "success");

           Intent login = new Intent(getApplicationContext(), act2.class);
           startActivity(login);

        } catch (ClientProtocolException e) {
           e.printStackTrace();
           Log.i("Exception","ClientProtocolException");
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
           Log.i("Exception", "UnsupportedEncodingException");
       } catch (IOException e) {
           e.printStackTrace();
           Log.i("Exception", "IOException");
       }

        return null;
    }
}

Following is my doPost method on the servlet.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String name=request.getParameter("u").toString();  
        String password=request.getParameter("p").toString();
        response.getWriter().write(name+password); //working
    }

This is what I have tried: I made a local java file on the laptop. Tried to contact with the apache tomcat7. It can contact and get the desired result. However my android activity cannot contact with the server. It does not pass the control to the act2 Activity.

I am in the learning phase so I have tried to keep things as simple as possible.

Additional Info: I am connected with wifi internet, home network, if required .

logcat output:

> 05-22 04:42:16.368  21511-23633/com.example.bhavya.sampleintent I/httpPost﹕ success
05-22 04:42:16.369  21511-23633/com.example.bhavya.sampleintent I/setEntity﹕ success
05-22 04:43:19.575  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.100:8080 refused
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at com.example.bhavya.sampleintent.IntentAct$Login1.doInBackground(IntentAct.java:117)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at com.example.bhavya.sampleintent.IntentAct$Login1.doInBackground(IntentAct.java:99)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /192.168.1.100 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:124)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ ... 14 more
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.Posix.connect(Native Method)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ ... 19 more
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent I/Exception﹕ IOException

I tried with local mobile hotspot network, disconnecting it from internet. But it does not work.

Note: I am not working on emulator. I am working on USB debugging.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bhavya Arora
  • 768
  • 3
  • 16
  • 35
  • add some log for every step and exception. – Jiang YD May 22 '15 at 08:07
  • added. Log.i("execute", "success"); does not returns output at logcat. The server does not responds as mentioned :( :( – Bhavya Arora May 22 '15 at 08:45
  • try to access the same from your google chrome from mobile ? did you get that ? i think the answer is no – King of Masses May 22 '15 at 10:16
  • Why are you redirecting to new activity in "doInBackground" method...**Intent login = new Intent(getApplicationContext(), act2.class);startActivity(login);** Do this redirecting in **onPostExecute()** Also you can log your http response String responsedata = EntityUtils.toString(response.getEntity()); – m0rpheu5 May 22 '15 at 10:19

1 Answers1

3

So, through your machine your accessing the local host but your not able access the same from android device.

It means your android device your using 2G/3G or WIFI connection to hit the localhost. If you used localhost for connection that time you must be in the same network, then only you can access the localhost.

But in emulator your in the same network (probably local host network) that's why it is working.

Finally the possible solutions for your approach is

  1. Use the same network (local host network) in your android device also. Means connect with Wifi to same local host internet connection. This time 2G/3G connections won't work, as they are not in same network
  2. Deploy the build some where externally and access the external ip address for connection. this time both 2G/3G or WIFI will work
King of Masses
  • 18,405
  • 4
  • 60
  • 77
  • I never said I tested it on emulator. I made a local Java file to test the connection and get the reply back. I cannot test on emulator because the machine is only 4 GB RAM and goes really slow. It does not work even with mobile hotspot, disconnecting it from internet. – Bhavya Arora May 22 '15 at 09:42
  • Try to use the same network i.e where your deployed your server (host) application. Then it will work :) – King of Masses May 22 '15 at 09:57
  • 1
    Tested on Emulator, it works there. But not through my phone. I am using the same network :( Tried on home 1) home wifi. 2) creating a mobile hotspot adhoc network. :( :( – Bhavya Arora May 22 '15 at 09:57
  • Yes. So, now you got my point , your emulator (system) and your server (host) both are in a same network. so that's why it is working. So try to create a WIFI connection with the same network and connect it from mobile then it will work :) – King of Masses May 22 '15 at 09:59
  • Not home wifi ? which network you used to run your host, same should be used for mobile connection too – King of Masses May 22 '15 at 10:03
  • What do you mean by which network? my host is my laptop having IP of the form 192.168.x.x they are connected through wifi(wlan0). My phone also has the IP of the form 192.168.x.x. They both are connected to the same wifi network. What am I missing? – Bhavya Arora May 22 '15 at 10:12
  • 1
    That is the problem what your doing here. you must use a LAN connection to your laptop and after that you need to share the wifi (same internet) from your laptop to your mobile.. then it will work – King of Masses May 22 '15 at 10:15
  • 1
    yes it works! Thanks so much!! Do you know why it does not work with mobile hot spot network, disconnecting the internet. It should be able to pass the messages in that way? For the home wifi thing, I guess it tries to find over the internet. But why ad-hoc network fails when all share the same network. – Bhavya Arora May 22 '15 at 10:53