Android Question Simply ResolveHost on 4.0+

Shark812

Member
Licensed User
Longtime User
Here is my situation:
I need to resolve a host(web address, ex. basic4ppc.com) to it's current IP.
I know I can use the hostname when connecting with sockets but in this case I really just want to get the current IP without having to use sockets to make a connection if possible.
I can do this easily by using this in versions below Android 3.0:

B4X:
Sub ResolveHost(Host As String) As String
  Dim r As Reflector
  r.Target = r.RunStaticMethod("java.net.InetAddress", "getByName", Array As Object(Host), _
  Array As String("java.lang.String"))
  If r.Target = Null Then Return "N/A"
  Return r.RunMethod("getHostAddress")
End Sub

But obviously I get thrown a NetworkOnMainThreadException in versions 3+.
I also know I can set the target version to be lower than 11 to fix this but I'd really like to keep up with the newer SDK's.

Doing some more research led me to the Threading Library and I may try going this route, but it seems a bit much just to get a hostname resolved...right?

My question is, is there an easier way to get the IP of a Host (on 3.0+) without using a lower SDK or using the Threading library?
If I'm missing a simpler way, please explain!
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…