2011-07-25 32 views
3

我想將android模擬器上的數據發送到本地主機web,並獲得一些結果。在Android上使用URL會拋出IOException:格式錯誤的ipv6地址

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

這裏是例外。

java.io.IOException: Malformed ipv6 address: [10.0.2.2:8888] 

爲什麼這麼說? 有人可以幫我嗎? 在此先感謝。

回答

5

它是一個在未來版本中已知的已知bug。

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

最簡單的解決方法是使用不同的構造函數的URL ..接受主機名,端口的一個和文件

URL(String protocol, String host, int port, String file) 

編輯

在你的情況下,它會be

URL url = new URL("http", "10.0.2.2" , 8888 , "json/rec?user_data=" + user_data + "&friends=" + friends); 
+0

嗨,謝謝。我已經閱讀了JAVA文檔的URL。我有一些問題是,當我這樣設置網址,所以:協議= HTTP,主機= 10.0.2.2,端口= 8888,然後文件=? – cht

+0

編輯答案..希望有所幫助。 – Kal

0

可能因爲主機包含冒號,這意味着這是一個數字的IPv6地址

相關問題