java.net.UnknownHostException: Unable to resolve host [LOCAL] - android

I use the INTERNET uses-permission in the manifest file.
If I type URL url = new URL('http://www.example.com/page') (remote server) it will work.
But if I type URL url = new URL('http://example.local/page') (local server) it will return the error
java.net.UnknownHostException: Unable to resolve host "example.local": No address associated with hostname
Now in %WINDIR%/System32/drivers/etc/hosts I wrote this entry :
10.101.0.179 example.local
I can't use new URL('http://10.101.0.179/page') because example.local is a virtualHost in my apache configuration.
Can't my AVD looks into the hosts file ? what should I do to tell android to fetch the ip 10.101.0.179 but still send a proper information to apache and tell it to serve the virtual host example.local ?

If you try to request a web-page from your local HTTP server and the page is bounded to a virtualHost, so let say you need to reach http://example.local/mypage and if as pointed by #CommonsWare you don't use mDNS to resolve inter-local domain-names, android will probably throw the same error message as it reads in the title of this question. Here's the solution I am using :
let's say http://example.local/mypage refers to the ip address 10.101.0.179, Android won't be able to resolve example.local, you need to write the raw url (of your actual local server)
try {
...
URL url = new URL("http://10.101.0.179/mypage");
HttpUrlConnection conn = (HttpUrlConnection) url.openConnection();
...
}
...
But now when Apache receives this request, unless serving your desired host, 10.101.0.179 won't be understood in case of a VirtualHost. To round-about this problem, just modify the Host http-header property using setRequestProperty :
...
conn.setRequestProperty("Host", "example.local");
...
And it should work.

First you must check if you have given INTERNET permission in your manifest file or not, second you make sure that your emulator or computer has a valid internet. If it doesn't work than you can do following:
If you are using localhost url than you must convert that url into IP address to work with App specially using Android emulator.
For example :
My Current url is
http://mylocaladdress:9090/myapp.svc/ForgetPassword?EmailID=test#test.com
Now you can put the following command into terminal on mac or you can use cmd on windows
ping mylocaladdress
The result will give u the IP address and my final url becomes
http://10.1.2.111:9090/myapp.svc/ForgetPassword?EmailID=test#test.com
This url worked perfectly in android emulator without exception.

Related

OkHttp cannot fetch data from localhost

Can anyone please show me the way to fetch data from localhost by OkHttp in Kotlin. When i changed url to https, it worked well
Picture in Logcat for successful one
but when i use http://localhost..., it failed to execute
Picture when failed
EDIT - 8/23/22
It is likely you need to set
android:usesCleartextTraffic="true"
as a flag within the <application> element in the AndroidManifest.xml file.
Old Answer
don't use localhost like this. open command prompt if you are using Windows. type ipconfig and then get Local Ip Address and then use that Ip Address to get data from local host.
same method for Mac. you have to get local ip address

AVD not able to connect to the internet?

I've tried running an app that fetches JSON data from the internet through an HTTP Request object, It showed
java.net.UnknownHostException: Unable to resolve host “api.github.com”: No address associated with hostname
as the Exception for every trial. Later I checked the internet connectivity in the AVD by running a browser. I wasn't able to access any site.
Is there any settings that I'll have to change in the AVD Manager so that I can access the in the Internet through the Virtual Device.
Help me out, Thanks in Advance.
If that is your exact error, I see what's wrong just from that.
Android API's, unlike any modern browser, requires explicit declaration of http or https.
You have to make sure the URL contains either of those protocols which you either can do by adding it into the URL manually, or adding this code before you create the volley request:
if(!url.startsWith("http://") && !url.startsWith("https://")){
url = "http://" + url;
}
You could replace it with HTTPS, but not all sites have https, so it's a generally good idea to default it to HTTP to not get errors from that. If you supply https but there's no HTTPS certificate, the website will most likely refuse the connection.
In my case , I received such error when the Emulator could not connect to the internet.
(Try browsing the internet using the Emulator to test its internet connection. suggested in https://teamtreehouse.com/community/unknownhostexception-unable-to-resolve-host-apidarkskynet-no-address-associated-with-hostname)

Connecting to localhost in Android

I'm developing an App and have designed a bit of Frontend and backend, just the essential. And now, I want to connect both sides. First of all, I need to know how to connect to a localhost in Android.
I tried some tutorials in Internet using for example:
URL url = new URL("http://127.0.0.1");
HttpURLConnection urlConnection = (HttpURLConnection)
url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
I've tried several approaches but all of them fail. Some doesn't do anything and others stop the App showing some Zygote errors.
My question is clear. How can I connect to localhost in Android? I want a function of the style makeConnectionLocalHost() that returns whether the connection has been successful or not. Any idea why nothing works?
Try instead of using "localhost" your local ip. To retrieve it, open CMD and type ipconfig. There you will find your local ip.
127.0.0.1 will create a loop back to your own device.
You can use InetAddress.getLocalHost() to get localhost address. More details here. And for HttpURLConnection you should have Web-server on this address.

Android: HTTP connection to localhost web site

I am developing a tablet application which needs to connect to a web site to collect online content.
I have seen that we can indeed connect to a web server on a local system by addressing it via it's IP address.
However, I am using virtual hosts on my system, so as to use virtual domains.
This is setup on my system in the apache httpd-vhosts.conf file like this -
#
# Project wordpress dev site
#
<VirtualHost *:80>
DocumentRoot "C:/web/www/boutique"
ServerName boutique.project.dev
</VirtualHost>
with my hosts file having the following entry
127.0.0.1 boutique.project.dev # project woocommerce site
I am using the HttpPost and HttpClient classes and I cannot see how I can provide the real IP address whilst still transmitting the host name in the URL request.
So, what I need to know is how can I make queries from my application using the virtual address " boutique.project.dev " ?
Is this possible ?
Edit :
Following one comment, I need to make things more clearer.
I am wanting to do this in code. Each time we make a connection to a site, the URL does a DNS lookup to determine the IP address to use. I need to intercept this and provide the IP address of my local system. I have seen some examples for proxy's using HttpHost, but I am unclear as to how to use this or even if it is relevant.
I think you don't do that unless you modify host file in android device (with root permissions)
This is a similar question: Apache Virtual Host (Subdomain) access with different computer on LAN
You also could setup a DNS server in your computer (i.e bind9) and set entries for your apache virtualhost and configure android in order to use that DNS server.
Here is the solution which I hope will avoid some useless "banging the head against a wall" for those of you who might have the same need.
Two or three steps are necessary.
1st step : Open the firewall to allow connections.
This is obviously necessary or else, whatever you do the connection will not be made. I use Comodo and so I added a rule in the firewall permitting all connections coming from the local LAN.
2nd step : Tell your server, apache in my case, to listen on its IP address.
I added the following entry in my httpd.conf file :
Listen [my ip address]:80
3rd step : Code the connection
The key to getting this to work is to tell which virtual server is needed. So, without further boring details, here is the code :
// setup the needed objects
HttpClient client = new DefaultHttpClient();
// create the request using the IP address of the server machine
HttpGet request = new HttpGet("http://[target ip address]:80/testpage.php");
// here is the code magic - manually set the host header
request.setHeader(HttpHeaders.HOST, "boutique.project.dev");
// now execute the request
HttpResponse response = client.execute(request);
// read back the response as normal
....
The result is that the apache server will map the request to the virtual host.
I hope this will be useful for others here !
Oh, I almost forgot, don't forget to enable the WiFi on your tablet - can save hours of wondering why it doesn't work !

Android emulator cannot connect with local tomcat server with HttpURLConeectioin

I'm doing a project to connect Android with local Tomcat server. But now I'm facing a problem for about 1 week. Who can help me..Thanks very much!
I use the newest android API and Tomcat 7.0. When I start Tomcat server. I can access the Tomcat homepage through emulator browser with url:(http://10.0.2.2:8080). But when I use HttpUrlConnection in the code. I cannot get the successful connection. I also use httpurlconnection to access www.google.com and www.android.com. I cannot connect them either.I use HttpURLConnection.HTTP_OK to see whether it make a successful connection. I also see the Connected is false for HttpURLConnection instance I make.
By the way I also add the internet access permission for it in AndroidManifest.xml before tag.
Thank you very much! Help me. It's emergency.
I recently had this problem. As mentioned above, (on Windows) start cmd, and use ipconfig to find the PC's IP address. Also, due to my Eclipse/Tomcat configuration I needed to have my Eclipse project name in the URL. The URL I used was like this: http://192.168.1.2/myEclipseProject/ServletName
Also, you may need to add the port to the ip address, if your Tomcat still has the default configuration: 192.168.1.2:8080

Categories

Resources