error connection refused - android

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

Related

Android localhost url is not working for tomcat 7

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.

using http to invoke my apache webservice on android

I'm working on an android application which allows users to create accounts on a database via my tomcat webservice. I've tested the webservice with a browser and it works fine. So I've tried to use make Http requests to that URL with my andriod app but it doesn't work.
Here is my code
try{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI webservice = new URI("http://my-machine-ip:8080/Login/login?username=jimmy&password=javajava");
request.setURI(webservice);
HttpResponse response = client.execute(request);
}catch (Exception e1) {
e1.printStackTrace();
}
I have also set the internet permission. <uses-permission android:name="android.permission.INTERNET" />
Checking my log I have a connection error
05-31 17:46:50.299: W/System.err(17395): org.apache.http.conn.ConnectTimeoutException: Connect to /192.168.0.3:8080 timed out
enter code here
Apparently, this was a connection problem caused my my firewall. A quick reconfiguration of applications with access on the firewall has solved everything
use your response object to get getEntity().getStatusLine() then get getStatusCode() and getReasonPhrase().
Check that the status is 200 and the reason is OK.

Android MySQL Connection

I am trying to connection with php-MySQL using below code
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://127.0.0.1/mytable.php");
// HttpPost httppost = new HttpPost("http://localhost/mytable.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
when control goes on httpclient.execute(httppost); it throws an exception:
org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused
I also add <uses-permission android:name="android.permission.INTERNET" /> to my AndroidManifest.xml file.
When I test my php file directly from a browser, it works fine. Any suggestions?
From the emulator, il you want to access your physical computer, you must use the following IP address :
10.0.2.2
The one you used, 127.0.0.1, points to the machine inside which your code is executed -- i.e. the emulator itself.
As you don't have an HTTP server running on your Android emulator, your application cannot connect to it -- which explains the error message you get.
For more informations, you should read the following section of the manual : Network Address Space
127.0.0.1 is localhost, the current device. On your computer, it refers to your computer, but on a device or an emulator it refers to that device.
See this document on how to refer to the localhost of your computer when running within an emulator. Alternatively, you can change 127.0.0.1 to your computer's actual IP address.
http://developer.android.com/resources/faq/commontasks.html#localhostalias

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.

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.

Categories

Resources