views:

6108

answers:

3

Can anybody tell my why this doesn't work in the Android emulator? From the browser I have access and the server is internal. All I can think of is that I'm missing some configuration on my app so it can access the network layer.

try {
    InetAddress server = Inet4Address.getByName("thehost");
    //Doesn't work either
    //or InetAddress server2 = Inet4Address.getByAddress(new String("192.168.1.30").getBytes());

    if(server.isReachable(5000)){
        Log.d(TAG, "Ping!");
    }

    Socket clientsocket = new Socket(server, 8080);
} catch (UnknownHostException e) {
    Log.e(TAG, "Server Not Found");
} catch (IOException e) {
    Log.e(TAG, "Couldn't open socket");
}

Throws an UnknownHostException

Thanks

+16  A: 

As far as configuration goes, the only setting you should need to access the Internet from your application is the INTERNET permission, enabled by adding the following line outside the Application tags within your application Manifest.

<uses-permission android:name="android.permission.INTERNET" />

So the manifest would follow this general construction

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.apis">    
  <uses-permission android:name="android.permission.INTERNET" />
  <application 
    android:name="MyApplication"    
    android:label="@string/application_title"
    android:icon="@drawable/my_icon">
    [ .. Your Activities go here ]
  </application>
</manifest>
Reto Meier
That was it. Thank you very much.
Ubersoldat
Excellent! I love it when it's the easy solutions.
Reto Meier
Wow! This thing has reached 3k views... that should give you lots of SO points Reto
Ubersoldat
A: 

It might still not work, because of the timeout. Since you need root permissions to send an ICMP Package and the implemetation of isReachable will use the slow TCP version of ECHO. Chekcout the javaDoc.

Simon Jeocks
A: 

thanks a ton...i have been scratching my head over this for a almost an hour...

Dhrumil
Please use the comment feature for "thx" and "cheers". Answers should, well, be actual answers
SealedSun