今天做用戶端想服務端提交資訊的時候,報出了如標題所顯示的方法 方法以及參數如下: 輸入的參數為:http://192.168.1.173:8080/Api/petinfo/petinfo?flag=adopt&json=[{"pettype":"100","petname":"ge"}]
public static InputStream getInputStreamFromUrl(String urlstr){ try { InputStream is = null; HttpURLConnection conn = null; System.out.println("urlstr:"+urlstr); URL url = new URL(urlstr); conn = (HttpURLConnection) url.openConnection(); if (conn.getResponseCode() == 200) { is = conn.getInputStream(); return is; } } catch (Exception e) { System.out.println(e.toString()); } return null; }
返回異常:09-16 09:32:58.892: I/System.out(416): java.io.IOException: Malformed ipv6 address: [192.168.1.173:8080]很納悶,因為我如果直接開啟模擬器的瀏覽器,輸入以上的網址是能正常訪問的。後來經過網上的朋友的協助,解決了這個問題,源解決方案串連: http://stackoverflow.com/questions/6811482/use-url-on-android-throws-ioexception-malformed-ipv6-address 翻譯過來就是使用HttpURLConnection時直接輸入url就會報上述的異常,這是一個BUG,在以後的版本中應該會被改正。 但是如果現在就想調用這個方法的話,那麼就使用URL url = new URL(protocol, host, port, file);這個方法 當然,這個源解決方案中也有一個小小的BUG,file這個參數前面是需要加上/的。 正確的解決方式應該是: URL url = new URL(“http”,"192.168.1.173", "8080", "/Api/petinfo/petinfo?flag=adopt&json=[{"pettype":"100","petname":"ge"}]"); 然後在進行測試,通過。