Android的HTTP连接Android、HTTP

2023-09-07 03:21:56 作者:你若离去巴掌扇去

谁能告诉我为什么不能在Android模拟器的工作?从浏览器我有机会和服务器内部。所有我能想到的是,我很想念我的应用程序的一些配置,以便它可以访问网络层。

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");
}

抛出一个UnknownHostException

感谢

推荐答案

至于配置的推移,唯一的设定,你应该需要从应用程序访问互联网是互联网的权限,通过添加以下行启用的外的应用程序中的应用程序标签清单。

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>