2

CiphetInputStream を拡張する InputStream クラスを作成します。InputStream (さらにパーサーで入力として使用するもの) からすべてのデータをログに記録したいので、次のようにしました。

public class MyInputStream extends CipherInputStream {
    private OutputStream logStream = new ByteArrayOutputStream();
.....
    @Override
    public int read() throws IOException {
        int read = super.read();
        logStream.write(read);
        return read;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        int read = super.read(b, off, len);
        if (read > 0) {
            logStream.write(b, off, read);
        }
        return read;
    }

    @Override
    public int read(byte[] buffer) throws IOException {
        int read = super.read(buffer);
        if (read()>0) {
            logStream.write(buffer);
        }
        return read;
    }

    @Override
    public void close() throws IOException {
        log();
        super.close();
    }

    public void log() {
        String logStr = new String(((ByteArrayOutputStream) logStream).toByteArray(), Charset.defaultCharset());
        Log.d(getClass(), logStr);
        try {
            logStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

実際、私のストリームには次のようなものがあります。

<response>
<result>0</result>
</response>

しかし、この突然変異のようなログショーのsmth :

<<response>
<resultt >0</resullt>
</respoonse>
[and (?) symbol at the end]

助けてくれてありがとう!

4

2 に答える 2

3

TeeInputStreamとを組み合わせることができますLogger.stream():

new TeeInputStream(
  yourStream, 
  Logger.stream(Level.INFO, this)
);
于 2016-01-22T20:24:58.097 に答える
-1

logcat でログを見たい場合は、Log.i(String tag, String message);またはを試してくださいSystem.out.println("");。どちらも機能します。Log.d、 、Log.wおよびも使用できますLog.e

于 2013-05-08T07:40:22.763 に答える