Android アプリで独自のデータ使用量を追跡したいと考えています。HTTP 応答の Content-Length を取得できますが、送信前に要求のサイズを取得する方法がわかりません。すべてのリクエスト (GET、POST、PUT など) は のインスタンスですHttpUriRequest
。
ありがとう
Android アプリで独自のデータ使用量を追跡したいと考えています。HTTP 応答の Content-Length を取得できますが、送信前に要求のサイズを取得する方法がわかりません。すべてのリクエスト (GET、POST、PUT など) は のインスタンスですHttpUriRequest
。
ありがとう
コンテンツを含むすべてのリクエストは、 のサブクラスである必要があり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.
}
このクラスは、 というメソッドを持つクラスHttpUriRequest
から継承されます。この関数を呼び出し、メソッドを呼び出してから関数を呼び出して、要求の長さを見つけることができます。HttpRequest
getRequestLine()
toString()
length()
例:
HttpUriRequest req = ...;
int reqLength = req.getRequestLine().toString().length());
String
これにより、リクエストの表現の長さが得られます。