0

I've got an UnknownHostException in my android application when I want to get to my web service.

I inserted an <uses-permission android:name="android.permission.INTERNET" /> at my AndroidManifest.xml.

If I go to my link of webservice via browser it's going fine.

I typed the domain name and IP at the C:\Windows\System32\drivers\etc\hosts file.

But if I run application via emulator I got an UnknownHostException. As I understand, emulator should use my system files to connect with the server...But, practically it doesn't..Help please with this issue.

Nirali
  • 13,571
  • 6
  • 40
  • 53
Rikki Tikki Tavi
  • 3,089
  • 5
  • 43
  • 81

3 Answers3

5

In my case, I encountered this problem when I had started the emulator while I was logged on to a VPN. Logging off the VPN had no effect until I quit the emulator and restarted. It is easy to test if your emulator is not resolving names correctly - just go to the emulator browser and type in a website hostname (anything but Google's).

saswanb
  • 1,975
  • 1
  • 17
  • 25
  • When this happens to me I have to exit the emulator and relaunch it – Someone Somewhere Oct 11 '18 at 20:47
  • 1
    Restarting Android from within the emulator doesn't seem to do it. I had to select "Cold Boot Now" from the device manager in Android Studio. Otherwise it was spot on, my VPN somehow disabled the emulator's Internet access. – Christian Davén Jan 24 '23 at 10:14
1

Problem solved using such domains as

CustomHttpClient.executeHttpPost("http//192.168.1.2/path"); 

Everything works fine with it. I can't edit hosts file in android file system even with root rights. So if you have similar problem - good solution is to use IP addresses, not domains.

Thanks to Haresh Chaudhary

Community
  • 1
  • 1
Rikki Tikki Tavi
  • 3,089
  • 5
  • 43
  • 81
0

A very nice tutorial for connecting with the webservice is here

You will come through this line in this tutorial..

CustomHttpClient.executeHttpPost("http//192.168.1.2/path");  

here path means path of your file placed in htdocs.

Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57