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.
Related
This is my code and it works perfectly in an emulator but some reason, this problem occurred in on a real device.
Error in http connectionjava.net.UnknownHostException: my.url.com
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost http = new HttpPost("http://my_url.com/folder/login.php");
http.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());
}
Error in http connectionjava.net.UnknownHostException occurs
if the URL is not recognizable
or
if the client does not have sufficient permissions to call the URL
or
if the client is restricted via firewall to access the url.
To avoid it, check the following:
Check if my.url.com is public URL available for internet use.
Check if internet(GPRS) and/or wi-fi connection available in the mobile phone.
If it is a private URL, then ensure that your mobile is connected to the private network same as the emulator via wi-fi.
Your code seems good
can you please check you are not missing to grant to internet access for your device
permission set in your Android manifest:
add the following permission that will help you charm...
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
I am trying to connect to my database on my localhost through my android application. I have created a php file and stored it in my wamp/www/myfolder, when I run it it works fine, so it must be a problem with my android class.
public void getData(){
String result ="";
InputStream isr = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost/testdata/getAllCustomers.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
isr = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection");
resultView.setText("Couldnt connect to database");
I have gone over my code many times but I can't find the problem.
I have created two morw try catches in this method that convert the result and the other one parses the data but when I run it the first log appears so it doesn't connect to the database.
P.s I am using eclipse, to I need a plugin for json, I know eclipse IDE supports it.
NetworkOnMainThreadException means that you are attempting to perform a network operation on the main thread, which is forbidden. Simply do it from another thread. This is mainly done by creating an AsyncTask. Here's an example. And here's another one.
It's worth noting that NetworkOnMainThreadException is only thrown for applications targeting the Honeycomb SDK or higher, but the behaviour is always heavily discouraged.
The loopback address in case of android when you are trying to connect from emulator is 10.0.2.2
i.e instead of localhost use 10.0.2.2
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
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.
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