Android localhost url is not working for tomcat 7 - android

I am using tomcat 7 from Java EE eclipse i also made a code for server part. In android i also made a project and ran it and using HttPGet of server part Url but my getRequest is not working.
My server is working in that Url
http://localhost:8080/dhoom/index.jsp
And in my android project i used like this:
String URL = "http://10.0.2.2:8080/dhoom/index.jsp";
HttpClient user = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(URL);
getRequest is not working need help thanks in advance.

Open your system's command prompt and run ipconfig command on prompt, you will find your local/public ip here this ip will depend upon your internet connectivity . If you are trying to access web application on lan then use above local ip with http port that should be ip:8080.
If you want to use on wan then you have to a static IP to access web application in your mobile application.

Related

Unable to contact host machine (10.0.2.2) from emulator

I have a python REST API server running on my laptop. I am trying to write a rest client in Android (using Eclipse ADT etc) to contact it using Apache's client (org.apache.http.client) libraries.
The code is really simple, and basically does the following -
HttpGet httpget = new HttpGet(new URI("http://10.0.2.2:8000/user?username=tim"));
HttpResponse response = httpclient.execute(httpget);
However at execute, it exceptions out with a time out exception. I cannot hit the URL even from the browser in the emulator.
Details of the exception
org.apache.http.conn.ConnectTimeoutException: Connect to /10.0.2.2:8000 timed out
However, I tried using the cREST client on Chrome on my laptop, and I am able to query the REST server fine.
Is it possible the machine is not on your network? Ie - it is on the other side of a router, on the internet somewhere? Because addresses starting "10." are reserved as private addresses and not routable.
See http://en.wikipedia.org/wiki/Private_network for more info
I had the same issue and here's how I figured out the cause:
Quit you emulator. Start your local server. In the browser, you should be able to access "http://localhost:8000/user?username=tim" and get some response. If you get a timeout, your server is likely not running. In my case, my python server had a break-point set and it was stuck there. Once I let it run, I was able to see responses on the browser and subsequently in the emulator (using 10.0.2.2).

problem in connecting to my EC2 instance from android simulator

i am facing strange problem i deployed a webservice in EC2 which is up and working fine, but when i am trying to do a REST request on these service I am getting java.net.SocketException: The operation timed out. When i am trying the same URL in my laptop browser it is working but when i am trying this in Android emulator browser it is giving me error.
here is my code
REST URL: http://122.248.194.88:8080/data_for?train=12657
code:-
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://122.248.194.88:8080/data_for?train=12658");
// Execute the request
HttpResponse response;
try {
response = httpclient.execute(httpget);
`
this httpclient.execute is returning the socket exception is there anything more which i need to know??
I am behind a proxy though but i set that proxy in eumulator options while starting the emulator.
I used the example here: http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html to consume a JSON string. It works perfectly.

Android Emulator not connecting with localhost Apache

I am developing an app in Android, and the emulator is not connecting to my localhost.
I have researched the issue and found some ideas but nothing seems to be working.
Below is the code i have been using:,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/YES_BUTTON.php");
httpclient.execute(httpPost);
I have also tried a connection using "http://10.0.2.2:3128/YES_BUTTON.php";
The code would run a php file, but nothing happens, I have also checked the server access log and no connection was ever made with the Apache server from the emulator.
This is my first time building an app, and was wondering if there was something I haven't done, do I need to map the emulator port with the 3128 port on Apache, disable the firewall? I am developing the app on a laptop,with the Apache Server on the laptop also, so everything is being done on the same machine. Is there anything I havent done that needs to be done?
I bet you forgot to add the INTERNET permission :)
INTERNET

Why am I Not able to send http request over WIFI to a machine on the local network from my android device

I am on WIFI and my Android device is connected.
In the android device I open a browser and go to http://192.168.1.12 - This is the IP address of the machine/server on the local network. I get the home page from this machine into my browser on the android device (because there is web server installed and it is a server machine).
Through my Java program - HttpPost is working very well for external sites like http://www.yahoo.com through WIFI - but it is NOT working for a server on the LAN
HttpClient hc = new DefaultHttpClient();
HttpPost post = new HttpPost("http://192.168.0.12/");
HttpResponse rp = hc.execute(post);
The above code DOES NOT work and http response status 404. But if I change the above url to say "http://www.yahoo.com" it works and response status is 200
Can anyone help please
The machine's IP is 192.168.1.12, but you're using HttpPost with 192.168.0.12.

error connection refused

I want to make an Http Connection to my own servlet. Here is my code:
try
{
HttpClient client = new DefaultHttpClient();
HttpPost httpMethod = new HttpPost("http://localhost:8080/getHeader/HeaderServlet");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(httppost, responseHandler);
String result = response.toString();
}
But i'm unable to, and I get the error:
org.apache.http.conn.HttpHostConnectionException:Connection to http://localhost:8080 refused
I will be thankful your help
Use 10.0.2.2 instead of localhost.
If you are referring to a localhost from your device than use the http://10.0.2.2/ instead of the http://127.0.0.1/ or http://localhost/.
Because your Android emulator is running on a Virtual Machine(QEMU) and you can not connect to a server directly running on your PC.
So your code snippet will be like this:
HttpPost httpMethod = new HttpPost("http://10.0.2.2:8080/getHeader/HeaderServlet");
Refer this : Emulator Networking for more information.
I had the same problem but I solved it by putting in the following label said
<uses-permission android:name="android.permission.INTERNET" />
which allowed me to connect to the Internet.
The better is that you put your PC LAN's IP , for example , in windows , run "ipconfig" in a cmd console , suppose that your IP is : 192.168.1.34 ,
then
HttpPost httpMethod =
new HttpPost("http://192.168.1.34:8080/getHeader/HeaderServlet");
localhost would be the Android device itself. I assume that this is not where your servlet is. You'll need to enter the hostname or IP of wherever your servlet is.
(If it's really on your device (why?!), then you need to make sure you have the INTERNET permission. You could try connecting to it from the built-in browser.)
For me problem solved by deleting proxyHost,proxyPort, etc from gradle.properties

Categories

Resources