1

我在我当前的 android 项目上使用 okhttp 和 okio 来获取和读取我正在发送的一些二进制流,在流中我使用该方法发送一些 UTF 值

DataOutputStream.writeUTF().

“okhttp”为您提供了 2 个选项,或者获取 inputStream,因此您可以直接使用 DataInputStream.readUTF() 方法。

另一种选择是将流作为 BufferedSource 获取并使用其方法读取数据,我很惊讶读取 UTF 的方法需要您为其提供字节数。

String readUtf8(long byteCount)

为了读取来自服务器的 UTF,我添加了以下方法

public static String readUtf(BufferedSource bufferedSource)throws IOException {
       int length =  readShortAsUnsignedInt(bufferedSource);
       return  bufferedSource.readUtf8(length);
    }

    public static int readShortAsUnsignedInt(BufferedSource bufferedSource)throws IOException {
        return bufferedSource.readShort() & 0xffff;
    }

这是正确的方法吗?

4

0 回答 0