gpt4 book ai didi

java - 有没有办法在 Android 中找到 HTTP 请求和响应的完整大小(用于数据使用跟踪)?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:52:03 26 4
gpt4 key购买 nike

我想跟踪我的应用程序的数据使用情况,但为此,我需要能够获得 HttpUriRequestHttpResponse 的完整大小。仅仅获取 HttpEntity 的大小是不够的,因为请求和响应都有更多的数据正在传输( header 、参数、正在传输的实际 URL 等)。那么,有没有办法找到双向传输的全部数据量?

谢谢。

最佳答案

如果您只是想“之后”使用指标,您可以提供自己的合适 ClientConnectionManager 实现的子类,并在请求返回给管理器后获取相关的 HttpConnectionMetrics .

作为一个简单的(和轻微的 sh*tty)示例,子类 SingleClientConnManager,如下所示:

class MeasuringClientConnManager extends SingleClientConnManager {
private long mReceivedBytes = -1;
private long mSentBytes = -1;
public MeasuringClientConnManager(HttpParams params, SchemeRegistry schreg) {
super(params, schreg);
}
@Override
public void releaseConnection(ManagedClientConnection conn,
long validDuration, TimeUnit timeUnit) {
HttpConnectionMetrics metrics = conn.getMetrics();
mReceivedBytes = metrics.getReceivedBytesCount();
mSentBytes = metrics.getSentBytesCount();
metrics.reset();
super.releaseConnection(conn, validDuration, timeUnit);
}

public long getReceivedBytes() {
return mReceivedBytes;
}
public long getSentBytes() {
return mSentBytes;
}
}

像这样把它塞进你的 HTTP 客户端:

BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schreg = new SchemeRegistry();
schreg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Add SSL here if you need it
MeasuringClientConnManager conman = new MeasuringClientConnManager(params, schreg);
DefaultHttpClient client = new DefaultHttpClient(conman, params);
client.execute(new HttpGet("http://www.google.com"));
System.out.println(conman.getSentBytes());
System.out.println(conman.getReceivedBytes());

编辑:使用日志记录 session 输入和输出缓冲区以及损坏的线路日志 + 添加监听器接口(interface)的替代示例。

class MeasuringClientConnManager extends SingleClientConnManager {

interface WireListener {
void onUpdate(long sent, long recv);
}
private WireListener mListener;
private long mReceivedBytes = -1;
private long mSentBytes = -1;
private Wire mWire = new Wire(null) {
public boolean enabled() { return true; };
// Time to override a bunch of methods (otherwise Wire will crash)
public void input(byte[] b) throws IOException {mReceivedBytes += b.length; rxtx();};
public void input(byte[] b, int off, int len) throws IOException {mReceivedBytes += len; rxtx();}
public void input(InputStream instream) throws IOException {mReceivedBytes += count(instream); rxtx();}
public void input(String s) throws IOException {mReceivedBytes += s.length(); rxtx();};
public void input(int b) throws IOException {mReceivedBytes++; rxtx();}
public void output(byte[] b) throws IOException {mSentBytes += b.length; rxtx();}
public void output(byte[] b, int off, int len) throws IOException {mSentBytes += len; rxtx();}
public void output(int b) throws IOException {mSentBytes++; rxtx();}
public void output(String s) throws IOException {mSentBytes += s.length(); rxtx();}
public void output(InputStream outstream) throws IOException {mSentBytes += count(outstream); rxtx();};

private int count(InputStream is) throws IOException {
int result = 0;
byte[] b = new byte[1024 * 8];
int count;
while ((count = is.read(b)) > -1) {
result += count;
}
return result;
}
};

public MeasuringClientConnManager(HttpParams params, SchemeRegistry schreg) {
super(params, schreg);
}

public void setWireListener(WireListener wl) {
mListener = wl;
}

/**
* {@inheritDoc}
*/
@Override
protected ClientConnectionOperator createConnectionOperator(
SchemeRegistry schreg) {
final ClientConnectionOperator actual = super.createConnectionOperator(schreg);
ClientConnectionOperator wired = new ClientConnectionOperator() {
@Override
public void updateSecureConnection(OperatedClientConnection conn,
HttpHost target, HttpContext context, HttpParams params)
throws IOException {
actual.updateSecureConnection(conn, target, context, params);
}
@Override
public void openConnection(OperatedClientConnection conn, HttpHost target,
InetAddress local, HttpContext context, HttpParams params)
throws IOException {
actual.openConnection(conn, target, local, context, params);
}

@Override
public OperatedClientConnection createConnection() {
DefaultClientConnection conn = new DefaultClientConnection() {
@Override
protected SessionInputBuffer createSessionInputBuffer(
Socket socket, int buffersize, HttpParams params)
throws IOException {
return new LoggingSessionInputBuffer(super.createSessionInputBuffer(socket, buffersize, params), mWire);
}
@Override
protected SessionOutputBuffer createSessionOutputBuffer(
Socket socket, int buffersize, HttpParams params)
throws IOException {
return new LoggingSessionOutputBuffer(super.createSessionOutputBuffer(socket, buffersize, params), mWire);
}
};
return conn;
}
};
return wired;
}

void rxtx() {
WireListener l = mListener;
if (l != null) {
l.onUpdate(mSentBytes, mReceivedBytes);
}
}
}

用法非常相似,但现在在发送内容时使用电汇更新:

BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schreg = new SchemeRegistry();
schreg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Add SSL here if you need it
MeasuringClientConnManager conman = new MeasuringClientConnManager(params, schreg);
conman.setWireListener(new MeasuringClientConnManager.WireListener() {
@Override
public void onUpdate(long sent, long recv) {
System.out.println("WIRE sent: " + sent + ", recv: " + recv);
}
});
DefaultHttpClient client = new DefaultHttpClient(conman, params);
client.execute(new HttpGet("http://www.thirdbase.se"));

关于java - 有没有办法在 Android 中找到 HTTP 请求和响应的完整大小(用于数据使用跟踪)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10788971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com