Zayko in the Net

Personal blog of Vitaly Zayko


How to convert an InputStream to String? (Java, Android)

If you call a Web service from your Android App (I believe this should work in other Java Apps as well but personally tested in Android), you probably will want to convert InputStream into a string which is not that simple. And here is simpliest way how to do that:

 HttpURLConnection conn = (HttpURLConnection)url.openConnection();
 if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
 	InputStream inStream = new BufferedInputStream(conn.getInputStream());
     // Here are we go:
  String res = new Scanner(inStream).useDelimiter("\\\\A").next();