I am trying to communicate with a servlet on my laptop via android activity on my phone
I am trying to interact with Android phone (USB) validation on a servlet in Apache Tomcat7 on my laptop. I am creating a button. by clicking on which I have to go to the next "action". However, when I try to contact the server in between, it stops responding and doesn't go to the next page. I am working on Ubuntu as my laptop operating system and OnePlus as my phone if there is a need for this information.
Below 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;
}
}
Below is my doPost method on a 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 tried: I made a local java file on my laptop. Tried to contact tomcat7 apache. He can contact and get the desired result. However, my android activity is unable to communicate with the server. It does not transfer control to act2.
I'm in the learning phase, so I've tried to keep things as simple as possible.
Additional information: I am connected to 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 area network of mobile hotspots, disconnecting it from the internet. But it doesn't work.
Note. I am not working on an emulator. I am working on USB debugging.
source to share
So, through your machine, you are accessing the localhost, but you cannot access it from your Android device.
This means your Android device is using a 2G / 3G or WIFI connection to get to the localhost. If you used localhost to connect, then you must be on the same network, then you can access the localhost.
But in the emulator yours is on the same network (possibly the host's local network) that's why it works.
Finally, possible solutions for your approach:
- Use the same network (host local network) in your android device as well. Means of communication with Wi-Fi in the same local computer connection. 2G / 3G connections won't work this time as they are not on the same network
- Expand the assembly where you are outside and access the external ip address to connect. this time 2G / 3G or WIFI works.
source to share