Android的HTTP连接 [英] Android HTTP Connection

查看:101
本文介绍了Android的HTTP连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么不能在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>

这篇关于Android的HTTP连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆