3

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.

Soumyadip Das
  • 1,781
  • 3
  • 16
  • 34

3 Answers3

14
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));
Caner
  • 57,267
  • 35
  • 174
  • 180
  • maybe this solution may be helpful for somebody: http://shomeser.blogspot.com/2013/12/redirect-stream-to-logcat.html – Oleksii K. Dec 23 '13 at 11:29
0

Your TAG should be a constant like:

public final String TAG = YourActivity.class.getSimpleName();

Then you would do something like:

Log.e(TAG, "You're message here:", e)

e is your error from print stack.

You also want to make sure you import the Log in the Android library.

Also, after looking at your code, you might want to surround your httpConnection() with try/catch statement, that way you can catch the error, and put it in your Log file. You have the Log printing if your stream has something, or isn't null, but you want to know if you don't have a connection, and that would give you a null value.

Hope that helps.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0
Just add this code to if(in != null):   

    byte[] reqBuffer = new byte[1024];
    int reqLen = 1024;
    int read = -1;

    StringBuilder result = new StringBuilder();
    try {
        while ((read = is.read(reqBuffer, 0, reqLen)) >= 0)
            result.append(new String(reqBuffer, 0, read));
    } catch (IOException e) {
        e.printStackTrace();
    } 


Log.d("TAG", result.toString());
Tamara
  • 275
  • 1
  • 10