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

时间:2015-04-29 12:42:10

标签: android inputstream okhttp

CODE:

  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。如何实现这个

1 个答案:

答案 0 :(得分:36)

您可以通过byteStream()

获取InputStream

E.g。

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

要返回它,您必须更改方法的签名。从StringInputStream