While I access the localhost servlet from android httpclient utils I am getting connection refused error. The servlet is running fine, but I am getting error.
This is the code:
String dmurl="http://127.0.0.1:9090/DataManagerProject/DMServlet";
HttpClient httpClient = new DefaultHttpClient();
HttpGet hrtreq = new HttpGet(dmurl);
HttpResponse resp = httpClient.execute(hrtreq);
String output = EntityUtils.toString(resp.getEntity());
I am getting following error org.apache.http.conn.httphostconnectexception connection to refused
if you are using emulator to run your app for local server. mention the local ip as 10.0.2.2 and have to give Internet permission into your app
For more details, please refer this link.
Also, Turn Off the firewall and any anti-virus application in your PC
Check whether you have internet access enabled in your emulator and in your application
<uses-permission android:name="android.permission.INTERNET"/>
To access internet data of the device or emulator from your application add the above line in your manifest xml file.Also check the servlet mapping is proper in your the servlet project.
Assuming your servlet runs on your computer the reason you can't get a connection is because your using the wrong IP. 127.0.0.1 is a loopback address meaning that if you connect to 127.0.0.1 on your Android device, it will try to connect to the Android device.
To make it work you have to find the IP address of the computer which runs the servlet and use that in the URL instead.
In addition make sure you have given the Internet permission to your app.
Related
I have made test application to investigate the issue related to http request. It is an android application where I'm making request to localhost:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://127.0.0.1")
.build();
try {
Response response = client.newCall(request).execute();
Log.v("krv", response.toString());
} catch (Exception e) {
Log.v("krv", e.toString());
}
As result on device emulator I have "java.net.ConnectException: Failed to connect to /127.0.0.1:80" exception. With any other request on an external resource(like google.com) all are Ok. Also I have installed IIS. Operating system is Windows 10.
P.S. Really what I need - to be able to make http request on stb device where http server is run(where I have the same problem). I guess both issues have similar reason. Could you give me any advice how to fix/debug my problem...
127.0.0.1 is the loopback address for localhost. But when you're inside an android OS, the localhost or 127.0.0.1 is android itself and that is not where your server is running.
If you want to connect to the host machine, you need to find out the IP address of your host machine (Windows 10) and use that IP address instead of 127.0.0.1.
See How to find your local IP address
However, If you want to refer to the computer which is running the Android emulator, use the IP address 10.0.2.2 instead.
I am new to android and java. I am trying to develop a test app which gets data stored in local database. I am using WAMPP. In emulator I can access it perfectly, but when I try to access it in my physical device (Micromax A311) I get java.io.FileNotFoundException. I have seen many questions asked for this question, but none worked for me.
In my AndroidManifesti have added following code
<uses-permission android:name="android.permission.INTERNET"/>
Url
String url="http://192.168.0.104/selectQuery.php";
which is my IP4 (Wireless LAN adapter Wi-Fi) address.
I have tried it with
http://10.0.2.2/....
/10.0.2.2:80/....
/192.168.0.104:80/
I can access data in browser saved in my database using
/192.168.0.104/ and /192.168.0.104:80/
I am connected physical device via USB to laptop. In my tethering setting I turned USB tethering on.
I am connected with same WiFi which is connected to my laptop.
My proxy setting of laptop is also off.
Can anyone help me to get out of this problem.. I am struck in this from past few days..
Thank you
If selectQuery.php file is in a folder your url is like:
String url ="http://192.168.0.104/name_folder/selectQuery.php";
So if have ./myFolder/selectQuery.php the url is:
String url = "http://192.168.0.104/myFolder/selectQuery.php";
Thanks for your reply... I found a solution myself. I downloaded https://ngrok.com/download, then used its CMD and copy pasted
ngrok http -bind-tls=false site.dev:80
which i found in https://ngrok.com/docs#bind-tls under Tunneling only HTTP or HTTPS. Then I used forwarding address in my url string.
It worked Fine.. I am happy now..
I am trying to build a website parser for one of our internal websites (accessible only from the company network - we get on the network through Cisco AnyConnect VPN).
I can access the site fine in any browser, but not using HTTP requests. Windows network and sharing center shows that I have two active networks:
The actual internet connection
The company network (without internet access).
Default HTTP client gets time out as I suppose it makes a request using the actual internet connection (and the website is not accessible to public), but using this code:
HttpParams params = httpClient.getParams();
params.setParameter(ConnRoutePNames.LOCAL_ADDRESS, InetAddress.getByName("10.x.x.x"));
I get the following error:
I/O exception caught when connectiong to /10.x.x.x -> {s} -> https://zzz.com:443: Network is unreachable: connect
Also, might be a stupid test but I have done a HTTP request to a "what is my ip" site and the IP is shown as my Wifi IP not the IP through VPN (which I get when I open browser and browse to a "what is my ip" website). Same thing (wrong IP) when I try this using a gui-less browser (Jaunt or HTMLUnit).
Please advise if any fixes for this.
ConnRoutePNames appears to be deprecated. See if the following works (I haven't tested):
HttpHost proxy = new HttpHost("10.x.x.x", 80);
HttpRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
CloseableHttpClient httpclient = HttpClients.custom()
.setRoutePlanner(routePlanner)
.build();
I have made an application and used DefaultHttpClient() to access my local server.
The address looks like this: "http://192.168.1.104:8080/login".
I have successfully used it :)
But problem comes when I am in my university and have connected to the network and have an IP address like: "http://130.230.146.148:8080/login", which I am unable to connect and gets the following error:
java.net.ConnectException: failed to connect to /130.230.146.148 (port 8080): connect failed: EHOSTUNREACH (No route to host).
Although on my pc I can access "130.230.146.148:8080/".
How can I solve this problem?
The EHOSTUNREACH (No route to host) error means that a TCP attempt to connect was made and failed because the underlying protocol software did not find a network node to connect to the targeting node.
When you say that when you are in the university and trying to connect it means that you are probably within a private sub-net. So what you could possibly do is check if your Android device is also connected to that sub-net.
Maybe at university you are under a proxy server, try to find out what are proxy server IP and port (if you can access to the URL through your pc-browser, you have to have the browser configured for the proxy). Once you have it, try this
// Proxy server
HttpHost proxy = new HttpHost("your host", your proxy);
// httpClient is your DefaultHttpClient once instantiated
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
// If you need credentials, ignore otherwise
httpClient.getCredentialsProvider().setCredentials(
new AuthScope("your host", your proxy),
new UsernamePasswordCredentials(
"your username", "your password"));
Hope helps!
P.D: As others user says HttpClient is deprecated, you may consider move to HttpUrlConnection
I have android application which connect to "hosturl" over the web. This application can connect to hosturl in the initial steps, but after i try to test it for some time(say 20 or more requests), I get above exception and I can not no longer connect to above url.
If i restart my android handset, then application can again connect to the "hosturl" but again I get the exception after I have tried to connect to hosturl few number of times
Could anybody help me here If you have encounter such a behavior with an android application before.
You probably don't have the INTERNET permission. Try adding this to your AndroidManifest.xml file, right before </manifest>:
<uses-permission android:name="android.permission.INTERNET" />
I met this and I just closed my AVD and start it again and it worked...
Check your Internet connection.
Check Wifi.
Check Server.