Android HttpURLConnection.setChunkedStreamingMode()的默认块长度?

Rap*_*tor 4 android httpurlconnection

HttpURLConnection.setChunkedStreamingMode()的文档中,它说如果我在参数中指定0,它将使用默认的块长度,例如:

conn.setChunkedStreamingMode(0);
Run Code Online (Sandbox Code Playgroud)

默认块长度的确切值是多少?什么是参数的单位?以字节为单位

Tim*_*Tim 11

你的问题让我好奇,所以我测试了一些东西.

默认块长度的确切值是多少?

我在这里发现chunkLength是HttpURLConnection类的受保护变量,这意味着它只能在类本身或子类中访问.所以我创建了一个HttpURLConnection的子类,并尝试打印出chunkLength

class HttpTest extends HttpURLConnection {

    protected HttpTest(URL url) {
        super(url);
        Log.d("CHUNKLENGTH", String.format("%d", this.chunkLength));
        this.setChunkedStreamingMode(0);
        Log.d("CHUNKLENGTH", String.format("%d", this.chunkLength));
    }

    @Override
    public void disconnect() {
        // TODO Auto-generated method stub
    }

    @Override
    public boolean usingProxy() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void connect() throws IOException {
        // TODO Auto-generated method stub

    }
}
Run Code Online (Sandbox Code Playgroud)

这样称呼它

try {
    HttpTest test = new HttpTest(new URL("http://www.google.com/"));
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

这些都是结果

在此输入图像描述

由此,我们可以得出结论,它使用的默认值是1024


参数的单位是多少?

在您发布的链接中,提到了

由于每个块上的头部,较小的块长度会增加必须传输的字节数.

我认为你必须给出字节数是安全的.默认1024也符合该标准