如何将 Okhttp 的响应转换为输入流

How to convert the response from Okhttp into inputstream

代码:

  public static String getRequestNoPayload(String urlString, String loginApi, String mUsername) throws Exception {
        Response response;
        try{
            client.setConnectTimeout(20, TimeUnit.SECONDS); // connect timeout
            client.setReadTimeout(20, TimeUnit.SECONDS);    // socket timeout
            Request request = new Request.Builder()
                    .url(urlString)
                    .addHeader("username",loginApi)
                    .addHeader("password",mUsername)
                    .build();

            response = client.newCall(request).execute();

        }catch(Exception e){
            throw e;
        }
        return response.body().string();
    }

我想做什么: 我正在尝试将 response.body() 转换为 inputstream。如何实现

可以通过byteStream()

获取InputStream

例如

 InputStream is = response.body().byteStream();

到return,您必须更改方法的签名。从 StringInputStream