Solution to java. io. IOException: Malformed ipv6 exception in android

Source: Internet
Author: User

When the client wants the server to submit information today, the method and parameters displayed as the title are as follows: the input parameter is http: // 192.168.1.173: 8080/Api/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;      }  

 

Return exception: 09-16 09:32:58. 892: I/System. out (416): java. io. IOException: Malformed ipv6 address: [192.168.1.173: 8080] I am wondering, because if I open the browser of the simulator directly, the above URL can be accessed normally. After the help of friends on the Internet, solved this problem, the source solution connection: http://stackoverflow.com/questions/6811482/use-url-on-android-throws-ioexception-malformed-ipv6-address translation is to use HttpURLConnection directly input url will report the above exception, This is a BUG, it should be corrected in later versions. But if you want to call this method now, use URL url = new URL (protocol, host, port, file). Of course, this method, this source solution also has a small BUG. The file parameter must be prefixed. The correct solution should be: URL url = new URL ("http", "192.168.1.173", "8080", "/Api/petinfo? Flag = adopt & json = [{"pettype": "100", "petname": "ge"}] "); then perform the test and pass.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.