支持我試圖運行簡單的應用程序從Android模擬器訪問互聯網,這裏是我的代碼。我在代理後面並通過「...無線網絡 - > APN - > ...」在模擬器中配置代理設置。但互聯網正在從瀏覽器,而不是從應用程序。java.net.SocketException異常:地址家族不受協議在Android模擬器
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中的部分如下:
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
我試着在論壇上搜索後,下面的選項,但他們都不似乎工作:
在安卓啓動選項我用-http代理服務器:端口-dns服務器服務器
我甲肝Ë列入清單文件權限互聯網
使用許可權的android:NAME = 「android.permission.INTERNET對」
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
設置系統屬性來使用IPv4棧之後有人建議
java.lang.System.setProperty("java.net.preferIPv4Stack", "true"); java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
我正在Android 2.2和windows vista上工作,請你幫我在這裏...
瀏覽器在模擬器中工作正常嗎? – 2012-01-11 12:06:44
@LalitPoptani yes瀏覽器在模擬器中正常工作 – 2012-01-11 12:14:26