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
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'm having problems with an app that works when connecting to a remote web server, running a php script against a database. However, when I point the same app to my local web server running on my machine, things doesn't work.
Here's the code I use for connecting to the remote web server (it needs authentication):
(All the networking code is done inside an AsyncTask class.)
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringBuilder authentication = new
StringBuilder().append("frankh").append(":").append("vriceI29");
result = Base64.encodeBytes(authentication.toString().getBytes());
httppost.setHeader("Authorization", "Basic " + result);
nameValuePairs.add(new BasicNameValuePair("date", date));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
For the connection to the local server, which doesn't use authentication, I'm commenting out these lines:
//StringBuilder authentication = new
// StringBuilder().append("frankh").append(":").append("vriceI29");
//result = Base64.encodeBytes(authentication.toString().getBytes());
//httppost.setHeader("Authorization", "Basic " + result);
However, I get two different errors, depending on how I phrase the url to the local web server.
If I use this url: "http://localhost.shoppinglistapp/fetchlist.php"
I get this error:
Error in http connectionjava.net.UnknownHostException: localhost.shoppinglistapp
If I skip the http part in the url, I get this error:
Error in http connectionjava.lang.IllegalStateException: Target host must not be null,
or set in parameters.
What am I doing wrong here? The remote server is a Linux Apache server, and the local server is IIS 7. The local server is supposed to be just for working on when I've got no or a bad internet connection, so it's not critical, but I hate not knowing why things doesn't work.
If you testing via your local emulator, you'll want to use 10.0.2.2 instead of 'localhost'.
Referring to localhost from the emulated environment
I'm trying to fetch an image from the AppEngine server in development mode, using this code from here:
HttpGet httpRequest = new HttpGet(URI.create(url) );
HttpClient httpclient = new DefaultHttpClient();
HttpResponse resp = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = resp.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bmp = BitmapFactory.decodeStream(instream);
When executing the 3rd line (httpclient.execute(..)) I get the error:
W/System.err(508): org.apache.http.conn.HttpHostConnectException:
Connection to http://0.0.0.0:8888 refused
This URL http://0.0.0.0:8888 is correct I believe, because in this App Engine connected Android project, I'm able to fetch images with this address on the browser client side of the project. (Again, I'm in local development mode)
Why is this connection being refused? Thanks.
http://0.0.0.0:8888 is certainly not correct. You need to use the IP address of the computer where the App engine development server is running. If you are on a local network that would be something like 192.168.x.xxx. Type ipconfig on Windows or /sbin/ifconfig on Linux in a a terminal to find out the address of your development machine. Then use that to access it from 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