Printing an InputStream in Logcat

android, inputstream

Solution

String convertStreamToString(java.io.InputStream is) {
    try {
        return new java.util.Scanner(is).useDelimiter("\\A").next();
    } catch (java.util.NoSuchElementException e) {
        return "";
    }
}

And in your code:

                Log.e(TAG, ">>>>>PRINTING<<<<<");
                Log.e(TAG, in.toString());
                Log.e(TAG, convertStreamToString(in));

Problem

I want to print the InputStream in logcat(for testing/later I will use it), my current code is as follows. ``` @Override protected Void doInBackground(Void... params) { try { InputStream in = null; int response = -1; URL url = new URL( "http://any-website.com/search/users/sports+persons"); URLConnection conn = null; HttpURLConnection httpConn = null; conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } if (in != null) { Log.e(TAG, ">>>>>PRINTING<<<<<"); Log.e(TAG, in.toString()); // TODO: print 'in' from here } in.close(); in = null; } catch (Exception e) { e.printStackTrace(); } return null; } ``` But I am not able to do this, so please check the code and add/modify the code to do this.

Original source

Related problems