在Android上使用URL会抛出IOException:格式错误的ipv6地址

cht*_*cht 3 java android

我想将android模拟器上的数据发送到本地主机网站,并获得一些结果.

String temp = "http://10.0.2.2:8888/json/rec?user_data=" + user_data + "&friends=" + friends;
URL url = new URL(temp);

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(5000);
InputStreamReader is = new InputStreamReader(urlConnection.getInputStream(), "UTF-8");
String output = "";
while(is.ready()) {
    output += is.read();
}
Run Code Online (Sandbox Code Playgroud)

这是例外.

java.io.IOException: Malformed ipv6 address: [10.0.2.2:8888]
Run Code Online (Sandbox Code Playgroud)

为什么这么说?有人能帮助我吗?提前致谢.

Kal*_*Kal 5

它是未来版本中修复的已知错误.

http://code.google.com/p/android/issues/detail?id=12724

简单的解决方法是对URL使用不同的构造函数..接受主机名,端口和文件的构造函数

URL(String protocol, String host, int port, String file)
Run Code Online (Sandbox Code Playgroud)

编辑

在你的情况下,它会

URL url = new URL("http", "10.0.2.2" , 8888 , "json/rec?user_data=" + user_data + "&friends=" + friends);
Run Code Online (Sandbox Code Playgroud)