zoukankan      html  css  js  c++  java
  • android环境下解决java.io.IOException: Malformed ipv6异常的方法

    今天做客户端想服务端提交信息的时候,报出了如标题所显示的方法

    方法以及参数如下:

    输入的参数为: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"}]");

    然后在进行测试,通过。

  • 相关阅读:
    面向对象与组合
    异常处理和三级菜单练习
    装饰器和生成器
    序列化模块
    leetcode_498. 对角线遍历
    leetcode_566. 重塑矩阵
    leetcode_59. 螺旋矩阵 II
    leetcode_54. 螺旋矩阵
    leetcode_396. 旋转函数
    leetcode_200. 岛屿数量
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3327661.html
Copyright © 2011-2022 走看看