Android, UnknownHostException: www.google.com, on device,not emulator - java

I'm using google api java client for connecting to Google Docs in Android app.
Once in a while i get following exception:
UnknownHostException: www.google.com
Code:
transport = AndroidHttp.newCompatibleTransport();
ClientLogin authenticator = new ClientLogin();
authenticator.authTokenType = "writely";
authenticator.username = username.getText().toString();
authenticator.password = password.getText().toString();
authenticator.transport = transport;
authenticator.authenticate().getAuthorizationHeaderValue();
Device reset clears exception but is it necessary.
I suspected for DNS caching and tried following but didnt work. Im not behind a proxy as far I know.
Security.setProperty("networkaddress.cache.ttl","0");
System.setProperty("networkaddress.cache.ttl","0");
System.setProperty("networkaddress.cache.negative.ttl","0");
System.setProperty("net.eth0.dns1","8.8.8.8");
System.setProperty("net.dns1","8.8.8.8");
Last time it occurred was this morning when one network connection "died", cell auto got second one, tried to do ClientLogin but failed with exception.
Just to make it clear, Im talking about DEVICE (htc desire hd), not emulator.
Thanks in advance.

add this line to your AndroidManifest.xml file, just after the <manifest> tag and before the <application> tag:
<uses-permission android:name="android.permission.INTERNET" />

I run into this daily. I think it is a bug in the emulator. After reset of emulator it works again and it never happened on a real device.
Edit: most definitely a bug in the emulator. See this thread. There are also several posts on StackOverflow regarding those emulator connection issues.

Related

Android App doesn't have internet connection - No Permissions required

Well, I have built an app to store values on a remote database. It works!! I didn't use an emulator for testing instead I used my own phone. Now the problem is that on my phone, it works perfectly...no issues but when I installed it on another phone, the app doesn't connect to the internet.
I have included internet permission in android manifest.xml.
I have tried these but didn't work:
Building apk and installing in the new phone.
Compiling directly to the new phone.
Sending the apk from old phone to new phone via shareit.
Creating a signed apk and installing.
Checked via wifi and mobile data (NOTE: BOTH WIFI AND MOBILE DATA WORKS IN OLD PHONE).
Checked android compatibility: supports up to android 10.
*I added error messages for try...catch blocks in form of toasts for the user to know what's the issue. and the catch exception for no internet returns connection problem. I'm getting that error message.
As I researched, I got to know that internet permission is categorized as normal permission which is not prompted to the user upon installation.
I built a second dummy app: Same issue with it... compiled directly to new phone but didn't work...it works in old phone...
Old phone: Samsung J7 Prime with Android 8.1
New Phone: Samsung J7 Pro with Android 9
Any idea or suggestion will be gladly helpful... Thank you!
in android 9 and above you have to set network Security Config
first of all in res package create xml package and in xml package create new xml resource file with network_security_config name
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
then in manifest in
android:networkSecurityConfig="#xml/network_security_config"
<uses-permission android:name="android.permission.INTERNET" />
Use debug APK instead of signed APK
please add permission on Manifest to give a permission of permission.INTERNET

java.io.FileNotFoundException when trying accessing localhost file in android physical device

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..

android ksoap2 unknown host exception for ip address

my android application communicates with .Net webservice.
i communicate with the web service using the ksoap2 library.
i see that in every time i activating method in the webservice its taking too long.
i debugged to the HttpTransportSE.call() method and i see that in the InetAddress.getHostByAddrImpl(byte[]) method there is an unknown host exception trowed after something like 20 seconds...
its strange because my url is an ip address and there is no dns lookups needed...
this exception trown even when im running this code:
InetAddress.getByName("192.168.191.110").getCanonicalHostName();
can some one explain me how to fix this?
thanks!!
Maybe you forget to add permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
EDIT
Try also this:
java.net.InetAddress[] x= java.net.InetAddress.getAllByName("192.168.191.110") ;
textView.setText("Address: "+x[0].getHostAddress());
I find that my sim card cannot access none of the available dns servers.
so i worked with the system/etc/hosts file and added the mapping there.
before the application is asking the dns server for the address there is a check of the system/etc/hosts file.
after i added the mapping there all worked well.
thanks for the help.
To use IP address you have to use getAllByName() instead of getByName(). See docs here.
So replace
InetAddress.getByName("192.168.191.110").getCanonicalHostName();
with
InetAddress.getAllByName("192.168.191.110").getCanonicalHostName();

: java.net.UnknownHostException: Unable to resolve host "hosturl": No address associated with hostname

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.

Last.fm API call from Android application

I'm trying to make last.fm API call from android application using this package: http://www.u-mass.de/lastfm
From simple java command line program, it works, but not in android application. Using windows and Eclipse
Code itself is very easy:
Artist artist = Artist.getInfo("Depeche Mode", "my_key");
I have set internet permission in android manifest
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
After call I end up here, where it prints "caching failed."
http://code.google.com/p/lastfm-java/source/browse/trunk/src/de/umass/lastfm/Caller.java?spec=svn173&r=173
response code there is -1
int responseCode = urlConnection.getResponseCode();
Any ideas what I'm doing wrong?
I was facing the same problem.
Its a reported bug.
Solution I found was this :
Just add this line somewhere in your code that will get run prior to calling any of the api methods:
Caller.getInstance().setCache(null);
Note: this will disable Caching..
for more info check this link

Categories

Resources