Eldelshell
Eldelshell

Reputation: 6940

Android HTTP Connection

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

Upvotes: 18

Views: 20007

Answers (2)

Simon Jeocks
Simon Jeocks

Reputation:

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.

Upvotes: 2

Reto Meier
Reto Meier

Reputation: 96916

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>

Upvotes: 32

Related Questions