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"}]");

    然后在进行测试,通过。

  • 相关阅读:
    vue 传参动态
    a href="tel" 拨打电话
    vue中rem的转换
    请求接口的封装
    http request 请求拦截器,有token值则配置上token值
    node溢出
    vue菜单切换
    vue的table切换
    vue页面初始化
    [论文笔记] Legacy Application Migration to the Cloud: Practicability and Methodology (SERVICES, 2012)
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3327661.html
Copyright © 2011-2022 走看看