问题描述
我试图运行简单的应用程序从Android模拟器访问互联网,这里是我的code。我身后的代理,并在仿真器配置代理服务器设置由......无线网络 - > APN - > ...。但是,互联网是从浏览器的工作,而不是从应用程序。
I am trying to run simple application to access internet from android emulator and here is my code. I am behind proxy and configured proxy settings in emulator by"...Wireless Networks -> APN -> ..." . But internet is working from browser and not from application.
HttpURLConnection connection = null;
String URLName = "http://www.google.com";
try {
URL u = new URL(URLName);
connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("HEAD");
int code = connection.getResponseCode();
Log.d(TAG1, " " + code);
// You can determine on HTTP return code received. 200 is success.
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d(TAG1, e.toString());
}
finally {
if (connection != null) {
connection.disconnect();
}
}
错误日志中的logcat的部分如下:
The part of error log in Logcat is as follows:
01-11 01:01:26.308: DEBUG/SntpClient(72): request time failed: java.net.SocketException: Address family not supported by protocol
01-11 01:02:39.909: WARN/System.err(422): java.net.SocketException: The operation timed out
我试过下列选项中搜索论坛后,但他们都不似乎工作:
I tried the following options after searching in forum but none of them seems working:
-
在Android的启动选项我用-http代理服务器:端口-dns服务器服务器
In android launch options I used -http-proxy server:port -dns-server server
我已经包括在清单文件权限互联网
I have included internet permissions in manifest file
使用许可权的android:NAME =android.permission.INTERNET对
uses-permission android:name="android.permission.INTERNET"
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
设置系统属性来使用IPv4协议栈有人建议在
Set the system properties to use ipv4 stack after someone suggested
java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
我的工作在Android 2.2和Windows Vista中,你可以请帮我在这里...
I am working on Android 2.2 and windows vista, can you please help me here...
推荐答案
请试试这个:
HttpURLConnection con =null;
URL url = new URL("abcdefg");
Proxy proxy=new Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(android.net.Proxy.getDefaultHost(),android.net.Proxy.getDefaultPort()));
con = (HttpURLConnection) url.openConnection(proxy);
这篇关于java.net.SocketException异常:地址族不是由Android模拟器支持协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!