1

Android アプリで独自のデータ使用量を追跡したいと考えています。HTTP 応答の Content-Length を取得できますが、送信前に要求のサイズを取得する方法がわかりません。すべてのリクエスト (GET、POST、PUT など) は のインスタンスですHttpUriRequest

ありがとう

4

2 に答える 2

3

コンテンツを含むすべてのリクエストは、 のサブクラスである必要がありHttpEntityEnclosingRequestBaseます。

HttpUriRequest req = ...;
long length = -1L;
if (req instanceof HttpEntityEnclosingRequestBase) {
    HttpEntityEnclosingRequestBase entityReq = (HttpEntityEnclosingRequestBase) req;
    HttpEntity entity = entityReq.getEntity();
    if (entity != null) {
        // If the length is known (i.e. this is not a streaming/chunked entity)
        // this method will return a non-negative value.
        length = entity.getContentLength();
    }
}

if (length > -1L) {
    // This is the Content-Length. Some cases (streaming/chunked) doesn't
    // know the length until the request has been sent however.
}
于 2012-05-27T19:15:31.177 に答える
0

このクラスは、 というメソッドを持つクラスHttpUriRequestから継承されます。この関数を呼び出し、メソッドを呼び出してから関数を呼び出して、要求の長さを見つけることができます。HttpRequestgetRequestLine()toString()length()

例:

HttpUriRequest req = ...;
int reqLength = req.getRequestLine().toString().length());

Stringこれにより、リクエストの表現の長さが得られます。

于 2012-05-27T19:38:16.783 に答える