美文网首页
java.net.URLConnection在Android平台

java.net.URLConnection在Android平台

作者: 祥龙翔天 | 来源:发表于2023-07-28 15:50 被阅读0次

    引言:最近在做网络访问功能,用到了失败重传,效果却不理想。其问题点在于,对同一个地址连续访问多次,如果第一次失败花了几秒钟的时间,后续访问失败根本不用花几秒(甚至到了几毫秒)就结束。一度让我怀疑,我应不应该做失败重传,好在经过我的实验,找到了问题所在,并得出了相应结论以及解决办法。但更为深层次的原因,还需要继续探究。所以现在跟我一起看看是怎么回事儿吧!

    既然是失败重传,那么就得创造网络访问失败的环境,这个环境是这样:手机正常连接路由器WiFi,将路由器WAN口的网线拔掉,即让路由断网。

    环境准备好了,就开始上代码运行,代码详情如下

    println("lr------ programming launch")
    for (i in 0 until 5) {
        val start = System.currentTimeMillis()
        var connection: HttpURLConnection? = null
        try {
            connection = URL("https://www.baidu.com").openConnection() as HttpURLConnection
            connection.connectTimeout = 2000
            connection.readTimeout = 2000
            connection.connect()
        } catch (e: Exception) {
            println("lr------ err:$e")
        } finally {
            connection?.disconnect()
        }
        println("lr------ running time: " + (System.currentTimeMillis() - start) / 1_000.0 + "s")
    }
    println("lr------ programming end")
    

    上述代码进行了5次同一个地址的网络连接请求,我们看看在我们创造网络访问失败的环境的情况下的打印输出

    lr------ programming launch
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.043s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 0.005s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 0.005s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 0.004s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 0.004s
    lr------ programming end
    

    可以看到,第一网络连接请求花了16s的时间,后续4次都只是花了几毫秒,反复测试也是如此。后几次的访问看上去就是简单复制了前一次的错误信息,根本没有去真实的访问网络,或者说,第一次的连接或许没有断开过,即便调用了connection?.disconnect()

    如何解决这个问题呢,有了上面的猜想,我决定在每次网络访连接请求后都延时1200毫秒,于是得到如下代码

    println("lr------ programming launch")
    for (i in 0 until 5) {
        val start = System.currentTimeMillis()
        var connection: HttpURLConnection? = null
        try {
            connection = URL("https://www.baidu.com").openConnection() as HttpURLConnection
            connection.connectTimeout = 2000
            connection.readTimeout = 2000
            connection.connect()
        } catch (e: Exception) {
            println("lr------ err:$e")
        } finally {
            connection?.disconnect()
        }
        println("lr------ running time: " + (System.currentTimeMillis() - start) / 1_000.0 + "s")
        TimeUnit.MILLISECONDS.sleep(1200)
    }
    println("lr------ programming end")
    

    运行后,打印输出如下

    lr------ programming launch
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.037s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 0.002s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.029s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 0.006s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.029s
    lr------ programming end
    

    通过这次打印输出,我发现其规律,无论我尝试多少次,花费时间是间隔的,即前一次花了16秒,后一次就花几毫秒。看上去情况有所改善,猜想是延时时间设置小了,于是我改成了2000毫秒,再次运行后,打印输出如下

    lr------ programming launch
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.195s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.029s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.03s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.033s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associated with hostname
    lr------ running time: 16.031s
    lr------ programming end
    

    可以看到,几次网络连接请求花费的时间都是16秒左右,看上去这个问题解决了。但前面提到,网络连接请求同一个网络地址的时候,会出现这个问题,那连续网络连接请求的是不同的网络地址会是一个怎样的情况,我们执行下面的代码来看情况

    lr------ programming launch
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu0.com": No address associated with hostname
    lr------ running time: 16.044s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu1.com": No address associated with hostname
    lr------ running time: 16.032s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu2.com": No address associated with hostname
    lr------ running time: 16.029s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu3.com": No address associated with hostname
    lr------ running time: 16.031s
    lr------ err:java.net.UnknownHostException: Unable to resolve host "www.baidu4.com": No address associated with hostname
    lr------ running time: 16.031s
    lr------ programming end
    

    看上去问题也得到解决了,此外,像Unable to resolve host "xxx"这类错误,整个网络连接请求流程没有完全建立,为什么这么说呢,因为抓包是抓不到滴!!!

    相关文章

      网友评论

          本文标题:java.net.URLConnection在Android平台

          本文链接:https://www.haomeiwen.com/subject/rporpdtx.html