Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the response from Okhttp into inputstream

Tags:

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

What i am trying to do: I am trying to convert response.body() into a inputstream. How to achieve this

like image 303
Devrath Avatar asked Apr 29 '15 12:04

Devrath


People also ask

How do I get data from okhttp3 response?

For this, we can access the body via the body() method of the Response object. The ResponseBody class has several options for extracting this data: byteStream(): exposes the raw bytes of the body as an InputStream; we can use this for all formats, but usually it is used for binaries and files.

How do you get an InputStream response?

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


1 Answers

you can get the InputStream through byteStream()

E.g.

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

to return it you have to change the method's signature. From String to InputStream

like image 151
Blackbelt Avatar answered Oct 12 '22 16:10

Blackbelt