栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

如果只希望度量“之后”,则可以提供适合自己的

ClientConnectionManager
实现的子类,并
HttpConnectionMetrics
在将请求返回给管理器之后获取相关的子类。

作为一个简单的示例(以及一点点sh * tty),subclass

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 itMeasuringClientConnManager 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());

编辑 :另一个示例,该示例使用日志记录会话输入和输出缓冲区以及错误的有线日志+添加侦听器接口。

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;    }        @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 itMeasuringClientConnManager 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"));


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470172.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号