android環境下解決java.io.IOException: Malformed ipv6異常的方法

來源:互聯網
上載者:User

今天做用戶端想服務端提交資訊的時候,報出了如標題所顯示的方法 方法以及參數如下: 輸入的參數為: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"}]"); 然後在進行測試,通過。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.