Alex Goncalves
Alex Goncalves

Reputation: 69

How to do a System.out.print in an Android project?

This is a pure Java project, to check if the connection between client and server is established, see if the received stream was converted into string, etc. I want to check if those step are working in my Android project. What is the aquivalent of a System.out.print or .println.

public class TestConnect {

    static String result = "";

    public static void main(String[] args) {

        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://127.0.0.1/index.php");

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();
            System.out.println(is);

            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;

                while ((line = reader.readLine()) != null) {
                    System.out.print(line);
                    sb.append(line + "\n");
                }

                is.close();
                result = sb.toString();
            } catch (Exception e) {
            }

            try {
                JSONArray jArray = new JSONArray(result);

                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    json_data.getString("nom");
                    // json_data.getInt("Id_Patients");

                    System.out.println(json_data.getString("nom"));
                    // r.add(json_data.getString("categorie"));
                }
            } catch (Exception e) {
            }
        } catch (Exception e) {
        }
    }
}

Upvotes: 4

Views: 1739

Answers (4)

Swayam
Swayam

Reputation: 16354

Use Log to print your desired values in the LogCat.

Log.v(MYTAG, myString);

MYTAG : The tag you would want to associate with your log messages so that you can later filer your LogCat messages to check only the required messages.

myString : The value that you want to write in the log, in String format.

You can use any of the log methods from Log.v(), Log.d(), Log.i(), Log.w(), Log.e() according to how you want to label your log message as. For example, if you want to label your log message as a debug message, use Log.d().

Upvotes: 1

Ahmad
Ahmad

Reputation: 72563

You can use System.out.println() It behaves the same as Log.i(TAG, String);

However in Android you can use various Log methods:

  • Log.v() - Verbose
  • Log.d() - Debug
  • Log.i() - Information
  • Log.w() - Warning
  • Log.e() - Error

Upvotes: 1

Artyom Kiriliyk
Artyom Kiriliyk

Reputation: 2513

Use Log class.

Log.i("TAG", "info");
Log.d("TAG", "debug");

Upvotes: 1

Frank
Frank

Reputation: 15641

Use the built-in Logger

A copy paste from the documentation: Generally, use the

  • Log.v()
  • Log.d()
  • Log.i()
  • Log.w()
  • Log.e()

methods.

The order in terms of verbosity, from least to most is ERROR, WARN, INFO, DEBUG, VERBOSE. Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.

Tip: A good convention is to declare a TAG constant in your class:

private static final String TAG = "MyActivity";

and use that in subsequent calls to the log methods.

Tip: Don't forget that when you make a call like

Log.v(TAG, "index=" + i);

that when you're building the string to pass into Log.d, the compiler uses a StringBuilder and at least three allocations occur: the StringBuilder itself, the buffer, and the String object. Realistically, there is also another buffer allocation and copy, and even more pressure on the gc. That means that if your log message is filtered out, you might be doing significant work and incurring significant overhead.

Upvotes: 3

Related Questions